poj 1979DFS&&BFS
2014-08-16 19:45
323 查看
DescriptionThere 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 thenumbers 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)The end of the input is indicated by a line consisting of two zeros.汉语意思:一个有障碍的地图,给出一个起始点,可以走多少地图方块;@表示现在所在的位置,只能上下左右四个方向走,只能在‘.’这个上面走,#是障碍物输入:
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 06 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
输出:45 59 6 13
分析用的是深度搜索的知识
#include <iostream> #include <string.h> #define Size 30 using namespace std; char Map[Size][Size]; bool visited[Size][Size]; int W,H; int result; int sx,sy; int xmove[4]={1,0,-1,0}; int ymove[4]={0,1,0,-1}; int ok(int x,int y) { if(x>=0&&x<W&&y>=0&&y<H) return 1; return 0; } void DFS(int x,int y) { for(int i=0;i<4;i++) { int a=x+xmove[i]; int b=y+ymove[i]; if(ok(a,b)) { if(!visited[a][b]&&Map[a][b]=='.') { visited[a][b]=1; result++; DFS(a,b); } } } } int main() { while(cin>>H>>W) { if(!W&&!H) break; for(int i=0;i<W;i++) { for(int j=0;j<H;j++) { cin>>Map[i][j]; if(Map[i][j]=='@') { sx=i; sy=j; } } } result=1; memset(visited,0,sizeof(visited)); DFS(sx,sy); cout<<result<<endl; } return 0; }
BFS
<pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace std;#define maxn 25struct XPoint{int x, y;} a;bool map[maxn][maxn], vis[maxn][maxn];int n, m, ans;int dir[4][2] ={{ 0, 1 },{ 0, -1 },{ 1, 0 },{ -1, 0 } };void input(){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){char ch;scanf("%c", &ch);if (ch == '#')map[i][j] = false;elsemap[i][j] = true;if (ch == '@'){a.x = i;a.y = j;}}getchar();}}bool ok(XPoint &a){if (a.x < 0 || a.y < 0 || a.x >= n || a.y >= m)return false;return !vis[a.x][a.y] && map[a.x][a.y];}void bfs(){memset(vis, 0, sizeof(vis));queue<XPoint> q;q.push(a);vis[a.x][a.y] = true;ans = 0;while (!q.empty()){XPoint a = q.front();q.pop();ans++;XPoint b;for (int i = 0; i < 4; i++){b.x = a.x + dir[i][0];b.y = a.y + dir[i][1];if (ok(b)){q.push(b);vis[b.x][b.y] = true;}}}}int main(){//freopen("t.txt", "r", stdin);while (scanf("%d%d", &m, &n), m | n){getchar();input();bfs();printf("%d\n", ans);}return 0;}[/code]
相关文章推荐
- POJ 1979 dfs和bfs两种解法
- POJ 1979 Red and Black(BFS or DFS)
- POJ-1979&&HDU-1312--Red and Black---DFS深搜
- poj 1130 Alien Security (dfs+bfs<反向最短路>)
- POJ_S1E03_DFS&BFS(2)
- POJ 1573 BFS&DFS
- ACM: 方向dfs + bfs求最短路 poj 3…
- poj-1979 && hdoj - 1312 Red and Black (简单dfs)
- POJ_S1E03_DFS&BFS(3)
- poj 3984 迷宫问题【BFS && DFS】【简单】
- POJ 1979 简单dfs||bfs
- POJ 1979--DFS or BFS(水)
- (水)POJ-1979 Red and Black(dfs,bfs)
- POJ_S1E03_DFS&BFS(1)
- POJ 1979 基础搜索 DFS\BFS 一
- POJ 1979 && HDU 1312 Red and Black(DFS)
- POJ 1979 3669 DFS BFS
- poj 1979 &&hdu 1312Red and Black (bfs)
- HDU_4123 && POJ_4003 Bob’s Race (dfs / bfs + RMQ + 尺取)
- poj 1979 Red and Black(DFS||BFS)