Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
水题#include <iostream> #include <stdio.h> using namespace std; int cnt; char grid[32][32]; bool used[32][32]; int m,n; int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; void dfs(int x, int y){ cnt ++; used[x][y] = true; for(int d = 0; d < 4; d++){ int nx = x+dir[d][0], ny = y+dir[d][1]; if(nx>=0 && nx<n && ny>=0 && ny<m && !used[nx][ny] && grid[nx][ny] == '.'){ dfs(nx,ny); } } } int main() { while(1){ scanf("%d%d",&m,&n); if(!m && !n) break; int sX, sY; for(int i = 0; i < n; i++){ scanf("%s", grid[i]); for(int j = 0; j < m; j++){ if(grid[i][j] == '@'){ grid[i][j] = '.'; sX = i, sY = j; } } } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ used[i][j] = false; } } cnt = 0; dfs(sX, sY); printf("%d\n", cnt); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator