fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <climits>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. #include <cmath>
  9. using namespace std;
  10.  
  11. long long mod = 1e9 + 7;
  12.  
  13.  
  14. const int N = 9;
  15. int board[10][10];
  16. bool check(int row, int col, int x){
  17. // kiểm tra hàng
  18. for (int i = 0; i < N; i++){
  19. if (board[row][i] == x){
  20. return false;
  21. }
  22. }
  23. // kiểm tra cột
  24. for (int i = 0; i < N; i++){
  25. if (board[i][col] == x){
  26. return false;
  27. }
  28. }
  29. // kiểm tra trong ô 3x3
  30. int startRow = row - row % 3, startCol = col - col % 3;
  31. for (int i = startRow; i < startRow + 3; i++){
  32. for (int j = startCol; j < startCol + 3; j++){
  33. if (board[i][j] == x){
  34. return false;
  35. }
  36. }
  37. }
  38. return true;
  39. }
  40.  
  41. bool BackTrack(int row, int col){
  42. if (row == N - 1 && col == N){
  43. return true;
  44. }
  45. if (col == N){
  46. row++;
  47. col = 0;
  48. }
  49. if (board[row][col] != 0){
  50. return BackTrack(row, col + 1);
  51. }
  52. else{
  53. for (int i = 1; i <= N; i++){
  54. if (check(row, col, i)){
  55. board[row][col] = i;
  56. if (BackTrack(row, col + 1)){
  57. return true;
  58. }
  59. }
  60. board[row][col] = 0;
  61. }
  62. return false;
  63. }
  64. }
  65. int main(){
  66. for (int i = 0; i < N; i++){
  67. for (int j = 0; j < N; j++){
  68. cin >> board[i][j];
  69. }
  70. }
  71. if (BackTrack(0, 0)){
  72. cout << "28tech" << endl;
  73. }
  74. else cout << "29tech" << endl;
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
28tech