hdu 1312 Red and Black(BFS水题)

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9684    Accepted Submission(s): 6021

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.

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).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <string>
 5 #include <iomanip>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 using namespace std;
11 #define maxn 25
12 #define INF 9999999
13 char mp[maxn][maxn];
14 struct Node{
15     int x, y, t;
16 }s;
17 int W, H, sx, sy; //宽,高
18 int dir[4][2] = {{0,1}, {1,0}, {-1,0},{0,-1}};
19 int timE[maxn][maxn], vis[maxn][maxn];
20 void BFS(Node start){
21     queue <Node> Q;
22     Q.push(start);
23     vis[start.x][start.y] = 1;
24     while(!Q.empty()){
25         Node hd = Q.front(); Q.pop();
26         for(int i = 0; i < 4; i++){
27             int nx = hd.x + dir[i][0];
28             int ny = hd.y + dir[i][1];
29             if(nx >= 1 && nx <= H && ny >= 1 && ny <= W && mp[nx][ny] != ‘#‘ && !vis[nx][ny]){
30                 Node temp;
31                 temp.x = nx; temp.y = ny; temp.t = hd.t+1;
32                 vis[nx][ny] = 1;
33                 timE[nx][ny] = temp.t;
34                     Q.push(temp);
35
36
37             }
38         }
39     }
40
41 }
42 int main(){
43     while(~scanf("%d%d", &W, &H)){
44         if(W == 0 || H ==0) break;
45         for(int i = 1; i <= H; i++){
46             for(int j = 1; j <= W; j++){
47                 cin>>mp[i][j];
48                 if(mp[i][j] == ‘@‘){
49                     s.x = i;
50                     s.y = j;
51                     s.t = 0;
52                 }
53                 timE[i][j] = INF;
54             }
55         }
56         timE[s.x][s.y] = 0;
57         memset(vis, 0, sizeof(vis));
58         BFS(s);
59         int ans = 0;
60         for(int i = 1; i <= H; i ++){
61             for(int j = 1; j <= W; j++){
62                 if(timE[i][j] != INF)  ans++;
63             }
64         }
65         printf("%d\n", ans);
66     }
67
68     return 0;
69 }

hdu 1312 Red and Black(BFS水题),布布扣,bubuko.com

时间: 2024-12-25 15:03:58

hdu 1312 Red and Black(BFS水题)的相关文章

hdu 1312 Red and Black (bfs)

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 mo

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

HDU 1800 Flying to the Mars (水题)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11099    Accepted Submission(s): 3572 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u 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

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou