Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:水题1A,DFS就行了。In Reply To:水题1A,DFS就行了。 Posted by:1196494730 at 2018-10-19 16:43:42 #include<iostream> #include<cstring> #define N 29 using namespace std; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n,m,sx,sy,vst[N][N];char d[N][N]; int dfs(int x,int y) { vst[x][y]=1; int ret=1; for (int k=0;k<4;++k) { int nx=x+dx[k],ny=y+dy[k]; if (nx<1||nx>n||ny<1||ny>m) continue; if (vst[nx][ny]||d[nx][ny]!='.') continue; ret+=dfs(nx,ny); } return ret; } int main() { while (cin>>m>>n) { if (n==0&&m==0) return 0; memset(vst,0,sizeof vst); for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) { cin>>d[i][j]; if (d[i][j]=='@') sx=i,sy=j; } cout<<dfs(sx,sy)<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator