CodeForces 825B(DFS)

拼搏现实的明天。 2022-06-06 12:59 256阅读 0赞

问题描述:

Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.

In current match they have made some turns and now it’s Alice’s turn. She wonders if she can put cross in such empty cell that she wins immediately.

Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.

Input

You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters ‘X’ being a cross, letters ‘O’ being a nought and ‘.’ being an empty cell. The number of ‘X’ cells is equal to the number of ‘O’ cells and there is at least one of each type. There is at least one empty cell.

It is guaranteed that in the current arrangement nobody has still won.

Output

Print ‘YES’ if it’s possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print ‘NO’.

Example

Input

  1. XX.XX.....
  2. .....OOOO.
  3. ..........
  4. ..........
  5. ..........
  6. ..........
  7. ..........
  8. ..........
  9. ..........
  10. ..........

Output

  1. YES

Input

  1. XXOXX.....
  2. OO.O......
  3. ..........
  4. ..........
  5. ..........
  6. ..........
  7. ..........
  8. ..........
  9. ..........
  10. ..........

Output

  1. NO

题目题意:给了我们一个10*10的图,问我们能否在加上一个X满足五子棋的成立条件.

题目分析:我们遍历每一个’.’点,把它变成’X‘,然后去沿四个方向去dfs(),

题目代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<string>
  6. using namespace std;
  7. char mmap[15][15];
  8. bool vis[15][15],ans;
  9. int Next1[2][2]={
  10. {-1,0},{1,0}};
  11. int Next2[2][2]={
  12. {0,-1},{0,1}};
  13. int Next3[2][2]={
  14. {-1,-1},{1,1}};
  15. int Next4[2][2]={
  16. {1,-1},{-1,1}};
  17. int cnt;
  18. void dfs1(int x,int y)
  19. {
  20. if (ans) return ;
  21. if (cnt==5) {
  22. ans=true;
  23. return ;
  24. }
  25. for (int i=0;i<2;i++) {
  26. int dx=x+Next1[i][0];
  27. int dy=y+Next1[i][1];
  28. if (dx<1||dx>10||dy<1||dy>10) continue;
  29. if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
  30. if (mmap[dx][dy]=='X') {
  31. vis[dx][dy]=true;
  32. cnt++;
  33. dfs1(dx,dy);
  34. }
  35. }
  36. }
  37. void dfs2(int x,int y)
  38. {
  39. if (ans) return ;
  40. if (cnt==5) {
  41. ans=true;
  42. return ;
  43. }
  44. for (int i=0;i<2;i++) {
  45. int dx=x+Next2[i][0];
  46. int dy=y+Next2[i][1];
  47. if (dx<1||dx>10||dy<1||dy>10) continue;
  48. if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
  49. if (mmap[dx][dy]=='X') {
  50. vis[dx][dy]=true;
  51. cnt++;
  52. dfs2(dx,dy);
  53. }
  54. }
  55. }
  56. void dfs3(int x,int y)
  57. {
  58. if (ans) return ;
  59. if (cnt==5) {
  60. ans=true;
  61. return ;
  62. }
  63. for (int i=0;i<2;i++) {
  64. int dx=x+Next3[i][0];
  65. int dy=y+Next3[i][1];
  66. if (dx<1||dx>10||dy<1||dy>10) continue;
  67. if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
  68. if (mmap[dx][dy]=='X') {
  69. vis[dx][dy]=true;
  70. cnt++;
  71. dfs3(dx,dy);
  72. }
  73. }
  74. }
  75. void dfs4(int x,int y)
  76. {
  77. if (ans) return ;
  78. if (cnt==5) {
  79. ans=true;
  80. return ;
  81. }
  82. for (int i=0;i<2;i++) {
  83. int dx=x+Next4[i][0];
  84. int dy=y+Next4[i][1];
  85. if (dx<1||dx>10||dy<1||dy>10) continue;
  86. if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
  87. if (mmap[dx][dy]=='X') {
  88. vis[dx][dy]=true;
  89. cnt++;
  90. dfs4(dx,dy);
  91. }
  92. }
  93. }
  94. int main()
  95. {
  96. for (int i=1;i<=10;i++) {
  97. scanf("%s",mmap[i]+1);
  98. }
  99. ans=false;
  100. for (int i=1;i<=10;i++) {
  101. for (int j=1;j<=10;j++) {
  102. if (mmap[i][j]=='.'&&!ans) {
  103. mmap[i][j]='X';
  104. memset (vis,false,sizeof (vis));
  105. vis[i][j]=true;
  106. cnt=1;
  107. dfs1(i,j);
  108. memset (vis,false,sizeof (vis));
  109. vis[i][j]=true;
  110. cnt=1;
  111. dfs2(i,j);
  112. memset (vis,false,sizeof (vis));
  113. vis[i][j]=true;
  114. cnt=1;
  115. dfs3(i,j);
  116. memset (vis,false,sizeof (vis));
  117. vis[i][j]=true;
  118. cnt=1;
  119. dfs4(i,j);
  120. mmap[i][j]='.';
  121. }
  122. if (ans) break;
  123. }
  124. if (ans) break;
  125. }
  126. if (ans) puts("YES");
  127. else puts("NO");
  128. }

发表评论

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

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

相关阅读