Advertisement
Saleh127

Light oj(LO) 1397

Feb 25th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll f=0;
  6. ll a[11][11];
  7. char ee;
  8. bool check(ll r,ll c,ll x)
  9. {
  10. ll i,j;
  11. if(a[r][c]!=0) return 0;
  12. for(i=0; i<9; i++)
  13. {
  14. if(a[i][c]==x) return 0;
  15. if(a[r][i]==x) return 0;
  16. }
  17.  
  18. ll s=r-r%3;
  19. ll e=c-c%3;
  20.  
  21. for(i=0; i<3; i++)
  22. {
  23. for(j=0; j<3; j++)
  24. {
  25. if(a[i+s][j+e]==x)
  26. {
  27. return 0;
  28. }
  29. }
  30. }
  31. return 1;
  32. }
  33.  
  34.  
  35. bool findindex(ll &u,ll &v)
  36. {
  37. ll m=10000;
  38. for(ll i=0;i<9;i++)
  39. {
  40. for(ll j=0;j<9;j++)
  41. {
  42. if(a[i][j]==0)
  43. {
  44. ll cnt=0;
  45. for(ll dd=1;dd<=9;dd++)
  46. {
  47. if(check(i,j,dd)) cnt++;
  48. }
  49. if(cnt<m)
  50. {
  51. m=cnt;
  52. u=i;
  53. v=j;
  54. }
  55. }
  56.  
  57. }
  58. }
  59. if(m<10) return 1;
  60. return 0;
  61. }
  62.  
  63.  
  64. bool dfs()
  65. {
  66. ll u,v;
  67.  
  68. if(findindex(u,v)==0)
  69. {
  70. return 1;
  71. }
  72.  
  73. for(ll z=1; z<=9; z++)
  74. {
  75. if(check(u,v,z)==1)
  76. {
  77. a[u][v]=z;
  78. if(dfs()) return 1;
  79. a[u][v]=0;
  80. }
  81.  
  82. }
  83. return 0;
  84. }
  85.  
  86. int main()
  87. {
  88. ios_base::sync_with_stdio(0);
  89. cin.tie(0);
  90. cout.tie(0);
  91.  
  92. test
  93. {
  94. string dd;
  95.  
  96. getline(cin,dd);
  97.  
  98. for(ll i=0; i<9; i++)
  99. {
  100. for(ll j=0; j<9; j++)
  101. {
  102. cin>>ee;
  103. if(ee=='.') a[i][j]=0;
  104. else a[i][j]=ee-48;
  105. }
  106. }
  107.  
  108. cout<<"Case "<<cs<<":"<<endl;
  109. dfs();
  110. for(ll i=0; i<9; i++)
  111. {
  112. for(ll j=0; j<9; j++)
  113. {
  114. cout<<a[i][j];
  115. }
  116. cout<<endl;
  117. }
  118. }
  119.  
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement