fork(1) download
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int curr[9][9]=
  6. {
  7. {5,8,2,7,1,6,9,3,4},
  8. {3,1,4,5,2,9,8,7,6},
  9. {9,7,6,4,3,8,5,1,2},
  10. {6,4,7,9,5,2,3,8,1},
  11. {8,5,9,1,6,3,2,4,7},
  12. {1,2,3,8,7,4,6,5,9},
  13. {4,6,5,3,9,1,7,2,8},
  14. {2,3,1,6,8,7,4,9,5},
  15. {7,9,8,2,4,5,1,6,3},
  16. };
  17.  
  18. bool can_input_A(int x, int y, int value) {
  19. for(int i = 0; i < 9; i++) {
  20. if (value == curr[x][i] || value == curr[i][y]) { return false; }
  21. }
  22. for (int i = (x/3)*3; i <= ((x/3)*3)+2; i++) {
  23. for (int j = (y/3)*3; j <= ((y/3)*3)+2; j++) {
  24. if (curr[i][j] == value) { return false; }
  25. }
  26. } return true;
  27. }
  28.  
  29. bool check_A()
  30. {
  31. bool ret=true;
  32. for(unsigned y=0;y<9;++y)
  33. {
  34. for(unsigned x=0;x<9;++x)
  35. {
  36. int value=curr[y][x];
  37. curr[y][x]=0;
  38. for(int i=1;i<9;++i) if((i==value)^(can_input_A(y,x,i))) ret=false;
  39. curr[y][x]=value;
  40. }
  41. }
  42. return ret;
  43. }
  44.  
  45. bool can_input_B(int x,int y,int value)
  46. {
  47. for(int i=0;i<9;++i) if((value==curr[y][i])||(value==curr[i][x])) return false;
  48. for(int dx=3*(x/3),dy=3*(y/3),py=dy;py<dy+3;++py)
  49. {
  50. for(int px=dx;px<dx+3;++px)
  51. {
  52. if(value==curr[py][px]) return false;
  53. }
  54. }
  55. return true;
  56. }
  57.  
  58. bool check_B()
  59. {
  60. bool ret=true;
  61. for(unsigned y=0;y<9;++y)
  62. {
  63. for(unsigned x=0;x<9;++x)
  64. {
  65. int value=curr[y][x];
  66. curr[y][x]=0;
  67. for(int i=1;i<9;++i) if((i==value)^(can_input_B(x,y,i))) ret=false;
  68. curr[y][x]=value;
  69. }
  70. }
  71. return ret;
  72. }
  73.  
  74. int main()
  75. {
  76. const unsigned testcount=30000;
  77. clock_t tm;
  78.  
  79. bool fa=true;
  80. tm=clock();
  81. for(unsigned t=0;t<testcount;++t) fa&=check_A();
  82. cout<<((clock()-tm)/(double)CLOCKS_PER_SEC)<<" sec ("<<fa<<")"<<endl;
  83.  
  84. bool fb=true;
  85. tm=clock();
  86. for(unsigned t=0;t<testcount;++t) fb&=check_B();
  87. cout<<((clock()-tm)/(double)CLOCKS_PER_SEC)<<" sec ("<<fb<<")"<<endl;
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.9s 3296KB
stdin
Standard input is empty
stdout
0.47 sec (1)
0.43 sec (1)