Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24734 Accepted Submission(s): 14953
Problem Description
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.
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)
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).
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
Source
Asia 2004, Ehime (Japan), Japan Domestic
染色数块问题 bfs板子或者dfs板子都过。。。。
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mset(a,b) memset(a,b,sizeof(a)) #define sz size() #define cl clear() #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164 typedef long long ll; typedef pair<int,int> pr; const int inf = 99999999; const double eps = 1e-8; const int dir4[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; const int dir8[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int maxn = 1; //易修改 struct node{ int x, y; node(){ } node(int x,int y) { this->x = x; this->y = y; } // const operator ? (const & node a) const // { // return // } }; int m, n; int sx, sy; char mp[22][22]; int visit[22][22]; int maxS; int bfs() { queue<node> q; q.push(node(sx,sy)); visit[sx][sy] = 1; while(!q.empty()) { node t = q.front(); q.pop(); for(int i = 0;i < 4;i ++) { int dx = t.x + dir4[i][0]; int dy = t.y + dir4[i][1]; if(dx < 1 || dy < 1 || dx > n || dy > m || visit[dx][dy] || mp[dx][dy] == '#') continue; q.push(node(dx,dy)); visit[dx][dy] = 1; } } int ts = 0; for(int i = 1;i <= n;i ++) { for(int j = 1;j <= m;j ++) if(visit[i][j]) ts ++; } return ts; } int main() { while(cin >> m >> n && m && n) { maxS = 0; mset(visit,0); for(int i = 1;i <= n;i ++) { for(int j = 1;j <= m;j ++) { cin >> mp[i][j]; if(mp[i][j] == '@') { sx = i; sy = j; } } } cout << bfs() << endl; } return 0; }