TOJ3184 Mine sweeping dfs

迷南。 2022-05-19 11:14 256阅读 0赞

3184: Mine sweeping

描述

I think most of you are using system named of xp or vista or win7.And these system is consist of a famous game what is mine sweeping.You must have played it before.If you not,just look the game rules followed.

There are N*N grids on the map which contains some mines , and if you touch that ,you lose the game.If a position not containing a mine is touched, an integer K (0 < =K <= 8) appears indicating that there are K mines in the eight adjacent positions. If K = 0, the eight adjacent positions will be touched automatically, new numbers will appear and this process is repeated until no new number is 0. Your task is to mark the mines’ positions without touching them.

Now, given the distribution of the mines, output the numbers appearing after the player’s first touch.

输入

The first line of each case is two numbers N (1 <= N <= 100) .Then there will be a map contain N*N grids.The map is just contain O and X.’X’ stands for a mine, ‘O’ stand for it is safe with nothing. You can assume there is at most one mine in one position. The last line of each case is two numbers X and Y(0<=X<N,0<=Y<N, indicating the position of the player’s first touch.

输出

If the player touches the mine, just output “it is a beiju!”.

If the player doesn’t touch the mine, output the numbers appearing after the touch. If a position is touched by the player or by the computer automatically, output the number. If a position is not touched, output a dot ‘.’.

Output a blank line after each test case.

样例输入

5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
1 1
5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
0 0

样例输出

it is a beiju!

1….
…..
…..
…..
…..

扫雷,先判断初始点是否为地雷,如果是的话直接输出“it is a beiju!”,否则从起始点开始搜,每一个‘O’判断8个方向是否有地雷,如果有1个则为1,有2个则为2,以此类推,然后如果有一个数字非0,则结束搜索,输出数字,没有搜到的地方不统计数字。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<vector>
  11. using namespace std;
  12. #define inf 0x3f3f3f3f
  13. #define LL long long
  14. bool v[110][110];
  15. char show[110][110];
  16. int n;
  17. int fx[8][2]={1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1};
  18. bool f(int i,int j)
  19. {
  20. if(i>=0&&i<n&&j>=0&&j<n&&v[i][j])
  21. return 1;
  22. else
  23. return 0;
  24. }
  25. void dfs(int i,int j)
  26. {
  27. if(i<0||i>=n||j<0||j>=n||show[i][j]||f(i,j))
  28. return;
  29. for(int k=0;k<8;k++)
  30. {
  31. int x=i+fx[k][0];
  32. int y=j+fx[k][1];
  33. if(f(x,y))
  34. show[i][j]++;
  35. }
  36. show[i][j]+='0';
  37. if(show[i][j]=='0')
  38. {
  39. for(int k=0;k<8;k++)
  40. {
  41. int x=i+fx[k][0];
  42. int y=j+fx[k][1];
  43. dfs(x,y);
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. int i,j,x,y;
  50. while(scanf("%d",&n)!=EOF)
  51. {
  52. for(i=0;i<n;i++)
  53. {
  54. getchar();
  55. for(j=0;j<n;++j)
  56. {
  57. show[0][0]=getchar();
  58. if(show[0][0]=='O')
  59. v[i][j] = 0;
  60. else
  61. v[i][j] = 1;
  62. }
  63. }
  64. scanf("%d%d",&x,&y);
  65. if(v[x][y])
  66. printf("it is a beiju!\n\n");
  67. else
  68. {
  69. memset(show,0,sizeof(show));
  70. dfs(x,y);
  71. for(i=0;i<n;++i)
  72. {
  73. for(j=0;j<n;++j)
  74. {
  75. if(show[i][j])
  76. printf("%c",show[i][j]);
  77. else
  78. printf(".");
  79. }
  80. printf("\n");
  81. }
  82. printf("\n");
  83. }
  84. }
  85. }

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TOJ1292 排序

    滴,集训第二十九天打卡。 (其实都快结束了... 距离老师上一次开比赛也是九天前了,于是我只能在TOJ划水了... 还在https://www.panda.tv/1352

    相关 TOJ 4972: 数独 4*4 dfs

    描述 预计的周末天气非常不错,小T和小伙伴在满足的吃完烧烤后,玩起了数独。所谓数独就是在N\N的表格上填补数字,使得每行每列每块上都存在1~N这N个不同的数字。不过这是小T临

    相关 UVALive 3530 Martian Mining

    题意:给出n\m网格中每个格子有A,B两种矿,A矿必须从右向左运输,B矿必须从下往上运输,管子不能拐弯或者折断,要求收集到的A,B矿的总量尽量大。 分析:每个格子不是A