hdu 1312 Red and Black

来源:互联网 发布:embase数据库 编辑:程序博客网 时间:2024/05/16 18:20

hdu  1312  Red and Black                 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

DFS水

题目大意:一个人被困在一个迷宫里,问他的生存空间。

题目分析:DFS就好了。

code:

#include<cstdio>#include<cstring>char mp[26][26],vis[26][26];int m,n,dir[4][2]={1,0,-1,0,0,1,0,-1},sum;int judge(int x,int y){return x>=0&&y>=0&&x<m&&y<n&&mp[x][y]=='.'?1:0;}void dfs(int x,int y){mp[x][y]='#';sum++;for(int i=0;i<4;i++){int xx=x+dir[i][0],yy=y+dir[i][1];if(judge(xx,yy))dfs(xx,yy);}}int main(){int i,j,sx,sy;while(scanf("%d%d",&n,&m)!=EOF&m|n){sum=0;for(i=0;i<m;i++){scanf("%s",mp[i]);for(j=0;j<n;j++){if(mp[i][j]=='@')sx=i,sy=j;}}dfs(sx,sy);printf("%d\n",sum);}return 0;}

15MS  232K  603B

PS:无tricker水





原创粉丝点击