Red and Black(BFS or DFS)
来源:互联网 发布:会计软件单机版 编辑:程序博客网 时间:2024/06/07 00:48
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
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.
…@…
.
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
BFS
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int w,h,sx,sy,cnt;char map[30][30];int vis[30][30];int dx[]={0,0,-1,1};int dy[]={1,-1,0,0};struct Node{ int x; int y;}Q[450];Node s;void bfs(){ int front=0,rear=0; Q[rear++]=s; while(front<rear) { Node t=Q[front++]; for(int i=0;i<4;i++) { int x0=t.x+dx[i]; int y0=t.y+dy[i]; Node f; f.x=x0;f.y=y0; if(!vis[f.x][f.y]&&f.x>=0&&f.x<h&&f.y>=0&&f.y<w&&map[f.x][f.y]!='#') { vis[f.x][f.y]=1; Q[rear++]=f; if(map[f.x][f.y]=='.') cnt++; } } }}int main(){ while(~scanf("%d%d",&w,&h)) {if(w==0||h==0) break; memset(vis,0,sizeof(vis)); memset(map,0,sizeof(map)); for(int i=0;i<h;i++) { scanf("%s",map[i]); for(int j=0;j<w;j++) if(map[i][j]=='@') { s.x=i; s.y=j; break; } } cnt=0; bfs(); printf("%d\n",cnt+1);} return 0;}
DFS
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int w,h,sx,sy,cnt;char map[30][30];int vis[30][30];int dx[]={0,0,-1,1};int dy[]={1,-1,0,0};void dfs(int x,int y){ if(map[x][y]=='.') cnt++; if(x<0||x>=h||y<0||y>=w||map[x][y]=='#') return; for(int i=0;i<4;i++) { int x0=x+dx[i]; int y0=y+dy[i]; if(!vis[x0][y0]) { vis[x0][y0]=1; dfs(x0,y0); } }}int main(){ while(~scanf("%d%d",&w,&h)) {if(w==0||h==0) break; memset(vis,0,sizeof(vis)); memset(map,0,sizeof(map)); for(int i=0;i<h;i++) { scanf("%s",map[i]); for(int j=0;j<w;j++) if(map[i][j]=='@') { sx=i; sy=j; break; } } cnt=0; dfs(sx,sy); printf("%d\n",cnt+1);} return 0;}//6 9//....#.//.....#//......//......//......//......//......//#@...#//.#..#.
可以看出:写BFS时一般要有结构体来表示状态。
求最短路一般用BFS,其他的可能更多用的是DFS
两者的关键都在于找转态。
- Red and Black(BFS or DFS)
- POJ 1979 Red and Black(BFS or DFS)
- poj 1979Red and Black(BFS DFS)
- HDU 1312 Red and Black(BFS,DFS)
- HDU1312 Red and Black (DFS || BFS)
- hdu 1312 Red and Black (dfs+bfs)
- 【HDU 1312 Red and Black】+ DFS + BFS
- Red and Black(DFS)
- Red and Black(DFS)
- Red and Black(DFS)
- dfs Red and Black
- 【DFS】RED AND BLACK
- Red and Black(DFS)
- hdu1312 Red and Black(DFS/BFS入门题目)
- HDU 1312Red and Black(简单搜索 bfs或dfs)
- poj 1979 Red and Black(DFS||BFS)
- HDU 1312 Red and Black-dfs&bfs-(分块)
- (水)POJ-1979 Red and Black(dfs,bfs)
- 上课笔记--商法(下)
- 微信JSAPI支付(JAVA版)
- 2015070507 - EffactiveJava笔记 - 第13条 使类和成员的可访问性最小(1)
- 黑马程序员——11_File 临时文件创建注意
- JAVA面向对象第三天
- Red and Black(BFS or DFS)
- 神经网络总结(初稿)
- 算法与数据结构八日谈之六——数据结构专题(uncompleted)
- LeetCode 219: Contains Duplicate II
- 黑马程序员——12_File_deleteOnExit()试验
- Longest Substring Without Repeating Characters
- IOS菜鸟的所感所思(十六)—— 立体式的切换视图
- C语言-解析命令行参数
- 二叉树相关算法总结