fork download
  1. #include <stdio.h>
  2. //Задаем указатель на массив
  3. int *a;
  4. //Координаты, в которые нужно вернуться
  5. int isearch;
  6. int jsearch;
  7. //Счетчик возможных путей
  8. int n=0;
  9.  
  10. //Прототип функции рекурсивного обхода
  11. void way(int, int);
  12.  
  13. int main()
  14. {
  15. int imax=2;
  16. int jmax=2;
  17.  
  18. //Инициализируем массив-поле, создавая вокруг лишнюю рамку толщиной в 2 клетки
  19. //Пример для поля 5*5
  20. // 1 1 1 1 1 1 1 1 1
  21. // 1 1 1 1 1 1 1 1 1
  22. // 1 1 0 0 0 0 0 1 1
  23. // 1 1 0 0 0 0 0 1 1
  24. // 1 1 0 0 0 0 0 1 1
  25. // 1 1 0 0 0 0 0 1 1
  26. // 1 1 0 0 0 0 0 1 1
  27. // 1 1 1 1 1 1 1 1 1
  28. // 1 1 1 1 1 1 1 1 1
  29. a = new a[intmax+4][jmax+4];
  30. for (int i=0; i<imax+4;i++)
  31. {
  32. for (int j=0;j<jmax+4;j++)
  33. {
  34. a[i][j]=1;
  35. }
  36. }
  37. //Инициализируем внутренний массив, который будет реальной шахматной доской
  38. for (int i=2; i<imax+2;i++)
  39. {
  40. for (int j=2;j<jmax+2;j++)
  41. {
  42. a[i][j]=0;
  43. }
  44. }
  45.  
  46. //Запускаем ход рекурсии, перебирая количество путей для всех точек массива
  47. for (int i=2; i<imax+2;i++)
  48. {
  49. for (int j=2;j<jmax+2;j++)
  50. {
  51. //Сохраняем точку входа в рекурсию и идем по ней
  52. isearch=i;
  53. jsearch=j;
  54. way(isearch,jsearch);
  55. }
  56. }
  57. //Вычитаем из n все случаи, когда счетчик увеличивался из-за самого входа в цикл
  58. n=n-imax*jmax;
  59. printf("%d ", n);
  60. }
  61.  
  62.  
  63. /**
  64.  * Рекурсивня функция для подсчета возможных перемещений коня по шахматному полю
  65.  *
  66.  * В функцию передаются координаты, где находится сейчас конь
  67.  * И проводятся попытки переместиться в какую-либо клетку, где он еще не был
  68.  * При этом на каждом шаге проводится проверка, вернулись ли мы в начальную точку
  69.  * А также есть ли вообще в принципе возможность вернуться в начало
  70.  *
  71.  * n-количество возможных путей
  72.  * ways-количество занятых путей
  73.  */
  74.  
  75. void way(int i, int j)
  76. {
  77. //Если мы оказались в искомой точке, то увеличиваем счетчик путей
  78. if ((i==isearch)&&(j==jsearch)) {n++; return;}
  79. //Считаем, сколько возможных путей для входа в начальную точку занято. Если 8-возврат.
  80. int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
  81. if (ways==8){return;}
  82. //Выполняем проверку, можем ли мы куда-то сходить, где еще не были, и если точка свободна
  83. //То отмечаем её как занятую и перемещаемся в неё, после возврата из рекурсии
  84. //Следующим действием мы возвращаем элемент доски в исходное состояние
  85. if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
  86. if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
  87. if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
  88. if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
  89. if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
  90. if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
  91. if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
  92. if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
  93. //Когда были проверены все точки возможных перемещений-возврат
  94. return;
  95. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:29:10: error: 'a' does not name a type
  a = new a[intmax+4][jmax+4];
          ^
prog.cpp:34:8: error: invalid types 'int[int]' for array subscript
  a[i][j]=1;
        ^
prog.cpp:42:8: error: invalid types 'int[int]' for array subscript
  a[i][j]=0;
        ^
prog.cpp: In function 'void way(int, int)':
prog.cpp:80:20: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                    ^
prog.cpp:80:32: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                ^
prog.cpp:80:44: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                            ^
prog.cpp:80:56: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                                        ^
prog.cpp:80:68: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                                                    ^
prog.cpp:80:80: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                                                                ^
prog.cpp:80:92: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                                                                            ^
prog.cpp:80:104: error: invalid types 'int[int]' for array subscript
 int ways=a[i+2][j+1]+a[i+2][j-1]+a[i-2][j+1]+a[i-2][j+1]+a[i+1][j+2]+a[i+1][j-2]+a[i-1][j+2]+a[i-1][j-2];
                                                                                                        ^
prog.cpp:85:15: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
               ^
prog.cpp:85:31: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
                               ^
prog.cpp:85:42: error: expected ')' before ';' token
 if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
                                          ^
prog.cpp:85:46: error: expected ';' before ')' token
 if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
                                              ^
prog.cpp:85:58: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;}
                                                          ^
prog.cpp:86:15: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
               ^
prog.cpp:86:31: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
                               ^
prog.cpp:86:42: error: expected ')' before ';' token
 if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
                                          ^
prog.cpp:86:46: error: expected ';' before ')' token
 if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
                                              ^
prog.cpp:86:58: error: invalid types 'int[int]' for array subscript
 if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;}
                                                          ^
prog.cpp:87:15: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
               ^
prog.cpp:87:31: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
                               ^
prog.cpp:87:42: error: expected ')' before ';' token
 if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
                                          ^
prog.cpp:87:46: error: expected ';' before ')' token
 if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
                                              ^
prog.cpp:87:58: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;}
                                                          ^
prog.cpp:88:15: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
               ^
prog.cpp:88:31: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
                               ^
prog.cpp:88:42: error: expected ')' before ';' token
 if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
                                          ^
prog.cpp:88:46: error: expected ';' before ')' token
 if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
                                              ^
prog.cpp:88:58: error: invalid types 'int[int]' for array subscript
 if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;}
                                                          ^
prog.cpp:89:15: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
               ^
prog.cpp:89:31: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
                               ^
prog.cpp:89:42: error: expected ')' before ';' token
 if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
                                          ^
prog.cpp:89:46: error: expected ';' before ')' token
 if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
                                              ^
prog.cpp:89:58: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;}
                                                          ^
prog.cpp:90:15: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
               ^
prog.cpp:90:31: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
                               ^
prog.cpp:90:42: error: expected ')' before ';' token
 if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
                                          ^
prog.cpp:90:46: error: expected ';' before ')' token
 if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
                                              ^
prog.cpp:90:58: error: invalid types 'int[int]' for array subscript
 if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;}
                                                          ^
prog.cpp:91:15: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
               ^
prog.cpp:91:31: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
                               ^
prog.cpp:91:42: error: expected ')' before ';' token
 if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
                                          ^
prog.cpp:91:46: error: expected ';' before ')' token
 if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
                                              ^
prog.cpp:91:58: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;}
                                                          ^
prog.cpp:92:15: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
               ^
prog.cpp:92:31: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
                               ^
prog.cpp:92:42: error: expected ')' before ';' token
 if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
                                          ^
prog.cpp:92:46: error: expected ';' before ')' token
 if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
                                              ^
prog.cpp:92:58: error: invalid types 'int[int]' for array subscript
 if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;}
                                                          ^
stdout
Standard output is empty