右上角的数算的不对为什么呢
查看原帖
右上角的数算的不对为什么呢
272309
QODGOD楼主2021/3/24 21:38
#include<bits/stdc++.h>
using namespace std;
int n,m;
char map1[105][105];//存放原来的地图
int map2[105][105];//存放雷的个数 
void search(int i,int j){
	if(map1[i][j]=='*') map2[i][j]=-1;
	else{
		for(int x=-1;x<=1;x++){
			for(int y=-1;y<=1;y++){
				if(i+x>=0&&i+x<=m&&j+y>=0&&j+y<=n&&map1[i+x][i+y]=='*') map2[i][j]++;
			}
		}
	}
	
}
void p(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(map2[i][j]==-1) cout<<'*';
			else cout<<map2[i][j];
		}
		cout<<endl;
	}
}
int main(){
	memset(map2,0,sizeof(map2));
	cin>>n>>m; 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>map1[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			search(i,j);
		}
	}
	p();
}
2021/3/24 21:38
加载中...