fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #define You_ss_ef ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  8. #define pi 3.141592654
  9. #define tc int T; cin >> T; while (T--)
  10. #define FP(_) fixed << std::setprecision(_)
  11. #define Gaza main
  12. #define ll long long
  13. #define ld long double
  14. #define ss << ' '
  15. #define el << '\n'
  16. #define all(_) _.begin(), _.end()
  17. #define rall(_) _.rbegin(), _.rend()
  18. #define uni(_) _.erase(unique(all(_)), _.end())
  19. using namespace std;
  20. void IO() {
  21. #ifndef ONLINE_JUDGE
  22. freopen("input.txt", "r", stdin);
  23. freopen("output.txt", "w", stdout);
  24. #endif
  25. }
  26. int dx[] = { 1 , -1 , 0 , 0 , 1 , 1 , -1 , -1 };
  27. int dy[] = { 0 , 0 , -1 , 1 , 1 , -1 , -1 , 1 };
  28. int ans = 0;
  29. vector <int> num;
  30. vector <pair<int,int>> idx;
  31. vector <vector <int>> v;
  32. bool valid()
  33. {
  34. vector <pair<int,pair<int,int>>> a;
  35. for(int i = 1; i <= 3; i++) {
  36. for(int j = 1; j <= 3; j++) {
  37. a.push_back({v[i][j],{i,j}});
  38. }
  39. }
  40. int cnt = 0;
  41. for(int i = 1; i < 9; i++) {
  42. int x1 = a[i].second.first , y1 = a[i].second.second;
  43. int x2 = a[i+1].second.first , y2 = a[i+1].second.second;
  44. if(abs(x1-x2) == 1 or abs(y1-y2) == 1) cnt++;
  45. }
  46. return cnt == 8;
  47. }
  48. void answer()
  49. {
  50. set <int> st;
  51. v = vector <vector <int>> (5, vector <int> (5,150));
  52. for(int i = 1; i <= 3; i++) {
  53. for(int j = 1; j <= 3; j++) {
  54. char x; cin >> x;
  55. v[i][j] = x - '0';
  56. if(!v[i][j]) idx.push_back({i,j});
  57. st.emplace(v[i][j]);
  58. }
  59. }
  60. for(int i = 1; i <= 9; i++) {
  61. if(!st.count(i)) num.push_back(i);
  62. }
  63. do {
  64. for(int i = 0; i < idx.size(); i++) {
  65. int x = idx[i].first , y = idx[i].second;
  66. v[x][y] = num[i];
  67. }
  68. if(valid()) {
  69. ans++;
  70. for(int i = 1; i <= 3; i++) {
  71. for(int j = 1; j <= 3; j++) {
  72. cout << v[i][j] ss;
  73. }
  74. cout el ;
  75. }
  76. cout el;
  77. }
  78. } while(next_permutation(all(num)));
  79. cout << ans;
  80. }
  81. int Gaza()
  82. { You_ss_ef
  83. IO();
  84. int TC = 1;
  85. // cin >> TC;
  86. do {
  87. answer();
  88. TC--;
  89. if (TC)
  90. cout el;
  91. } while (TC != 0);
  92. return 0;
  93. }
  94.  
  95.  
Success #stdin #stdout 0.07s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty