Thursday, September 27, 2007

Minesweeper

ACCEPTED at last after a long stand with a presentation error!!!
____________________________________________________________________
Question: Minesweeper
____________________________________________________________________

#include "iostream.h"

int arr[102][102];

void increment(int i, int j) {
if(arr[i][j] != -1) arr[i][j]++;
}

int main()
{
int n, m;
char c;

int count = 1;
while(true) {
cin >> n >> m;
if(n == 0 && m == 0) break;
if(count > 1)
cout << "\n";
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) arr[i][j] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> c;
if( c == '*') {
arr[i][j] = -1;
increment(i, j + 1);
increment(i, j - 1);
increment(i + 1, j);
increment(i + 1, j + 1);
increment(i + 1, j - 1);
increment(i - 1, j);
increment(i - 1, j + 1);
increment(i - 1, j - 1);
}
}
}

cout << "Field #" << count++ << ":" << endl;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(arr[i][j] == -1) cout << '*';
else cout << arr[i][j];
}
cout << endl;
}
}
return 0;
}
____________________________________________________________________

No comments:

Contributors