HDU 1312 Red and Black

来源:互联网 发布:中文字符串相似度算法 编辑:程序博客网 时间:2024/05/21 17:37

还是水题一道 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 0