Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19524 Accepted Submission(s): 11879
Problem DescriptionThere 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.
InputThe 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)
OutputFor 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 Input6 9....#......#..............................#@...#.#..#.
Sample Output45
题意:
@为起点,可上下左右走,不能走#,问可以走多少.
简单DFS
附AC代码:
1 #include2 using namespace std; 3 4 int ans,sx,sy,n,m; 5 char ma[30][30]; 6 int a[4][2]={ 1,0,-1,0,0,1,0,-1}; 7 8 void dfs(int x,int y){ 9 ans++;10 ma[x][y]='#';11 for(int i=0;i<4;i++){12 int tx=x+a[i][0];13 int ty=y+a[i][1];14 if(tx =0&&ty =0&&ma[tx][ty]=='.'){15 dfs(tx,ty);16 }17 }18 return ;19 }20 21 int main(){22 while(cin>>n>>m){23 if(n==0&&m==0)24 break;25 ans=0;26 for(int i=0;i >ma[i][j];29 if(ma[i][j]=='@'){30 sx=i;31 sy=j;32 //cout< <<" "< <