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.
Input
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)
Output
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).
bool Map[N][M]; int n, m; structpoint { int x, y; } start;
intbfs() { int ans = 0; queue<point> q; q.push(start); // q while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) // 一批一批地处理,每一批都是步数相同的 { point now = q.front(); q.pop(); ans++; Map[now.x][now.y] = 0;
for (int j = 0; j < 4; j++) { int Nx = now.x + dir[j][0]; int Ny = now.y + dir[j][1]; if (Nx < 0 || Nx >= n || Ny < 0 || Ny >= m || !Map[Nx][Ny]) continue; Map[Nx][Ny] = 0; q.push({Nx, Ny}); } } } return ans; } intmain() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
while (cin >> m >> n) { if (n == 0 && m == 0) continue;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char tmp = getchar(); while (tmp != '.' && tmp != '#' && tmp != '@') tmp = getchar();
#include<bits/stdc++.h> usingnamespace std; constint N = 16; int dir[6][3] = { {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}; structpoint { int x, y, z; } S, T; bool Map[N][N][N]; int n;
intbfs() { int len = 0; queue<point> q; q.push(S); Map[S.x][S.y][S.z] = true; while (!q.empty()) {
int size = q.size(); for (int i = 0; i < size; i++) { point p = q.front(); q.pop(); if (p.x == T.x && p.y == T.y && p.z == T.z) return len; for (int i = 0; i < 6; i++) { int x = p.x + dir[i][0]; int y = p.y + dir[i][1]; int z = p.z + dir[i][2]; if (x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n && !Map[x][y][z]) { Map[x][y][z] = true; q.push({x, y, z}); } } } len++; } return-1; }
while (scanf("%*s %d", &n) != EOF) { memset(Map, 0, sizeof(Map)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { char c = getchar(); while (c != 'O' && c != 'X') c = getchar();
#include<bits/stdc++.h> #define max(a, b) ((a) > (b) ? (a) : (b)) usingnamespace std; constint N = 1024; constint INF = 0x3f3f3f3f; // bool Map[N][N]; bool vis[N]; int friendsList[N][N], dis[N]; int n, m;
// int bfs(int id) // { // memset(vis, 0, sizeof(vis)); // queue<int> q; // q.push(id); // int sum = 0, len = 0; // while (!q.empty() && sum != n) // { // int size = q.size(); // for (int k = 0; k < size; k++) // { // int now = q.front(); // q.pop(); // if (vis[now]) // continue; // vis[now] = 1; // sum++; // for (int i = 0; i < n; i++) // { // if (Map[now][i] && !vis[i]) // { // q.push(i); // } // } // } // len++; // } // // for (int i = 0; i < n; i++) // // { // // if (!vis[i]) // // return -1; // // } // if (sum != n) // return -1; // else // return len - 1; // }
intbfs(int start) { fill(vis, vis + n, 0); fill(dis, dis + n, INF); queue<int> q; q.push(start); vis[start] = 1; dis[start] = 0; while (!q.empty()) { int now = q.front(); q.pop();
for (int i = 1; i <= friendsList[now][0]; i++) { int next = friendsList[now][i]; if (!vis[next]) { vis[next] = 1; dis[next] = dis[now] + 1; q.push(next); } } } int maxDis = 0; for (int i = 0; i < n; i++) { if (dis[i] == INF) return-1; maxDis = max(maxDis, dis[i]); } return maxDis; }