#include <stdio.h> //Задаем указатель на массив int *a; //Координаты, в которые нужно вернуться int isearch; int jsearch; //Счетчик возможных путей int n=0; //Прототип функции рекурсивного обхода void way(int, int); int main() { int imax=2; int jmax=2; //Инициализируем массив-поле, создавая вокруг лишнюю рамку толщиной в 2 клетки //Пример для поля 5*5 // 1 1 1 1 1 1 1 1 1 // 1 1 1 1 1 1 1 1 1 // 1 1 0 0 0 0 0 1 1 // 1 1 0 0 0 0 0 1 1 // 1 1 0 0 0 0 0 1 1 // 1 1 0 0 0 0 0 1 1 // 1 1 0 0 0 0 0 1 1 // 1 1 1 1 1 1 1 1 1 // 1 1 1 1 1 1 1 1 1 a = new a[intmax+4][jmax+4]; for (i=0; i<imax+4;i++) { for (j=0;j<jmax+4;j++) { a[i][j]=1; } } //Инициализируем внутренний массив, который будет реальной шахматной доской for (i=2; i<imax+2;i++) { for (j=2;j<jmax+2;j++) { a[i][j]=0; } } //Запускаем ход рекурсии, перебирая количество путей для всех точек массива for (i=2; i<imax+2;i++) { for (j=2;j<jmax+2;j++) { //Сохраняем точку входа в рекурсию и идем по ней isearch=i; jsearch=j; way(isearch,jsearch); } } //Вычитаем из n все случаи, когда счетчик увеличивался из-за самого входа в цикл n=n-imax*jmax; printf("%d ", n); } /** * Рекурсивня функция для подсчета возможных перемещений коня по шахматному полю * * В функцию передаются координаты, где находится сейчас конь * И проводятся попытки переместиться в какую-либо клетку, где он еще не был * При этом на каждом шаге проводится проверка, вернулись ли мы в начальную точку * А также есть ли вообще в принципе возможность вернуться в начало * * n-количество возможных путей * ways-количество занятых путей */ void way(int i, int j) { //Если мы оказались в искомой точке, то увеличиваем счетчик путей if ((i==isearch)&&(j==jsearch)) {n++; return;} //Считаем, сколько возможных путей для входа в начальную точку занято. Если 8-возврат. 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]; if (ways==8){return;} //Выполняем проверку, можем ли мы куда-то сходить, где еще не были, и если точка свободна //То отмечаем её как занятую и перемещаемся в неё, после возврата из рекурсии //Следующим действием мы возвращаем элемент доски в исходное состояние if (a[i+2][j+1]==0){a[i+2][j+1]=1;way(i+2;j+1);a[i+2][j+1]=0;} if (a[i+2][j-1]==0){a[i+2][j-1]=1;way(i+2;j-1);a[i+2][j-1]=0;} if (a[i-2][j+1]==0){a[i-2][j+1]=1;way(i-2;j+1);a[i-2][j+1]=0;} if (a[i-2][j-1]==0){a[i-2][j-1]=1;way(i-2;j-1);a[i-2][j-1]=0;} if (a[i+1][j+2]==0){a[i+1][j+2]=1;way(i+1;j+2);a[i+1][j+2]=0;} if (a[i+1][j-2]==0){a[i+1][j-2]=1;way(i+1;j-2);a[i+1][j-2]=0;} if (a[i-1][j+2]==0){a[i-1][j+2]=1;way(i-1;j+2);a[i-1][j+2]=0;} if (a[i-1][j-2]==0){a[i-1][j-2]=1;way(i-1;j-2);a[i-1][j-2]=0;} //Когда были проверены все точки возможных перемещений-возврат return; }
Standard input is empty
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:30:7: error: 'i' was not declared in this scope for (i=0; i<imax+4;i++) ^ prog.cpp:32:7: error: 'j' was not declared in this scope for (j=0;j<jmax+4;j++) ^ prog.cpp:38:7: error: 'i' was not declared in this scope for (i=2; i<imax+2;i++) ^ prog.cpp:40:7: error: 'j' was not declared in this scope for (j=2;j<jmax+2;j++) ^ prog.cpp:47:7: error: 'i' was not declared in this scope for (i=2; i<imax+2;i++) ^ prog.cpp:49:7: error: 'j' was not declared in this scope for (j=2;j<jmax+2;j++) ^ 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;} ^
Standard output is empty