[DFS/BFS]HOJ1797Red and Black
来源:互联网 发布:关于数据的例子 编辑:程序博客网 时间:2024/06/07 05:45
传送门:Red and Black
Red and Black
Submitted : 834, Accepted : 550
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 0Sample Output
4559613
解题报告:
此题为典型的搜索题,搜索题有深度优先(DFS)和广度优先(BFS)两种。
DFS递归。很显然,当找到起始状态后,直接向四个方向递归即可。思路很简单代码如下:
#include<stdio.h>#include<string.h>char s[30][30];int maps[30][30],h,w,sum;void dfs(int i,int j){ sum++; maps[i][j]=1; if(i+1<w&&maps[i+1][j]==0&&s[i+1][j]=='.') dfs(i+1,j); if(j+1<h&&maps[i][j+1]==0&&s[i][j+1]=='.') dfs(i,j+1); if(i-1>=0&&maps[i-1][j]==0&&s[i-1][j]=='.') dfs(i-1,j); if(j-1>=0&&maps[i][j-1]==0&&s[i][j-1]=='.') dfs(i,j-1);}int main(){ int i,j; while(scanf("%d%d",&h,&w)==2&&(h||w)){ memset(maps,0,sizeof(maps)); sum=0; for(i=0;i<w;i++) scanf("%s",s[i]); for(i=0;i<w;i++) for(j=0;j<h;j++) if(s[i][j]=='@') dfs(i,j); printf("%d\n",sum); } return 0;}
DFS非递归
因为DFS可以使用栈(stack)实现。所以实现起来就简单了。
#include<iostream>#include<stack>#include<cstdio>using namespace std;struct node{ int x,y;}st;char s[30][30];int maps[30][30];int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int dfs(node st,int h,int w){ stack<node> q; int i,cnt=0; node a,b; while(!q.empty()){ q.pop(); } q.push(st); maps[st.x][st.y]=1; cnt++; while(!q.empty()){ a=q.top(); q.pop(); for(i=0;i<4;i++){ b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x<0||b.y<0||b.x>w-1||b.y>h-1) continue; if(maps[b.x][b.y]==0){ q.push(b); maps[b.x][b.y]=1; cnt++; } } } return cnt;}int main(){ int h,w,i,j; while(scanf("%d%d",&h,&w)==2&&(h||w)){ for(i=0;i<w;i++) scanf("%s",s[i]); for(i=0;i<w;i++) for(j=0;j<h;j++){ if(s[i][j]=='.') maps[i][j]=0; else if(s[i][j]=='#') maps[i][j]=1; else{ maps[i][j]==0; st.x=i; st.y=j; } } printf("%d\n",dfs(st,h,w)); } return 0;}
BFS
因为BFS可以使用队列(queue)实现。所以代码与DFS非递归相似。
#include<iostream>#include<queue>#include<cstdio>using namespace std;struct node{ int x,y;}st;char s[30][30];int maps[30][30];int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int bfs(node st,int h,int w){ queue<node> q; int i,cnt=0; node a,b; while(!q.empty()){ q.pop(); } q.push(st); maps[st.x][st.y]=1; cnt++; while(!q.empty()){ a=q.front(); q.pop(); for(i=0;i<4;i++){ b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x<0||b.y<0||b.x>w-1||b.y>h-1) continue; if(maps[b.x][b.y]==0){ q.push(b); maps[b.x][b.y]=1; cnt++; } } } return cnt;}int main(){ int h,w,i,j; while(scanf("%d%d",&h,&w)==2&&(h||w)){ for(i=0;i<w;i++) scanf("%s",s[i]); for(i=0;i<w;i++) for(j=0;j<h;j++){ if(s[i][j]=='.') maps[i][j]=0; else if(s[i][j]=='#') maps[i][j]=1; else{ maps[i][j]==0; st.x=i; st.y=j; } } printf("%d\n",bfs(st,h,w)); } return 0;}
- [DFS/BFS]HOJ1797Red and Black
- poj 1979Red and Black(BFS DFS)
- HDU 1312 Red and Black(BFS,DFS)
- Red and Black(BFS or DFS)
- HDU1312 Red and Black (DFS || BFS)
- hdu 1312 Red and Black (dfs+bfs)
- 【HDU 1312 Red and Black】+ DFS + BFS
- 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(BFS or DFS)
- (水)POJ-1979 Red and Black(dfs,bfs)
- HDU 1312 Red and Black(BFS,DFS)
- (BFS/DFS)POJ 1979 Red and Black
- 【hdu 1312】 Red and Black (dfs+bfs)
- HDOJ 1312 Red and Black(DFS,BFS)
- HDU1312_Red and Black(BFS)
- SQL Server:安全设计从头起
- Python中有‘self’这个关键字么
- leetcode Path Sum 1.2 难度系数1
- How to install Slackware
- 微软RPC官方教程
- [DFS/BFS]HOJ1797Red and Black
- lua5.2参考手册已经翻译好了
- 在Python的入门路上(答篇)……
- leetcode Two Sum 2.1 难度系数2
- HOW I MADE $2,000,000 IN THE STOCK MARKET
- 【stm32】实现STM32的串口数据发送和printf函数重定向
- 6.linux的文件权限与目录配置
- Java String.Format() 方法及参数 详细说明 总结篇
- 下一步,怎么走——Start Developing iOS Apps Today——从今天开始开发IOS(IOS7版)系列源文档翻译(二十三)