~cpp
// no10189 - Minesweeper(a)
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100;
bool inMine(vector<int> & mine, int & nField, int * size);
void mineSweep(vector<int> & mine, int & nField, int * size);
int main()
{
vector<int> mine; // 입력을 저장할 벡터
int size[MAX]; // 행과 열만을 저장할 배열
int nField = 0; // 지뢰 게임 수
if(!inMine(mine, nField, size))
return 0;
mineSweep(mine, nField, size);
return 0;
}
bool inMine(vector<int> & mine, int & nField, int * size)
{
char temp;
int i, n, m;
int count = 0;
while(cin >> n >> m) // 입력
{
if (n == 0 && m == 0)
break;
size[count++] = n;
size[count++] = m;
for (i=0; i<n*m; i++)
{
cin >> temp;
if (temp == '*')
mine.push_back(1); // * -> 1
else if (temp == '.')
mine.push_back(0); // . -> 0
else
{
cout << "Wrong Input!\n";
return false;
}
}
nField++;
}
return true;
}
void mineSweep(vector<int> & mine, int & nField, int * size)
{
int mineSweep[MAX + 2][MAX + 2]; // 출력 할 지뢰밭 start point (1,1)
vector<int>::iterator pr = mine.begin();
int row, col; // 행,열
int count = 0;
int i, j, k, p, q;
bool first = true;
for(i=0; i<nField; i++)
{
if (!first)
cout << endl;
row = size[count++];
col = size[count++];
for (j=1; j<row+1; j++)
{
for (k=1; k<col+1; k++)
mineSweep[j][k] = 0;
}
for (j=1; j<row+1; j++)
{
for (k=1; k<col+1; k++)
{
if (*pr) // 지뢰라면
{
for (p=j-1; p<=j+1; p++)
for (q=k-1; q<=k+1; q++)
mineSweep[p][q]++;
mineSweep[j][k] = -9;
}
pr++;
}
}
cout << "Field #" << i+1 << ":\n";
for (j=1; j<row+1; j++)
{
for (k=1; k<col+1; k++)
{
if (mineSweep[j][k] < 0)
cout << "*";
else
cout << mineSweep[j][k];
}
cout << endl;
}
first = false;
}
}