自顶
回复 9 楼 azzbcc
你是说 void fb(int **p,int m,int n)这段函数么?可是我++之前有一个判断if(p[i][j]==N) 了耶。。
#include <stdio.h> #include<stdlib.h> #include<time.h> #define N -1 void main() { void bl(int **p,int m,int n); void fb(int **p,int m,int n); void xs(int **p,int m,int n); int n1,n2,i,j; int **mine; printf("输入地雷阵行数:"); scanf("%d",&n1); printf("输入地雷阵列数:"); scanf("%d",&n2); mine=(int **)malloc(sizeof(int *)*n1); for(i=0;i<n1;i++) mine[i]=(int *)malloc(sizeof(int *)*n2); for(i=0;i<n1;i++) for(j=0;j<n2;j++) mine[i][j]=0; bl(mine,n1,n2); fb(mine,n1,n2); xs(mine,n1,n2); } void bl(int **p,int m,int n) { int ms,i,j,k; printf("输入“地雷”数:"); scanf("%d",&ms); srand(time(NULL)); for(k=0;k<ms;k++) { i=rand()%m; j=rand()%n; if(p[i][j]!=0) { k--; continue; } p[i][j]=N; } } void fb(int **p,int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) if(p[i][j]==N) { if(i==0&&j==0) { if(p[0][1]!=-1) p[0][1]++; if(p[1][0]!=-1) p[1][0]++; } if(i==n-1&&j==n-1) { if(p[n-1][n-2]!=-1) p[n-1][n-2]++; if(p[n-2][n-1]!=-1) p[n-2][n-1]++; } if(i==n-1&&j==0) { if(p[n-1][1]!=-1) p[n-1][1]++; if(p[n-2][0]!=-1) p[n-2][0]++; } if(j==n-1&&i==0) { if(p[1][n-1]!=-1) p[1][n-1]++; if(p[0][n-2]!=-1) p[0][n-2]++; } if(i==0&&j!=0&&j!=n-1) { if(p[i][j-1]!=-1) p[i][j-1]++; if(p[i][j+1]!=-1) p[i][j+1]++; if(p[i+1][j-1]!=-1) p[i+1][j-1]++; if(p[i+1][j]!=-1) p[i+1][j]++; if(p[i+1][j+1]!=-1) p[i+1][j+1]++; } if(i==n-1&&j!=n-1&&j!=0) { if(p[i-1][j-1]!=-1) p[i-1][j-1]++; if(p[i-1][j]!=-1) p[i-1][j]++; if(p[i-1][j+1]!=-1) p[i-1][j+1]++; if(p[i][j-1]!=-1) p[i][j-1]++; if(p[i][j+1]!=-1) p[i][j+1]++; } if(i!=0&&i!=n-1&&j==0) { if(p[i-1][j]!=-1) p[i-1][j]++; if(p[i-1][j+1]!=-1) p[i-1][j+1]++; if(p[i][j+1]!=-1) p[i][j+1]++; if(p[i+1][j]!=-1) p[i+1][j]++; if(p[i+1][j+1]!=-1) p[i+1][j+1]++; } if(i!=0&&i!=n-1&&j==n-1) { if(p[i-1][j-1]!=-1) p[i-1][j-1]++; if(p[i-1][j]!=-1) p[i-1][j]++; if(p[i][j-1]!=-1) p[i][j-1]++; if(p[i+1][j-1]!=-1) p[i+1][j-1]++; if(p[i+1][j]!=-1) p[i+1][j]++; } if(i!=0&&i!=n-1&&j!=n-1&&j!=0) { if(p[i-1][j-1]!=-1) p[i-1][j-1]++; if(p[i-1][j]!=-1) p[i-1][j]++; if(p[i-1][j+1]!=-1) p[i-1][j+1]++; if(p[i][j-1]!=-1) p[i][j-1]++; if(p[i][j+1]!=-1) p[i][j+1]++; if(p[i+1][j-1]!=-1) p[i+1][j-1]++; if(p[i+1][j]!=-1) p[i+1][j]++; if(p[i+1][j+1]!=-1) p[i+1][j+1]++; } } } } void xs(int **p,int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(p[i][j]==N) printf("%3c",'*'); else if(p[i][j]!=0) printf("%3d",p[i][j]); if(p[i][j]==0) printf("%3d",0); } printf("\n"); } }我懂了。。9楼你对的