Problem:


Problem B: Red and Black

 There is a rectangular room, covered with square tiles. Each tile is
colored either red or black. A man is standing on a black tile.
From a tile, he can move to one of four adjacent tiles. But he can't 
move on red tiles, he can move only on black tiles.

 Write a program to count the number of black tiles which he can reach 
by repeating the moves described above. 

Input

 The input consists of multiple data sets.
 A data set starts with a line containing two positive integers W and H;
 W and H are the numbers of tiles in the x-
 and y- directions, respectively. W and H are 
not more than 20.

  There are H more lines in the data set, each of which
includes W characters. Each character represents the color of a
tile as follows.

'.' - a black tile

'#' - a red tile

'@' - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

Output

  For each data set, your program 
should output a line which contains the number of 
tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Output for the Sample Input

45
59
6
13



Sample input:
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0


Sample output:
45
59
6
13

Submission status: Runtime Error

Solution code:
while True:
    W,H = [int(x) for x in input().split()]
    if W==0 and H==0:
        exit()
    tiles = []
    X = 0
    Y = 0
    for i in range(H):
        tiles.append(input())
        if '@' in tiles[i]:
            Y = i
            X = tiles[i].index('@')
    d = [[9999 for j in range(H*W)] for i in range(H*W)]
    for i in range(H*W):
        d[i][i] = 0
    for i in range(H):
        for j in range(W):
            if j>0:
                if tiles[i][j] in '.@' and tiles[i][j-1] in '.@':
                    d[W*i+j][W*i+j-1] = 1
            if j<W-1:
                if tiles[i][j] in '.@' and tiles[i][j+1] in '.@':
                    d[W*i+j][W*i+j+1] = 1
            if i>0:
                if tiles[i][j] in '.@' and tiles[i-1][j] in '.@':
                    d[W*i+j][W*i+j-W] = 1
            if j<H-1:
                if tiles[i][j] in '.@' and tiles[i+1][j] in '.@':
                    d[W*i+j][W*i+j+W] = 1
    for k in range(H*W):
        for i in range(H*W):
            for j in range(H*W):
                if d[i][j] > d[i][k]+d[k][j]:
                    d[i][j] = d[i][k]+d[k][j]
    cnt = 0
    for i in range(H*W):
        if d[X+Y*H][i]<9999:
            cnt += 1
    print(cnt)

Submission status: Runtime Error

Solution code:
re = open("input")
count = 0
role = {".": True, "@": False, "#": False}
def now(xpoint, ypoint):
    return role[room[ypoint][xpoint]]

def search(xpoint, ypoint):
    global count
    if 0 <= xpoint < wh[0] and 0 <= ypoint < wh[1] and now(xpoint, ypoint):
        count += 1
        room[ypoint][xpoint] = "@"
        search(xpoint - 1, ypoint)
        search(xpoint + 1, ypoint)
        search(xpoint, ypoint + 1)
        search(xpoint, ypoint - 1)

while True:
    wh = map(int, re.readline().split())
    room = []
    count = 0
    for ypoint in range(wh[1]):
        line = re.readline().replace("\n", "")
        if "@" in line:
            xp = line.index("@")
            yp = ypoint
        room.append([x for x in line])
    if not room: break
    room[yp][xp] = "."
    search(xp, yp)
    print count

Submission status: Runtime Error

Solution code:
def getter(targ,x,y):
    #print(x,y,len(targ[0]),len(targ))
    if 0 > x or x >= len(targ) or 0 > y or y >= len(targ[0]) or targ[x][y] == '#':
        return 0
    else:
        targ[x][y] = '#'
        return 1 + getter(targ,x+1,y) + getter(targ,x-1,y) + getter(targ,x,y+1) + getter(targ,x,y-1)
with open("input1.txt") as f:
    width,height = [int(n) for n in f.readline().split(" ")]
    global lab
    while True:
        lab,start = [],[]
        for a in range(height):
            disp = [n for n in f.readline().rstrip('\n')]
            if '@' in disp:
                start.extend([disp.index('@'),a])
            lab.append(disp)
        else:
            a,b = start
            print(getter(lab,b,a))
            width,height = [int(n) for n in f.readline().split(" ")]
            if width == 0 and height == 0:
                break

Submission status: Runtime Error

Solution code:
data, res

def valid(x, y):
    global data
    return 0 <= x < W and 0 <= y < H and data[y][x] != '#'

def dfs(x, y):
    global data, res
    if valid(x, y):
        res += 1
        data[y][x] = '#'
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4):
            dfs(x + dx[i], y + dy[i])
    else:
        return
    
while True:
    global data, res
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    data = [list(input()) for _ in range(H)]
    res = 0
    for i in range(W):
        for j in range(H):
            if data[j][i] == '@':
                dfs(i, j)
                break
    print(res)

Submission status: Runtime Error

Solution code:
data, res

def valid(x, y):
    global data
    return 0 <= x < W and 0 <= y < H and data[y][x] != '#'

def dfs(x, y):
    global data, res
    if valid(x, y):
        res += 1
        data[y][x] = '#'
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4):
            dfs(x + dx[i], y + dy[i])
    else:
        return
    
while True:
    global data, res
    W, H = map(int, input().split())
    if not W:
        break
    data = [list(input()) for _ in range(H)]
    res = 0
    for i in range(W):
        for j in range(H):
            if data[j][i] == '@':
                dfs(i, j)
                break
    print(res)

Submission status: Runtime Error

Solution code:
data, res = [], 0

def valid(x, y):
    global data
    return 0 <= x < W and 0 <= y < H and data[y][x] != '#'

def dfs(x, y):
    global data, res
    if valid(x, y):
        res += 1
        data[y][x] = '#'
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4):
            dfs(x + dx[i], y + dy[i])
    else:
        return
    
while True:
    global data, res
    W, H = map(int, input().split())
    if not W:
        break
    data = [list(input()) for _ in range(H)]
    res = 0
    for i in range(W):
        for j in range(H):
            if data[j][i] == '@':
                dfs(i, j)
                break
    print(res)

Submission status: Runtime Error

Solution code:

def depth_first_search(x, y):
    """
    ??±???????????¢?´¢?????????
    :param x: ?????¨??°????¨?
    :param y: ?????¨??°??????
    :return: ?????°
    """

    # ????????????????????§?¢??????????
    # ??????x  ,y+1
    # ??????x+1,y
    # ??????x  ,y-1
    # ??????x-1,y
    move_list = [[0,1],[1,0],[0,-1],[-1,0]]
    for i in range(0,4,1):
        next_x = x+move_list[i][0]
        next_y = y+move_list[i][1]
        # tile_map??????????§??????§????????´??????'.'???
        if 0 <= next_x and 0 <= next_y and \
            next_x < W and next_y < H and \
            tile_map[next_y][next_x] != '#':

            # ?§?????????????????????¨??°????§??????§??????????????????"#"??????????????????
            tile_map[y][x] = '#'
            import __main__
            __main__.move_point = __main__.move_point+1
            depth_first_search(next_x, next_y)


def get_start_point(tile_map):
    """
    @????????????????????????
    :param tile_map:
    :return:
    """
    for i, y in enumerate(tile_map):
        for j, x in enumerate(y):
            if x == '@':
                return i, j



if __name__ == "__main__":
    move_point = 0
    first = True
    while (True):
        # input_1 : tile????????????
        tile_size = input('').split(' ')
        W, H = [int(x) for x in tile_size]
        if W == 0 and H == 0:
            break
        if first:
            first = False
        else:
            print('')
        tile_map=[''] * H
        for i in range(0, H, 1):
            # input_2 : tile???????????????????????????
            map_line = list(input(''))
            tile_map[i] = map_line
        start_H, start_W = get_start_point(tile_map)
        depth_first_search(start_W, start_H)
        import ipdb;ipdb.set_trace()
        print(move_point)

Submission status: Runtime Error

Solution code:
def search(M, l, walkable):
    i, j = (l.pop(0))
    nexts = []
    if i !=0:
        nexts.append((i-1, j))
    if i != len(M)-1:
        nexts.append((i+1, j))
    if j != 0:
        nexts.append((i, j-1))
    if j != len(M[0])-1:
        nexts.append((i, j+1))

    for nex in nexts:
        x, y = nex
        if M[x][y] == '.' and not nex in walkable:
            l.append(nex)
            walkable.append(nex)

inp = ''
while '0 0' != (inp = input()):
    i, j = [int(x) for x in inp.split()]
    M = []
    l = []
    for n in range(j):
        M.append(input())
        if '@' in M[n]:
            l.append((n, M[n].find('@')))

    walkable = []
    while l:
        search(M, l, walkable)
    print(len(walkable))

Submission status: Runtime Error

Solution code:
def search(M, l, walkable):
    i, j = (l.pop(0))
    nexts = []
    if i !=0:
        nexts.append((i-1, j))
    if i != len(M)-1:
        nexts.append((i+1, j))
    if j != 0:
        nexts.append((i, j-1))
    if j != len(M[0])-1:
        nexts.append((i, j+1))

    for nex in nexts:
        x, y = nex
        if M[x][y] == '.' and not nex in walkable:
            l.append(nex)
            walkable.append(nex)

inp = ''
while '0 0' != (inp = input()):
    i, j = [int(x) for x in inp.split()]
    M = [input() for i in range(j)]
    l = []
    for n in len(M):
        if '@' in M[n]:
            l.append((n, M[n].find('@')))

    walkable = []
    while l:
        search(M, l, walkable)
    print(len(walkable)+1)

Submission status: Runtime Error

Solution code:
import queue

def solve(w, h):
    q = queue.Queue()
    m = [['#' for i in range(w+2)] for j in range(h+2)]
    for i in range(h):
        inp = input()
        for j in range(w):
            m[i+1][j+1] = inp[j]
            if m[i+1][j+1] == '@':
                start = [i+1,j+1]
    q.put(start)
    c = 1
    while not q.empty():
        cur = q.get()
        XY = [(1,0), (-1, 0), (0, 1), (0, -1)]
        for x, y in XY:
            cx = cur[0]+x
            cy = cur[1]+y
            if m[cx][cy] == '.':
                m[cx][cy] = '#'
                q.put([cx, cy])
                c+=1
    return c

while True:
    w, h = [int(i) for i in input().split()]
    if w == 0 and h == 0:
        break
    print solve(w,h)

Submission status: Runtime Error

Solution code:
def search(M, l, walkable):
    i, j = (l.pop(0))
    nexts = []
    if i !=0:
        nexts.append((i-1, j))
    if i != len(M)-1:
        nexts.append((i+1, j))
    if j != 0:
        nexts.append((i, j-1))
    if j != len(M[0])-1:
        nexts.append((i, j+1))

    for nex in nexts:
        x, y = nex
        if M[x][y] == '.' and not nex in walkable:
            l.append(nex)
            walkable.append(nex)

inp = ''
while '0 0' != (inp = input()):
    i, j = [int(x) for x in inp.split()]
    M = [input() for k in range(j)]
    l = []
    for n in range(j):
        if '@' in M[n]:
            l.append((n, M[n].find('@')))

    walkable = []
    while l:
        search(M, l, walkable)
    print(len(walkable)+1)

Submission status: Runtime Error

Solution code:
def search(M, l, walkable):
    i, j = (l.pop(0))
    nexts = []
    if i !=0:
        nexts.append((i-1, j))
    if i != len(M)-1:
        nexts.append((i+1, j))
    if j != 0:
        nexts.append((i, j-1))
    if j != len(M[0])-1:
        nexts.append((i, j+1))

    for nex in nexts:
        x, y = nex
        if M[x][y] == '.' and not nex in walkable:
            l.append(nex)
            walkable.append(nex)

inp = ''
while '0 0' != (inp = input().rstrip()):
    i, j = [int(x) for x in inp.split()]
    M = [input() for k in range(j)]
    l = []
    for n in range(j):
        if '@' in M[n]:
            l.append((n, M[n].find('@')))

    walkable = []
    while l:
        search(M, l, walkable)
    print(len(walkable)+1)

Submission status: Runtime Error

Solution code:
while 1:
	x,y = list(map(int,input()split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return
		if m[cy][cx] = '.'v or  m[cy][cx] = '@'
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Submission status: Runtime Error

Solution code:
while 1:
	x,y = list(map(int,input()split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return 0
		if m[cy][cx] = '.'v or  m[cy][cx] = '@'
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Submission status: Runtime Error

Solution code:
while 1:
	x,y = list(map(int,input().split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return
		if m[cy][cx] = '.'v or  m[cy][cx] = '@'
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Submission status: Runtime Error

Solution code:
while 1:
	x,y = list(map(int,input().split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return
		if m[cy][cx] == '.'v or  m[cy][cx] == '@'
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Submission status: Runtime Error

Solution code:
while 1:
	x,y = list(map(int,input().split()))
	if x == 0:
		break

	pp = [1,-1,0,0]
	qq = [0,0,1,-1]
	m = []
	for i in range(y):
		m.append(list(input()))
		if '@' in m[-1]:
			ss = m[-1].index('@')
			tt = i
	cnt = 0

	def move (cx,cy):
		global cnt
		if cy >= y or cy < 0 or cx >= x or cx < 0:
			return
		if m[cy][cx] == '.' or  m[cy][cx] == '@'
			cnt += 1
			m[cy][cx] = '0'
			for i in range(4):
				move(cx + pp[i],cy + qq[i])
	move(ss,tt)
	print(cnt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

count = 0
def dfs(i, j, island):
    global count
    print i, j
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    
    if i < 0 or i >= len(island) or j < 0 or j >= len(island[0]):
        return

    if island[i][j] == '#' or island[i][j] == 'x':
        return

    island[i][j] = 'x'
    count += 1
    
    for d in dxy:
        dfs(i+d[0], j+d[1], island)

def solve(w, h, island):
    s_i = 0
    s_j = 0
    for i, row in enumerate(island):
        for j, s in enumerate(row):
            if s == '@':
                s_i = i
                s_j = j
                break

    dfs(s_i, s_j, island)
    print count

def func():
    '''

    '''
    while True:
        global count
        wh = map(int, raw_input().split())
        w = wh[0]
        h = wh[1]
        island = []
        count = 0
        for i in range(h):
            row = []
            line = raw_input()
            for j in line:
                row.append(j)
            island.append(row)
        
        for row in island:
            for j in row:
                print j
            print ''

        solve(w, h, island)
    return None


func()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

count = 0
def dfs(i, j, island):
    global count
    #print i, j
    dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    
    if i < 0 or i >= len(island) or j < 0 or j >= len(island[0]):
        return

    if island[i][j] == '#' or island[i][j] == 'x':
        return

    island[i][j] = 'x'
    count += 1
    
    for d in dxy:
        dfs(i+d[0], j+d[1], island)

def solve(w, h, island):
    s_i = 0
    s_j = 0
    for i, row in enumerate(island):
        for j, s in enumerate(row):
            if s == '@':
                s_i = i
                s_j = j
                break

    dfs(s_i, s_j, island)
    print count

def func():
    '''

    '''
    while True:
        global count
        wh = map(int, raw_input().split())
        w = wh[0]
        h = wh[1]
        island = []
        count = 0
        for i in range(h):
            row = []
            line = raw_input()
            for j in line:
                row.append(j)
            island.append(row)
        
        # for row in island:
        #     for j in row:
        #         print j
        #     print ''

        solve(w, h, island)


func()