DFS/BFS-A - Red and Black

A - Red and Black

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.

InputThe 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)
OutputFor 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 //并查集解决
 2 #include<iostream>
 3 using namespace std;
 4
 5 const int h = 22;
 6 char map[h][h];
 7 int  key[h*h];
 8 int rrank[h*h];
 9 int  n,m,dx,dy;
10
11 int find(int a){
12     return a==key[a]? a : key[a]=find(key[a]);
13 }
14
15 void key_union(int a,int c){
16     int fa = find(a);
17     int fc = find(c);
18     if(rrank[fa]>rrank[fc])
19         key[fc] = fa;
20     else{
21         key[fa] = fc;
22         if(rrank[fa]==rrank[fc])
23             rrank[fc]++;
24     }
25 }
26
27 int num(int a){
28     int k = find(a);
29     int ans = 0;
30     for(int i=1;i<=m;i++)
31         for(int j=1;j<=n;j++)
32             if(find(i*n+j)==k)
33                 ans++;
34
35     return ans;
36 }
37
38 int main()
39 {
40     while(scanf("%d %d",&n,&m)!=EOF){
41         if(n==0&&m==0)    break;
42         for(int i=1;i<=m;i++){
43             cin.get();
44             for(int j=1;j<=n;j++){
45                 scanf("%c",&map[i][j]);
46                 if(map[i][j]!=‘#‘)    key[i*n+j] = i*n+j;
47                 else                key[i*n+j] = 0;
48                 if(map[i][j]==‘@‘){//找到@的坐标
49                     dx = i;
50                     dy = j;
51                     map[i][j] = ‘.‘;
52                 }
53             }
54         }
55
56         for(int i=1;i<m;i++){
57             for(int j=1;j<n;j++){
58                 if(key[i*n+j]){
59                     if(key[i*n+j+1])
60                         key_union(i*n+j,i*n+j+1);
61                     if(key[i*n+n+j])
62                         key_union(i*n+n+j,i*n+j);
63                 }
64             }
65             if(key[i*n+n])
66                 if(key[i*n+2*n])
67                     key_union(i*n+2*n,i*n+n);
68         }
69         for(int i=1;i<n;i++)
70             if(key[m*n+i])
71                 if(key[m*n+i+1])
72                     key_union(m*n+i,m*n+i+1);
73
74         int ans = num(dx*n+dy);
75         printf("%d\n",ans);
76     }
77 }


 1 //DFS解决
 2 #include<iostream>
 3 using namespace std;
 4
 5 int mov[4][2] = {-1,0,1,0,0,-1,0,1};
 6 int sum,w,h;
 7 char s[21][21];
 8
 9 void dfs(int x,int y){
10     sum++;//计数
11     s[x][y] = ‘#‘;
12     for(int i=0;i<4;++i){//四个方向前进
13         int tx = x+mov[i][0];
14         int ty = y+mov[i][1];
15
16         if(s[tx][ty]==‘.‘ && tx>=0 && tx<h && ty>=0 && ty<w)
17             dfs(tx,ty);//判断该点可行后进入dfs
18     }
19 }
20
21 int main()
22 {
23     int x,y;
24     while(scanf("%d %d",&w,&h)!=EOF){
25         if(w==0&&h==0)    break;
26         for(int i=0;i<h;i++){
27             cin.get();
28             for(int j=0;j<w;j++){
29                 scanf("%c",&s[i][j]);
30                 if(s[i][j]==‘@‘){//起点
31                     x = i;
32                     y = j;
33                 }
34             }
35         }
36         sum = 0;
37         dfs(x,y);
38         printf("%d\n",sum);
39     }
40     return 0;
41 }
 1 //BFS解决
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4
 5 char room[23][23];
 6 int dir[4][2] = { //左上角的坐标是(0,0)
 7     {-1, 0},     //向左
 8     {0, -1},     //向上
 9     {1, 0},     //向右
10     {0, -1}        //向下
11 };
12
13 int Wx, Hy, num;
14 #define check(x, y)(x<Wx && x>=0 && y>=0 && y<Hy)    //是否在room中
15 struct node{int x, y};
16
17 void BFS(int dx, int dy){
18     num = 1;
19     queue<node> q;
20     node start, next;
21     start.x = dx;
22     start.y = dy;
23     q.push(start);//插入队列
24
25     while(!q.empty()){//直到队列为空
26         start = q.front();//取队首元素,即此轮循环的出发点
27         q.pop();//删除队首元素(以取出)
28
29         for(int i=0; i<4; i++){//往左上右下四个方向逐一搜索
30             next.x = start.x + dir[i][0];
31             next.y = start.y + dir[i][1];
32             if(check(next.x, next.y) && room[next.x][next.y]==‘.‘){
33                 room[next.x][next.y] = ‘#‘;//标记已经走过
34                 num ++;//计数
35                 q.push(next);//判断此点可行之后,插入队列,待循环判断
36             }
37         }
38     }
39 }
40
41 int main(){
42     int x, y, dx, dy;
43     while(~scanf("%d %d",&Wx, &Hy)){
44         if(Wx==0 && Hy==0)
45             break;
46         for(y=0; y<Hy; y++){
47             for(x=0; x<Wx; x++){
48                 scanf("%d",&room[x][y]);
49                 if(room[x][y] == ‘@‘){//找到起点坐标
50                     dx = x;
51                     dy = y;
52                 }
53             }
54         }
55         num = 0;//初始化
56         BFS(dx, dy);
57         printf("%d\n",num);
58     }
59     return 0;
60 }



原文地址:https://www.cnblogs.com/0424lrn/p/12230508.html

时间: 2024-10-05 09:33:58

DFS/BFS-A - Red and Black的相关文章

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

HDU 4771 (DFS+BFS)

Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know,

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因为广搜需要的就是队列,所以相比递归队列更耗内存? 当然DFS并不像上图所说,需要用栈,而是运用递归即可. BFS: 因为BFS是要一个接一个的遍历,所以用到了结构体,来保存坐标和当前所走步数 1.每走一步,通过定义的结构体,从队列中提取a(即上一步的坐标.步数(步数每次累加)) 2.在a的基础上进行

DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门 1 /* 2 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 3 在树上的路径权值都小于这两个点 4 DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案.因为排序后,他们之间的路径, 5 可定都是从当前节点u连过去的,那么都是小于这两个节点的.DFS需手动加栈,BFS类似拓扑排序的思路 6 */ 7 #pragma comment (linker, "/STACK:1024000000,10240000

Dfs/Bfs/记忆化搜索问题 | 问题集合

写在前面 动归和搜索似乎我打得特憋懒. 可能是因为搜索打的太少了??? 然后之前做过的一些题我就不再写了,比如填涂颜色/海战啥的? 然后每一题打两种解法(:Dfs/Bfs 前提是在题目里两种都能A P1596 湖计数 题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <=

【dfs/bfs+set+快速幂】swjtuOJ 2094

[dfs/bfs+set+快速幂]swjtuOJ 2094 [注:交大的看到这篇文章要学会自己写,不要为了比赛而比赛!~] 题目大意 问题一:主人公去度假,问经过a^b天后是星期几(简单题) 问题二:一个天平,n个重物,每个物体的重量wi已知,问能称出的所有重量有多少种? 问题二要注意到天平两侧都可以放重物,每一个重物的权值都可以赋值为w,0,-w,相当于三分,我们知道二分可以用二进制位运算进行枚举,例如:枚举所有子集 int j,k,top=0; int t = 1 << n; for(in