Oil Deposits
Oil Deposits |
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise and . Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either ` *', representing the absence of oil, or ` @', representing an oil pocket.Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2 感觉挺简单的题目思路一开始就错了,开始想着每次遇到‘@’就八个方向搜索,其余的‘@’,然后用个标记数组标记遇到过的‘@’,然后计数的时候,每遇到一个‘@’就判断,他周围8个有没有‘@’并且已经标记的没有则,数量+1;很多数据都对......提交无限wrong。。。。
可能计算数量的方法出错了,找不到反例而已把wrong的放着以后再解决。
#include<stdio.h> #include<string.h> char map[101][101],use[101][101]; int sum,n,m,move[8][2]={1,0,0,1,-1,0,0,-1,1,-1,-1,1,1,1,-1,-1}; int ok(int x,int y) {int X,Y,i; for (i=0;i<8;i++) {X=x+move[i][0]; Y=y+move[i][1]; if ((X>=0)&&(X<n)&&(Y>=0)&&(Y<m)&&(map[X][Y]=='@')&&(use[X][Y]==0)) return 0; } return 1; }; int sort(int step) {int X,Y,i,x,y,k; if (step>=m*n) return 0; y=step % m; x=step / m; //if (use[x][y]) printf("%d %d\n",x,y); if ((map[x][y]=='@')&&(use[x][y]==1)) {use[x][y]=0; //sum=sum+ok(x,y); for (i=0;i<8;i++) {X=x+move[i][0]; Y=y+move[i][1]; if ((X>=0)&&(X<n)&&(Y>=0)&&(Y<m)&&(map[X][Y]=='@')&&(use[X][Y]==1)) { sort(X*m+Y); } }
sort(step+1); } else {use[x][y]=0; sort(step+1); } return 0; }; int main() {int i,j; while (scanf("%d%d",&n,&m)) {if (n+m==0) break; getchar(); for (i=0;i<n;i++) gets(map[i]); for (i=0;i<n;i++) for (j=0;j<m;j++) use[i][j]=1; sum=0; sort(0);
printf("%d\n",sum); } return 0; }
换了一种计算思路,每次遇到‘@’就+1,然后把相邻的‘@’改成‘*’;
简单很多代码也短AC,思路一错,全是徒劳,挺简单的题做了一下午。
#include<stdio.h> #include<string.h> char map[101][101]; int sum,n,m,move[8][2]={1,0,0,1,-1,0,0,-1,1,-1,-1,1,1,1,-1,-1}; void sort(int x,int y) {int X,Y,i; map[x][y]='*'; for (i=0;i<8;i++) {X=x+move[i][0]; Y=y+move[i][1]; if ((X>=0)&&(X<n)&&(Y>=0)&&(Y<m)&&(map[X][Y]=='@')) sort(X,Y); } }; int main() {int i,j; while (scanf("%d%d",&n,&m)) {if (n+m==0) break; getchar(); for (i=0;i<n;i++) gets(map[i]); sum=0; for (i=0;i<n;i++) for (j=0;j<m;j++) if (map[i][j]=='@') {++sum; sort(i,j);} printf("%d\n",sum); } return 0; }