三个题基本上就是一样的思路,学到了怎么遍历四个方向和八个方向
count初始化 poj1562
#include<stdio.h> #include<string.h> int w,h; char a[105][105]; void mi_gong(int x,int y) { a[x][y]='*'; int tempx; int tempy; for(int k=-1;k<=1;k++) { for(int p=-1;p<=1;p++) { tempx=x+k; tempy=y+p; if(tempx>=0&&tempy>=0&&tempx<h&&tempy<w&&a[tempx][tempy]=='@') mi_gong(tempx,tempy); } } return ; } int main() { while(scanf("%d%d",&h,&w),h||w) { int count=0; //memset(a,'*',sizeof(a)); getchar(); for(int i=0;i<h;i++) { scanf("%s",a[i]); getchar(); } for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { if(a[i][j]=='@') { mi_gong(i,j); count++; } } } printf("%d\n",count); } return 0; }
poj2386 #include<stdio.h> int w,h; char a[105][105]; void mi_gong(int x,int y) { a[x][y]='.'; int tempx; int tempy; for(int k=-1;k<=1;k++) { for(int p=-1;p<=1;p++) { tempx=x+k; tempy=y+p; if(tempx>=0&&tempy>=0&&tempx<h&&tempy<w&&a[tempx][tempy]=='W') mi_gong(tempx,tempy); } } return ; } int main() { int count=0; scanf("%d%d",&h,&w); getchar(); for(int i=0;i<h;i++) { scanf("%s",a[i]); getchar(); } for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { if(a[i][j]=='W') { mi_gong(i,j); count++; } } } printf("%d",count); return 0; }
poj1979 #include<stdio.h> #include<iostream> using namespace std; int w,h; char a[21][21]; int mi_gong(int x,int y) { if(x<0||x>=h||y<0||y>=w) return 0; if(a[x][y]=='#') return 0; a[x][y]='#'; return (1+mi_gong(x-1,y)+mi_gong(x,y-1)+mi_gong(x+1,y)+mi_gong(x,y+1)); } int main() { int kw,kh; while(scanf("%d%d",&w,&h),w||h) { getchar(); for(int i=0;i<h;i++) { scanf("%s",a[i]); getchar(); } for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { if(a[i][j]=='@') { printf("%d\n",mi_gong(i,j)); break; } } } } return 0; }
poj 2386 poj1562 poj1979 图的遍历 八个方向和四个方向 深搜
最新推荐文章于 2021-01-13 11:20:59 发布