[DFS/BFS]HOJ1797Red and Black

传送门:Red and Black

Red and Black

My Tags  (Edit)
  Source : Japan Domestic 2004
  Time limit : 1 sec   Memory limit : 32 M

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 HW 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


解题报告:

此题为典型的搜索题,搜索题有深度优先(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;
}
 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值