43 扫雷
作者: Turbo时间限制: 1S章节: 基本练习(数组)
问题描述 :
扫雷游戏你一定玩过吧!现在给你若干个n×m的地雷阵,请你计算出每个矩阵中每个单元格相邻单元格内地雷的个数,每个单元格最多有8个相邻的单元格。 0<n,m<=100
输入说明 :
输入包含若干个矩阵,对于每个矩阵,第一行包含两个整数n和m(0<n,m<=100),分别表示这个矩阵的行数和列数。接下来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
代码:
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include <sstream>
using namespace std;
static int number=1;
int main()
{
int row,col;
while(cin>>row>>col)
{
if(row==0&&col==0)
break;
int count=0;
char table[row][col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>table[i][j];
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(table[i][j]=='*')
continue;
int time=0;
if(i==0)
{
if(j==0)
{
for(int m=i;m<=i+1;m++)
{
for(int n=j;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else if(j==col-1)
{
for(int m=i;m<=i+1;m++)
{
for(int n=j-1;n<col;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else
{
for(int m=i;m<=i+1;m++)
{
for(int n=j-1;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
}
else if(i==row-1)
{
if(j==0)
{
for(int m=i-1;m<=row;m++)
{
for(int n=j;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else if(j==col-1)
{
for(int m=i-1;m<=row;m++)
{
for(int n=j-1;n<col;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else
{
for(int m=i-1;m<=row;m++)
{
for(int n=j-1;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
}
else if(j==0)
{
for(int m=i-1;m<=i+1;m++)
{
for(int n=j;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else if(j==col-1)
{
for(int m=i-1;m<=i+1;m++)
{
for(int n=j-1;n<=j;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
else
{
for(int m=i-1;m<=i+1;m++)
{
for(int n=j-1;n<=j+1;n++)
{
if(table[m][n]=='*')
time++;
}
}
}
table[i][j]=time+'0';
}
}
cout<<"Field #"<<number<<":"<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<table[i][j];
}
cout<<endl;
}
number++;
cout<<endl;
}
return 0;
}
方法有点笨,而且oj有一个例子过不了,求指正。