博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minesweeper(暴力,注意特判)
阅读量:6170 次
发布时间:2019-06-21

本文共 5221 字,大约阅读时间需要 17 分钟。

Minesweeper

Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

Submit
原题链接:

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.

minesweeper.png

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.

Input

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:-

  • The first line will contain two integers separated by a single space denoting 1<=n<=20 and 1 <=m<=20 respectively.
  • The next n lines will contain mcharacters each. Each character will either be a digit (0 to 8inclusive) or 'F'. The presence of 'F' indicates that Clark has flaggedthe square. The digits indicate the number of mines in the adjacentsquares.

Output

Output one line per case:-

  • 'Well done Clark!' if the board was solved successfully.
  • 'Please sweep the mine again!' otherwise.
Note that quotes are for clarity only.

Sample Input

28 8F10122101101FF21121234F1F2F11F21121111211100012FF21101F212F101118 8F10122101101FF21121234F1F2FF1F21121111211100012FF21101F212F10111

Sample Output

Well done Clark!Please sweep the mine again!

这题因为n和m较小,直接暴力即可,但要注意特判所有格都为F的情况,这种情况肯定是玩家出错了。以下提供两个AC CODE

AC CODE #1

 

1 //Memory: 0 KB         Time: 8 MS  2 //Language: C++ 4.1.2         Result: Accepted  3   4 #include 
5 #include
6 using namespace std; 7 8 int n, m; 9 char map[22][22]; 10 int move[8][2] = { {-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}, {
1, 1}, {
1, -1}, {-1, 1}, {-1, -1} }; 11 12 bool check(int i, int j) 13 { 14 int cnt, ii, jj; 15 cnt = map[i][j] - '0'; 16 for(int k = 0; k < 8; k++) 17 { 18 ii = i + move[k][0]; 19 jj = j + move[k][1]; 20 if(ii > -1 && ii < n && jj > -1 && jj < m) 21 { 22 if(map[ii][jj] == 'F') 23 cnt--; 24 } 25 } 26 if(!cnt) return 1; 27 return 0; 28 } 29 30 int main() 31 { 32 int T; 33 int i, j; 34 bool ok, flag; 35 scanf("%d", &T); 36 while(T--) 37 { 38 ok = 1; 39 flag = 0; 40 scanf("%d %d", &n, &m); 41 for(i = 0; i < n; i++) 42 scanf("%s", map[i]); 43 for(i = 0; i < n && ok; i++) 44 { 45 for(j = 0; j < m && ok; j++) 46 { 47 if(map[i][j] != 'F') 48 { 49 ok = check(i, j); 50 flag = 1; 51 } 52 } 53 } 54 if(ok && flag) puts("Well done Clark!"); 55 else puts("Please sweep the mine again!"); 56 } 57 return 0; 58 } 59 60 也可以采取另一个方法,将所有为F的点压入队列,然后遍历队列中的元素,对于每一个F点,将邻接的格子的数(如果该格不是F)减1,最后遍历field,不是全部点为F而且所有点要么是F要么是‘0’,就well done 61 62 AC CODE #2 63 64 // 65 //Memory: 0 KB Time: 8 MS 66 //Language: C++ 4.1.2 Result: Accepted 67 68 #include
69 #include
70 #include
71 using namespace std; 72 73 int n, m; 74 char field[22][22]; 75 typedef struct Map 76 { 77 int y, x; 78 Map(int i, int j){y = i; x = j;} 79 80 }M; 81 queue
que; 82 int move[8][2] = { { 1,0}, { 0, 1}, {-1, 0}, { 0, -1}, { 1, 1}, {-1, -1}, { 1, -1}, {-1, 1}}; 83 84 void Check(M t) 85 { 86 int i, j, k; 87 for(k = 0; k < 8; k++) 88 { 89 i = t.y + move[k][0]; 90 j = t.x + move[k][1]; 91 if(i > -1 && j > -1 && i < n && j < m && field[i][j] != 'F') 92 field[i][j]--; 93 } 94 } 95 96 int main() 97 { 98 char c; 99 int T, i, j;100 bool ok;101 scanf("%d", &T);102 while(T--)103 {104 ok = 0;105 scanf("%d %d", &n, &m);106 for(i = 0; i < n; i++)107 {108 scanf("%c", &c); //吃掉换行符109 for(j = 0; j < m; j++)110 {111 scanf("%c", &field[i][j]);112 if(field[i][j] == 'F')113 {114 M tmp(i, j);115 que.push(tmp);116 }117 }118 }119 if(que.size() != n * m) //当que.size() == n * m时,全为F120 {121 ok = 1;122 while(!que.empty())123 {124 M tmp = que.front();125 Check(tmp);126 que.pop();127 }128 for(i = 0; i < n && ok; i++)129 for(j = 0; j < m; j++)130 {131 if(field[i][j] != 'F' && field[i][j] != '0')132 {133 ok = 0;134 break;135 }136 }137 }138 else139 {140 while(!que.empty())141 que.pop();142 }143 if(ok) puts("Well done Clark!");144 else puts("Please sweep the mine again!");145 }146 return 0;147 }

 

转载地址:http://iavba.baihongyu.com/

你可能感兴趣的文章
高手详解SQL性能优化十条建议
查看>>
修改 IntelliJ IDEA 默认配置路径
查看>>
《现在的泪,都是当年脑子进的水》读书笔记
查看>>
IOSday04 UIButton使用
查看>>
铁大好青年内部分组
查看>>
unity3D ——自带寻路Navmesh入门教程(一)(转)
查看>>
判断字符串是否为数字的函数
查看>>
[emuch.net]MatrixComputations(7-12)
查看>>
linux 命令 — 文件相关
查看>>
自己空闲的时候封装一下
查看>>
Datagard產生gap
查看>>
本机web开发环境的搭建--nginx篇
查看>>
rcnn 理解笔记
查看>>
问答项目---登陆验证码点击切换及异步验证验证码
查看>>
plist文件中iphone和ipad的应用图片设置
查看>>
搜集的一些资源网站链接
查看>>
struts2中类型转换器的使用
查看>>
11G Oracle RAC添加新表空间时数据文件误放置到本地文件系统的修正
查看>>
从91移动应用发展趋势报告看国内应用现状
查看>>
【ORACLE技术嘉年华PPT】MySQL压力测试经验
查看>>