#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair locationPair; /*returns positive minimum*/ int posMin(int a, int b) { if (a < 0) return b; if (b < 0) return a; return (a < b) ? a : b; } int minPath(vector< vector > board, locationPair start, locationPair end, int a, int b, int currentMin) { /*if TTL expired - no path found*/ if (currentMin == 0) { return -1; } /*if arrived to end*/ if (start.first == end.first && start.second == end.second) { return 0; } /*if looped back*/ if (board[start.first][start.second] == 1) { return -1; } board[start.first][start.second] = 1; /*find min path in next moves*/ int path = -1; if (start.first + a <= end.first && start.second + b <= end.first) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first+a << "," << start.second+b << endl; int tmpPath = minPath(board, locationPair(start.first + a, start.second + b), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path++ = " << path << endl; } } if (start.first + a <= end.first && start.second - b >= 0) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first+a << "," << start.second-b << endl; int tmpPath = minPath(board, locationPair(start.first + a, start.second - b), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path+- = " << path << endl; } } if (start.first - a >= 0 && start.second + b <= end.first) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first-a << "," << start.second+b << endl; int tmpPath = minPath(board, locationPair(start.first - a, start.second + b), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path-+ = " << path << endl; } } if (start.first - a >= 0 && start.second - b >= 0) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first-a << "," << start.second-b << endl; int tmpPath = minPath(board, locationPair(start.first - a, start.second - b), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path-- = " << path << endl; } } /*reverese a and b*/ if (start.first + b <= end.first && start.second + a <= end.first) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first+a << "," << start.second+b << endl; int tmpPath = minPath(board, locationPair(start.first + b, start.second + a), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path++ = " << path << endl; } } if (start.first + b <= end.first && start.second - a >= 0) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first+a << "," << start.second-b << endl; int tmpPath = minPath(board, locationPair(start.first + b, start.second - a), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path+- = " << path << endl; } } if (start.first - b >= 0 && start.second + a <= end.first) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first-a << "," << start.second+b << endl; int tmpPath = minPath(board, locationPair(start.first - b, start.second + a), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path-+ = " << path << endl; } } if (start.first - b >= 0 && start.second - a >= 0) { //cout << "start = " << start.first << "," << start.second << endl; //cout << "next check = " << start.first-a << "," << start.second-b << endl; int tmpPath = minPath(board, locationPair(start.first - b, start.second - a), end, a, b, posMin(path, currentMin -1)); if (path == -1 || (tmpPath < path && tmpPath >= 0)) { path = tmpPath; //cout << "path-- = " << path << endl; } } return (path == -1) ? -1 : path + 1; } int main(){ int n; cin >> n; for(int i = 1; i < n; ++i) { for (int j = 1; j < n; ++j) { int path = minPath(vector< vector >(n, vector(n,0)), locationPair(0, 0), locationPair(n-1, n-1), i, j, -1); cout << path << " "; } cout << endl; } return 0; }