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.
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.
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).
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45
59
6
13
The problem asked us to figure out how many black squares are adjacent to the square where the man stand directly or indirectly.So we use BFS here to obatin the number of continuous squares(DFS,as the recommended way ,would also be OK but I 've not tried it yet).A deque in STL is used here. In the while loop. we add the adjacent square of the current square in each loop, so we can guarantee that all of the asked squares could be traveled sequenctially.
Here is the Accepted code:
#include<iostream> #include<iomanip> #include<fstream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<complex> #include<set> #include<map> #include<list> #include<vector> #include<deque> using namespace std; typedef long long ll; typedef vector<int>vec; typedef vector<vec>mat; const int INF = 0x3f3f3f3f; const int Mod = 1e9 + 7; const double eps = 1e-7; bool a[22][22];//the map bool v[22][22];//visited int dirX[4] = { 1,0,-1,0 }; int dirY[4] = { 0,1,0,-1 }; struct point{ int x; int y; point() { x = y = 0; } point(int X, int Y) { x = X; y = Y; } point(const point &p) { x = p.x; y = p.y; } }; deque<point>que;//for bfs int main() {/*at first test one data*/ //ifstream ifs; //ifs.open("D:\\documents\\programDatas\\POJ1979\\test.txt", ifstream::in); int m, n;//the size while (cin >> n >> m &&n) { memset(a, 0, sizeof(a)); memset(v, 0, sizeof(v)); point ini; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { char temp; cin >> temp; if (temp == '#')a[i][j] = 1; else if (temp == '@') { ini.x = i; ini.y = j; } } } //debugging /*for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cout << a[i][j] << " "; } cout << "\n"; } cout <<setw(3) <<ini.x <<setw(3)<< ini.y << endl; */ que.push_back(ini); int ans = 0;//total black points while (que.size()) { point prv(que[0]);//previous point que.pop_front(); v[prv.x][prv.y] = 1; ans++; for (int i = 0; i < 4; i++) { int curX = prv.x + dirX[i]; int curY = prv.y + dirY[i]; if ((curX >= 1) && (curX <= m) && (curY >= 1) && (curY <= n) && v[curX][curY] == 0 && a[curX][curY] == 0) { que.push_back(point(curX, curY)); v[curX][curY] = 1; } } } cout << ans << endl; } //system("pause"); return 0; }