试题 算法提高 扫雷
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相邻的单元格。 0<n,m<=100
输入格式
输入包含若干个矩阵,对于每个矩阵,第一行包含两个整数n和m,分别表示这个矩阵的行数和列数。接下来n行每行包含m个字符。安全区域用‘.’表示,有地雷区域用'*'表示。当n=m=0时输入结束。
输出格式
对于第i个矩阵,首先在单独的一行里打印序号:“Field #i:”,接下来的n行中,读入的'.'应被该位置周围的地雷数所代替。输出的每两个矩阵必须用一个空行隔开。
样例输入
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
样例输出
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
(注意两个矩阵之间应该有一个空行,由于oj的格式化这里不能显示出来)
数据规模和约定
0<n,m<=100
解题思路:模拟。这里重点强调的细节有:1、int 转char,ex: char c=8+'0'; 2、将更新地雷后的二维数组 遍历 转换成 字符串 存放到结果向量中,最后输出。
AC代码如下:
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
struct Eg{
int row,line;
vector<char> res;
Eg(){ }
};
vector<Eg> vkt;
int n,m;
int G[110][110];//0:安全,-1:地雷
void update(int x,int y){
//遍历(x,y)周围的点
int bomb=0;// 周围的地雷数
if(x-1>=1 && y-1>=1 && G[x-1][y-1]==-1)
bomb++;
if(x-1>=1 && G[x-1][y]==-1)
bomb++;
if(x-1>=1 && y+1<=m && G[x-1][y+1]==-1)
bomb++;
if(y+1<=m && G[x][y+1]==-1)
bomb++;
if(x+1<=n && y+1<=m && G[x+1][y+1]==-1)
bomb++;
if(x+1<=n && G[x+1][y]==-1)
bomb++;
if(x+1<=n && y-1>=1 && G[x+1][y-1]==-1)
bomb++;
if(y-1>=1 && G[x][y-1]==-1)
bomb++;
G[x][y]=bomb;
}
void FindBomb(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(G[i][j]!=-1)
update(i,j);
}
}
}
int main(int argc, char** argv) {
while(1){
cin>>n>>m;
if(n==0 && m==0)
break;
memset(G,0,sizeof(G));
char c;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c;
if(c=='.')
G[i][j]=0;
else
G[i][j]=-1;
}
}
FindBomb();
Eg E;
E.line=n;
E.row=m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(G[i][j]==-1){
E.res.push_back('*');
}
else{
char tmp=G[i][j]+'0';
E.res.push_back(tmp);
}
}
}
vkt.push_back(E);
}
for(int i=0;i<vkt.size();i++){
cout<<"Field #"<<i+1<<":"<<endl;
for(int j=0;j<vkt[i].res.size();j++){
cout<<vkt[i].res[j];
if((j+1)%vkt[i].row==0)
cout<<endl;
}
cout<<endl;
}
return 0;
}