HDU 1312 Red and Black

还是水题一道 http://acm.hdu.edu.cn/showproblem.php?pid=1312

就是求可以到达的点,移动方向为上下左右四个方向,要求是‘.’为可到达的,'#'不可越过,先找到'@'起始位置,然后就可以DFS了

其实和1241差不多,甚至还要简单一点...

没什么难度,入门级搜索

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int m,n,cnt,a,b;
char map[22][22];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y)
{
    int fx,fy;
    for(int i=0;i<4;i++)
    {
        fx=x+dir[i][0];
        fy=y+dir[i][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='.')
        {
            cnt++;
            map[fx][fy]='#';
            dfs(fx,fy);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        cnt=1;
        if(n==0&&m==0)
            break;
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='@')
                {
                    a=i;
                    b=j;
                }
            }
            getchar();
        }
        map[a][b]='#';
        dfs(a,b);
        printf("%d\n",cnt);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值