Last active
August 29, 2015 14:01
-
-
Save evansb/e06bda0f72d5169d754a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Minesweeper.cc | |
// Evan Sebastian <evanlhoini@gmail.com> | |
#include <cstdio> | |
#include <vector> | |
using namespace std; | |
int count9(vector<vector<char> >& mine, int i, int j) { | |
int c = 0; | |
int w = (int)mine[0].size(); | |
int h = (int)mine.size(); | |
if (i - 1 >= 0 && mine[i - 1][j] == '*') c++; | |
if (i - 1 >= 0 && j - 1 >= 0 && mine[i - 1][j - 1] == '*') c++; | |
if (i - 1 >= 0 && j + 1 < w && mine[i - 1][j + 1] == '*') c++; | |
if (i + 1 < h && mine[i + 1][j] == '*') c++; | |
if (i + 1 < h && j - 1 >= 0 && mine[i + 1][j - 1] == '*') c++; | |
if (i + 1 < h && j + 1 < w && mine[i + 1][j + 1] == '*') c++; | |
if (j - 1 >= 0 && mine[i][j - 1] == '*') c++; | |
if (j + 1 < w && mine[i][j + 1] == '*') c++; | |
return c + '0'; | |
} | |
void init(vector<vector<char> >& mine, int w, int h) { | |
for(int i = 0; i < h; i++) { | |
mine.push_back(vector<char>(w)); | |
} | |
} | |
static int tc = 0; | |
void solve(vector<vector<char> >& mine) { | |
if (mine.size() == 0) { return;} | |
tc++; | |
printf("Field #%i:\n", tc); | |
for (int i = 0; i < (int)mine.size(); i++) { | |
for (int j = 0; j < (int)mine[0].size(); j++) { | |
if (mine[i][j] != '*') mine[i][j] = count9(mine, i, j); | |
printf("%c", mine[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
int main() { | |
int w, h; | |
while(scanf("%d %d\n", &h, &w) == 2) { | |
if (h == 0 && w == 0) break; | |
if (tc > 0) printf("\n"); | |
vector<vector<char> > mine; | |
init(mine, w, h); | |
for(int i = 0; i < h; i++) { | |
for(int j = 0; j < w; j++) { | |
mine[i][j] = getchar(); | |
} | |
getchar(); | |
} | |
solve(mine); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment