fork(2) download
  1. // Задача о ходе коня, Coded by Decker ;))
  2.  
  3. #include <stdio.h>
  4. #define size 8
  5.  
  6. int arr[size][size], row[size*size], col[size*size];
  7. int ktmov1[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
  8. int ktmov2[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  9.  
  10. int i, j, move_num, d;
  11.  
  12. int check(int i, int j) {
  13. register int a, b, e, sum = 0;
  14.  
  15. if ( i < 0 || i > (size-1) || j < 0 || j > (size-1) ) return 0;
  16. if ( arr[i][j] == 1 ) return 0;
  17.  
  18. for ( a = 0 ; a <= 7 ; a++ ) {
  19. b = i + ktmov1[a]; e = j + ktmov2[a];
  20. if ( b < 0 || b > (size-1) || e < 0 || e > (size-1) ) continue;
  21. if ( arr[b][e] == 1 ) continue;
  22. sum++;
  23. }
  24.  
  25. if ((sum == 0) && (arr[i][j]==0)) sum = 1; // последний ход
  26.  
  27. return sum;
  28. }
  29.  
  30. main() {
  31.  
  32. i=0; j=0;
  33. addknight( );
  34.  
  35. }
  36.  
  37. addknight( ) {
  38.  
  39. register int a, b, e;
  40. register int mina, min;
  41.  
  42. arr[i][j] = 1; row[move_num] = i; col[move_num] = j; move_num++;
  43.  
  44. if ( move_num > size*size-1 ) {
  45. //if (i==7 && j==7) {
  46. writeboard( size*size-1 );
  47. printf("i = %d, j = %d\n",i,j);
  48. exit ( 0 );
  49. //}
  50. }
  51.  
  52. // printf("[%d] i = %d, j = %d\n",move_num,i,j);
  53.  
  54. min = 8; mina = 0;
  55. for ( a = 0 ; a <= 7 ; a++ ) {
  56. b = i + ktmov1[a]; e = j + ktmov2[a];
  57. // printf("%d. [%d,%d] = %d\n",a, b,e,check(b,e));
  58. if ((check(b,e) > 0) && (check(b,e) < min)) { min = check(b,e); mina = a; }
  59. }
  60.  
  61. for ( a = 0 ; a <= 7 ; a++ ) {
  62. b = i + ktmov1[a]; e = j + ktmov2[a];
  63. if (check(b,e) == min) {
  64. // printf("%d. [%d,%d] = %d\n",a, b,e,check(b,e));
  65. i = b; j = e; addknight();
  66. }
  67. }
  68.  
  69. move_num-- ; arr[row[move_num]][col[move_num]] = 0; move_num--; i = row[move_num]; j = col[move_num]; move_num++;
  70.  
  71. }
  72.  
  73. writeboard( int step ) {
  74. int a,i,j;
  75. int t[size][size];
  76.  
  77. for (i=0; i<=size-1; i++) {
  78. for (j=0; j<=size-1; j++) {
  79. t[i][j] = 0;
  80. }
  81. }
  82.  
  83.  
  84.  
  85. for ( a = 0 ; a <= step ; a++ ) {
  86. // printf("%d. [%d,%d] = %d\n",a,col[a],row[a],a+1);
  87. t[col[a]][row[a]] = a+1;
  88. }
  89.  
  90.  
  91. for (i=0; i<=size-1; i++) {
  92. for (j=0; j<=size-1; j++) {
  93. printf ( "%5d;", t[i][j] );
  94. }
  95. printf("\n");
  96. }
  97.  
  98. printf("\n");
  99.  
  100. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
    1;   26;   15;   24;   29;   36;   13;   32;
   16;   23;   28;   35;   14;   31;   40;   37;
   27;    2;   25;   30;   61;   38;   33;   12;
   22;   17;   62;   45;   34;   41;   50;   39;
    3;   46;   21;   60;   49;   64;   11;   42;
   18;   57;   48;   63;   44;   53;    8;   51;
   47;    4;   55;   20;   59;    6;   43;   10;
   56;   19;   58;    5;   54;    9;   52;    7;

i = 5, j = 4