标签:char out iostream continue stream color cin show red
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int N = 30; 5 int row, col, sx, sy; 6 int vis[N][N]; 7 char a[N][N]; 8 int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; 9 void dfs(int x, int y){ 10 for(int i = 0; i < 4; i++){ 11 int tx = dir[i][0] + x, ty = dir[i][1] + y; 12 if(tx < 0 || tx > row - 1 || ty < 0 || ty > col - 1) continue; 13 if(!vis[tx][ty] && a[tx][ty] == ‘.‘){ 14 vis[tx][ty] = 1; 15 dfs(tx, ty); 16 } 17 } 18 } 19 int main(){ 20 while(cin >> col >> row){ 21 if(col == 0 && row == 0) break; 22 int cnt = 0; 23 memset(vis, 0, sizeof(vis)); 24 for(int i = 0; i < row; i++){ 25 for(int j = 0; j < col; j++){ 26 cin >> a[i][j]; 27 if(a[i][j] == ‘@‘){ 28 sx = i; sy = j; 29 } 30 } 31 } 32 vis[sx][sy] = 1; 33 dfs(sx, sy); 34 for(int i = 0; i < row; i++){ 35 for(int j = 0; j < col; j++){ 36 if(vis[i][j] == 1) cnt++; 37 } 38 } 39 cout << cnt << endl; 40 } 41 return 0; 42 }
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 struct node{ 6 int x, y; 7 }que[2003]; 8 char a[23][23]; 9 int book[25][25], cnt, h, w; 10 int pos[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; 11 void bfs(int rx, int ry){ 12 int head = 1, tail = 1; 13 que[tail].x = rx; que[tail].y = ry; 14 book[rx][ry] = 1; 15 tail++; cnt++; 16 while(head < tail){ 17 for(int i = 0; i < 4; i++){ 18 int tx = que[head].x + pos[i][0]; 19 int ty = que[head].y + pos[i][1]; 20 if(tx < 0 || tx > h - 1 || ty < 0 || ty > w - 1) continue; 21 if(a[tx][ty] == ‘.‘ && book[tx][ty] == 0){ 22 cnt++; 23 book[tx][ty] = 1; 24 que[tail].x = tx; que[tail].y = ty; 25 tail++; 26 } 27 } 28 head++; 29 } 30 } 31 int main(){ 32 while(cin >> w >> h){ 33 if(w == 0 && h == 0) break; 34 memset(a, 0, sizeof(a)); 35 memset(book, 0, sizeof(book)); 36 cnt = 0; 37 for(int i = 0; i < h; i++){ 38 cin >> a[i]; 39 } 40 int rx, ry; 41 for(int i = 0; i < h; i++){ 42 for(int j = 0; j < w; j++){ 43 if(a[i][j] == ‘@‘){ 44 rx = i; 45 ry = j; 46 } 47 } 48 } 49 bfs(rx, ry); 50 cout << cnt << endl; 51 } 52 return 0; 53 }
标签:char out iostream continue stream color cin show red
原文地址:https://www.cnblogs.com/zoom1109/p/11197123.html