Skip to content

Instantly share code, notes, and snippets.

@mohamed-ennahdi
Created February 24, 2014 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamed-ennahdi/9181752 to your computer and use it in GitHub Desktop.
Save mohamed-ennahdi/9181752 to your computer and use it in GitHub Desktop.
Mine Sweeper Contest Solution.
#include<stdio.h>
/*
* M is the maximum size that a line can take.
*/
#define M 100
/*
* N is the maximum size that a column can take.
*/
#define N 100
int main(){
/*
* m is the number of lines that is going to be provided by the user;
* n is the number of columns that is going to be provided by the user;
* i allows to parse field and backStageField lines;
* j allows to parse field and backStageField columns.
*/
int m, n, i, j, x = 1;
/*
* field is the structure where safe squares and mines are specified by
* the user;
*/
char field[N][M];
/*
* backStageField is the structure where the processing outcome is stored
* and displayed.
*/
int backStageField[N][M];
scanf("%d %d", &m, &n);
while(m != 0 && n != 0){
/*
* O(M)
* Reading values, line by line (string).
*/
for (i = 0; i < m; i++) {
scanf("%s", field[i]);
}
/*
* O(N * M)
* Initializing backStageField.
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
backStageField[i][j] = 0;
}
}
/*
* O(N * M)
* Parsing field and updating backStageField's squares when applicable.
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (field[i][j] == '*') {
if (i - 1 >= 0 && j - 1 >= 0) {
backStageField[i - 1][j - 1]++;
}
if (i < m && j - 1 < n) {
backStageField[i][j - 1]++;
}
if (i + 1 >= 0 && j - 1 >= 0) {
backStageField[i + 1][j - 1]++;
}
if (i - 1 >= 0 && j >= 0) {
backStageField[i - 1][j]++;
}
if (i + 1 < m && j < n) {
backStageField[i + 1][j]++;
}
if (i - 1 >= 0 && j + 1 >= 0) {
backStageField[i - 1][j + 1]++;
}
if (i >= 0 && j + 1 >= 0) {
backStageField[i][j + 1]++;
}
if (i + 1 < m && j + 1 < n) {
backStageField[i + 1][j + 1]++;
}
}
}
}
/*
* O(N * M)
* Displaying backStageField values.
*/
printf("\nField #%d:\n", x);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if(field[i][j] == '*')
putchar('*');
else
printf("%d", backStageField[i][j]);
}
printf("\n");
}
x++;
scanf("%d %d", &m, &n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment