- poj2386 Lake Counting 简单题
#include<iostream> #include<cstring> using namespace std; int n,m; char map[101][101]; int dir[8][2] = { {1,0},{1,1},{1,-1},{0,1},{0,-1},{-1,0},{-1,-1},{-1,1} }; bool vis[101][101]; void dfs(int x,int y) { int nx,ny; vis[x][y]=1; for(int k=0;k<8;k++) { nx=x+dir[k][0];ny=y+dir[k][1]; if( nx>=n || nx<0 || ny>=m || ny<0 || vis[nx][ny] ) continue; if(map[nx][ny] == 'W') dfs(nx,ny); } } int main(){ cin>>n>>m; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) cin>>map[i]; //getchar(); int cnt=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(vis[i][j]) continue; if(map[i][j] == 'W'){ dfs(i,j); cnt++; } } } cout<<cnt<<endl; return 0; } /* 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W........W. ..W...W...W. */
- poj1979 Red and Black 简单题
/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<cstdio> #include<set> #include<queue> using namespace std; int w,h, mx,my,cnt; char map[21][21]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; bool vis[21][21]; bool can_reach(int x,int y){ return (x<h&&x>=0&&y<w&&y>=0&&map[x][y]!='#'&&!vis[x][y]); } int dfs(int x,int y){ int nx,ny; cnt++; vis[x][y]=1; for(int i=0;i<4;i++) { nx=x+dir[i][0];ny=y+dir[i][1]; if(can_reach(nx,ny)) { dfs(nx,ny); } } } int main(){ // freopen("a.txt","r",stdin); // freopen(".out","w",stdout); while(cin>>w>>h){ if(!(w|h)) break; getchar(); for(int i=0;i<h;i++) gets(map[i]); for(int i=0;i<h;i++) for(int j=0;j<w;j++) if(map[i][j]=='@') {mx=i;my=j;} cnt=0; memset(vis,0,sizeof(vis)); dfs(mx,my); cout<<cnt<<endl; } return 0; } /* DESCRIPTION: */
- poj3009 curling2.0(DFS模拟题)
/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<cstdio> #include<set> #include<queue> using namespace std; int w,h; int map[21][21]; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int sx,sy,ML; bool vis[21][21]; bool border(int x,int y){ return (x>=h || x<0 || y<0 ||y>=w); } void dfs(int x,int y,int mve){ if(mve>10||mve>ML)//如果已经超出预期,则不可到达 return; for(int i=0;i<4;i++){ int nx=x+dir[i][0];int ny=y+dir[i][1]; if( border(nx,ny) || map[nx][ny]==1 ) continue;////////////// while(1){ if(border(nx,ny)) break; if(!map[nx][ny]){ nx+=dir[i][0];ny+=dir[i][1]; continue; } if(map[nx][ny]==1) { map[nx][ny]=0; dfs(nx-dir[i][0],ny-dir[i][1],mve+1); map[nx][ny]=1; break; } if(map[nx][ny]==3) { ML=min(ML,mve+1); return; } } } } int main(){ // freopen("a.txt","r",stdin); // freopen(".out","w",stdout); while(cin>>w>>h){ if(!(w|h)) break; memset(map,0,sizeof(map)); for(int i=0;i<h;i++) for(int j=0;j<w;j++){ scanf("%d",&map[i][j]); if(map[i][j]==2){ sx=i;sy=j; } } ML=11;/// map[sx][sy]=0; dfs(sx,sy,0); if( ML<=10) cout<<ML<<endl; else cout<<-1<<endl; } return 0; } /* DESCRIPTION: 快被这题整哭了。。。。以后不敢再随便设公有变量了FUCK!!! */