标签:des style blog java color strong
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0 此题中@也算一个可以走位置所以答案要加1,搜索时注意标记走过的点的位置以免重复计算 还有边界也要注意
#include<iostream> #include<cstring> using namespace std; int ans,n,m; int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; char a[1005][1005],b[1005][1005]; void dfs(int x,int y) { int xx,yy,i; for(i=0;i<4;i++) { xx=x+dx[i]; //此处注意不能直接用x+=d[x];否则回溯时不会回到x的初始值 yy=y+dy[i]; if(a[xx][yy]=='.'&&xx>=0&&xx<m&&yy>=0&&yy<n) { ans++; a[xx][yy]='#'; dfs(xx,yy); } } } int main() { int i,j; memset(b,0,sizeof(b)); while(cin>>n>>m,m&&n) { ans=0; for(i=0;i<m;i++) for(j=0;j<n;j++) cin>>a[i][j]; for(i=0;i<m;i++) for(j=0;j<n;j++) if(a[i][j]=='@') {a[i][j]='#';dfs(i,j);} /*cout<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout<<a[i][j]; cout<<endl; }*/ cout<<ans+1<<endl; } return 0; }
杭电 1312 red and black,布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/37358945