题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
-----------------------------------------------------------------------------------
题意:根据地图,问可以到达多少地方.‘@‘为起点, ‘ . ‘为路,可以到达, ‘ # ‘为墙,不能通过.
思路:DFS,按照一定方式搜索标记。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int MAXN =20+5; int H; int W; char road[MAXN][MAXN]; int flag[MAXN][MAXN]; int ans =0; void dfs(int i,int j){ if(road[i][j+1]==‘.‘&&flag[i][j+1]==0){ ans++; flag[i][j+1]=1; dfs(i,j+1); } if(road[i][j-1]==‘.‘&&flag[i][j-1]==0){ ans++; flag[i][j-1]=1; dfs(i,j-1); } if(road[i+1][j]==‘.‘&&flag[i+1][j]==0){ ans++; flag[i+1][j]=1; dfs(i+1,j); } if(road[i-1][j]==‘.‘&&flag[i-1][j]==0){ ans++; flag[i-1][j]=1; dfs(i-1,j); } } int main(void){ while(~scanf("%d %d",&W,&H)&&(W!=0&&H!=0)){ ans= 0; for(int i=0;i<MAXN;i++){ for(int j=0;j<MAXN;j++){ road[i][j]=‘#‘; flag[i][j]=0; } } for(int i=0;i<H;i++){ scanf("%s",&road[i]); //printf("road:%s\n",road[i]); } int inx =0; int iny =0; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(road[i][j]==‘@‘){ ans++; inx =i; iny =j; flag[i][j]=1; //printf("%d %d\n",inx,iny); } } } dfs(inx,iny); printf("%d\n",ans); } return 0; }