poj 1979DFS&&BFS

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) 
The end of the input is indicated by a line consisting of two zeros.

 汉语意思:一个有障碍的地图,给出一个起始点,可以走多少地图方块;@表示现在所在的位置,只能上下左右四个方向走,只能在‘.’这个上面走,#是障碍物

输入:

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
6 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 25

struct 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;
            else
                map[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;
}




 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值