fork download
  1. #define CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <bits/stdc++.h>
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. #include <ext/pb_ds/tree_policy.hpp>
  6.  
  7. using namespace __gnu_pbds;
  8. using namespace std;
  9. typedef long long ll;
  10. typedef long double ld;
  11. typedef unsigned long long ull;
  12. #define ordered_set tree<pair<ll,ll>, null_type, less <pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update>
  13. #define ll long long
  14. #define all(name) name.begin(),name.end()
  15. #define rall(name) name.rbegin(),name.rend()
  16. #define sz(s) (int)s.size()
  17. const int N = 2e6 + 10, mod = 1e9 + 7;
  18. const double PI = asin(1.0) * 2;
  19. const int OO = 0x3f3f3f3f;
  20. int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
  21. int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
  22.  
  23. void fast() {
  24. std::ios_base::sync_with_stdio(0);
  25. cin.tie(0);
  26. cout.tie(0);
  27. }
  28.  
  29. map<string, bool> mp;
  30.  
  31. bool isWin(string s) {
  32. return (
  33. // row
  34. (s[0] != '.' && s[0] == s[1] && s[0] == s[2]) ||
  35. (s[3] != '.' && s[3] == s[4] && s[3] == s[5]) ||
  36. (s[6] != '.' && s[6] == s[7] && s[6] == s[8]) ||
  37. // col
  38. (s[0] != '.' && s[0] == s[3] && s[0] == s[6]) ||
  39. (s[1] != '.' && s[1] == s[4] && s[1] == s[7]) ||
  40. (s[2] != '.' && s[2] == s[5] && s[2] == s[8]) ||
  41. // diag
  42. (s[0] != '.' && s[0] == s[4] && s[0] == s[8]) ||
  43. (s[0] != '.' && s[2] == s[4] && s[2] == s[6])
  44. );
  45. }
  46.  
  47. string ans;
  48. bool ck = 0;
  49.  
  50. void bfs(string s = ".........") {
  51. ck = 0;
  52. queue<pair<string, bool>> q; // grid, player( X --> 1, O --> 0)
  53. q.push({s, 1});
  54. while (!q.empty()) {
  55. pair<string, bool> p = q.front();
  56. q.pop();
  57. string grid = p.first;
  58. if (grid == ans) {
  59. ck = 1;
  60. break;
  61. }
  62. if (!isWin(grid)) {
  63. for (int i = 0; i < 9; i++) {
  64. if (grid[i] == '.') {
  65. if (p.second) grid[i] = 'X'; else grid[i] = 'O';
  66. if (grid[i] == ans[i])
  67. q.push({grid, p.second ^ 1});
  68. grid[i] = '.';
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. void solve() {
  76. string s;
  77. ans = "";
  78. for (int i = 0; i < 3; i++) {
  79. cin >> s;
  80. ans += s;
  81. }
  82. bfs();
  83. cout << (ck ? "yes" : "no") << "\n";
  84. }
  85.  
  86. int main() {
  87. fast();
  88. //freopen("abc.in", "r", stdin);
  89. //freopen("output.txt", "w", stdout);
  90. int T = 1;
  91. cin >> T;
  92. while (T--) {
  93. solve();
  94. }
  95. return 0;
  96. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
no