Red and Black
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 16
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 613
//题意,从@开使搜,#是墙,输出一共有多少连在一起的.(加上@);
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<cstdlib> #include<map> using namespace std; int n,m; char a[100][100]; int s; int c[4][2]={0,-1,0,1,1,0,-1,0}; void ss(int x,int y) { int i; if(x<0||y<0||x>=n||y>=m||a[x][y]=='#') return; else { s++; a[x][y]='#'; for(i=0;i<4;i++) { ss(x+c[i][0],y+c[i][1]); } } } int main() { while(scanf("%d%d\n",&m,&n)) { if(n==0||m==0) break; s=0; int i,j; for(i=0;i<n;i++) gets(a[i]); for(i=0;i<n;i++) { for(j=0;j<m;j++) if(a[i][j]=='@') { ss(i,j); } } printf("%d\n",s); } }