Minesweeper
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Minesweeper is a single-player computer game. Theobjective of the game is to clear an abstract minefield withoutdetonating a mine. When the game is started, the player is presentedwith a grid ofn x m blank squares.If the player clicks on a square without a mine, a digit is revealed inthat square, the digit indicating the number of adjacent squares thatcontain mines. Two squares are adjacent if they share an edge or acorner, i. e. a square can have at most 8 adjacent squares. By usinglogic, players can in many instances use this information to deducethat certain other squares are mine-free (or mine-filled), and proceedto click on additional squares to clear them or mark them with flaggraphics to indicate the presence of a mine.
Clark Kent is a Minesweeper addict. And with help from hisKryptonian (a planet far far away from earth) powers he solves them atlightning speed and gives them to you. Your job is to tell him whetherthe solved version is correct or not. A board is correctly solved iffall flagged squares should contain a mine and every square containing a numberX has exactly X adjacent squares flagged.
The first line of input will contain an integer T <= 20 denoting the number of test cases.
Each test case will be formatted as follows:-
Output one line per case:-
2 8 8 F1012210 1101FF21 121234F1 F2F11F21 12111121 1100012F F21101F2 12F10111 8 8 F1012210 1101FF21 121234F1 F2FF1F21 12111121 1100012F F21101F2 12F10111
Well done Clark! Please sweep the mine again!
这题因为n和m较小,直接暴力即可,但要注意特判所有格都为F的情况,这种情况肯定是玩家出错了。以下提供两个AC CODE
AC CODE #1
//Memory: 0 KB Time: 8 MS //Language: C++ 4.1.2 Result: Accepted #include <iostream> #include <cstdio> using namespace std; int n, m; char map[22][22]; int move[8][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1} }; bool check(int i, int j) { int cnt, ii, jj; cnt = map[i][j] - '0'; for(int k = 0; k < 8; k++) { ii = i + move[k][0]; jj = j + move[k][1]; if(ii > -1 && ii < n && jj > -1 && jj < m) { if(map[ii][jj] == 'F') cnt--; } } if(!cnt) return 1; return 0; } int main() { int T; int i, j; bool ok, flag; scanf("%d", &T); while(T--) { ok = 1; flag = 0; scanf("%d %d", &n, &m); for(i = 0; i < n; i++) scanf("%s", map[i]); for(i = 0; i < n && ok; i++) { for(j = 0; j < m && ok; j++) { if(map[i][j] != 'F') { ok = check(i, j); flag = 1; } } } if(ok && flag) puts("Well done Clark!"); else puts("Please sweep the mine again!"); } return 0; }
也可以采取另一个方法,将所有为F的点压入队列,然后遍历队列中的元素,对于每一个F点,将邻接的格子的数(如果该格不是F)减1,最后遍历field,不是全部点为F而且所有点要么是F要么是‘0’,就well done
AC CODE #2
// //Memory: 0 KB Time: 8 MS //Language: C++ 4.1.2 Result: Accepted #include <iostream> #include <cstdio> #include <queue> using namespace std; int n, m; char field[22][22]; typedef struct Map { int y, x; Map(int i, int j){y = i; x = j;} }M; queue<Map> que; int move[8][2] = {{1,0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; void Check(M t) { int i, j, k; for(k = 0; k < 8; k++) { i = t.y + move[k][0]; j = t.x + move[k][1]; if(i > -1 && j > -1 && i < n && j < m && field[i][j] != 'F') field[i][j]--; } } int main() { char c; int T, i, j; bool ok; scanf("%d", &T); while(T--) { ok = 0; scanf("%d %d", &n, &m); for(i = 0; i < n; i++) { scanf("%c", &c); //吃掉换行符 for(j = 0; j < m; j++) { scanf("%c", &field[i][j]); if(field[i][j] == 'F') { M tmp(i, j); que.push(tmp); } } } if(que.size() != n * m) //当que.size() == n * m时,全为F { ok = 1; while(!que.empty()) { M tmp = que.front(); Check(tmp); que.pop(); } for(i = 0; i < n && ok; i++) for(j = 0; j < m; j++) { if(field[i][j] != 'F' && field[i][j] != '0') { ok = 0; break; } } } else { while(!que.empty()) que.pop(); } if(ok) puts("Well done Clark!"); else puts("Please sweep the mine again!"); } return 0; }