Created
July 29, 2013 08:54
-
-
Save Arthraim/6103052 to your computer and use it in GitHub Desktop.
第一次做出广搜好吗!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
# http://acm.hdu.edu.cn/showproblem.php?pid=1312 | |
while True: | |
queue = [] | |
#input W and H | |
WnH = raw_input() | |
width = int(WnH.split(' ')[0]) | |
height = int(WnH.split(' ')[1]) | |
if width == 0 and height == 0: | |
break | |
# input matrix | |
matrix = [[] for i in range(width)] | |
for l_idx in range(height): | |
line_in = raw_input() | |
for c_idx in range(len(line_in)): | |
column = matrix[c_idx] | |
char = line_in[c_idx] | |
column.append(char) | |
if char == '@': | |
queue.append((c_idx, l_idx)) | |
count = 0 | |
# search | |
while True: | |
# print queue | |
new_queue = [] | |
for item in queue: | |
x = item[0] | |
y = item[1] | |
# visited | |
if matrix[x][y] == '.': | |
count = count + 1 | |
# if matrix[x][y] == '#': | |
# continue | |
matrix[x][y] = '#' | |
#x-1 | |
if(x-1 >= 0) and (matrix[x-1][y] != '#'): | |
new_queue.append((x-1, y)) | |
matrix[x-1][y] = '#' | |
#x+1 | |
if(x+1 < width) and (matrix[x+1][y] != '#'): | |
new_queue.append((x+1, y)) | |
matrix[x+1][y] = '#' | |
#y-1 | |
if(y-1 >= 0) and (matrix[x][y-1] != '#'): | |
new_queue.append((x, y-1)) | |
matrix[x][y-1] = '#' | |
#y+1 | |
if(y+1 < height) and (matrix[x][y+1] != '#'): | |
new_queue.append((x, y+1)) | |
matrix[x][y+1] = '#' | |
if len(new_queue) <= 0: | |
break | |
queue = new_queue | |
print count + 1 # 好像答案包括起点 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment