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. #define fast_io() ios::sync_with_stdio(false); cin.tie(nullptr);
  14.  
  15. const int N = 9;
  16.  
  17. bool check(int board[N][N], int row, int col, int x){
  18. // kiểm tra hàng xem coi số đã xuất hiện chưa
  19. for (int i = 0; i < N; i++){
  20. if (board[row][i] == x){
  21. return false;
  22. }
  23. }
  24. // kiểm tra cột xem coi số đã xuất hiện chưa
  25. for (int i = 0; i < N; i++){
  26. if (board[i][col] == x){
  27. return false;
  28. }
  29. }
  30. // kiểm tra trong ô 3x3 đang xét số đã xuất hiện chưa
  31. int startRow = row - row % 3, startCol = col - col % 3;
  32. for (int i = startRow; i < startRow + 3; i++){
  33. for (int j = startCol; j < startCol + 3; j++){
  34. if (board[i][j] == x){
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41.  
  42. bool BackTrack(int board[N][N], int row, int col){
  43. if (row == N - 1 && col == N){
  44. return true;
  45. }
  46. if (col == N){
  47. row++;
  48. col = 0;
  49. }
  50. if (board[row][col] > 0){
  51. return BackTrack(board, row, col + 1);
  52. }
  53. for (int i = 1; i <= N; i++){
  54. if (check(board, row, col, i)){
  55. board[row][col] = i;
  56. if (BackTrack(board, row, col + 1)){
  57. return true;
  58. }
  59. }
  60. board[row][col] = 0;
  61. }
  62. return false;
  63. }
  64. int main(){
  65. int board[N][N];
  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(board, 0, 0)){
  72. cout << "28tech" << endl;
  73. }
  74. else cout << "29tech" << endl;
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
28tech