#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; int N; bool marked[25][25]; int myCount[25][25]; int bfs(int a, int b) { memset(&marked,0,sizeof(marked)); memset(&myCount,0,sizeof(marked)); marked[0][0] = true; queue> Q; Q.push(pair(0,0)); int minLength = 0; while(!Q.empty()) { pair e = Q.front(); Q.pop(); if(e.first == N-1 && e.second == N-1) { if(minLength == 0||minLength > myCount[e.first][e.second]) { minLength = myCount[e.first][e.second]; } } if(e.first + a < N && e.second + b < N && !marked[e.first + a][e.second + b]) { Q.push(pair(e.first + a, e.second + b)); marked[e.first + a][e.second + b] = true; myCount[e.first + a][e.second + b] = myCount[e.first][e.second] + 1; } if(e.first + a < N && e.second - b >= 0 && !marked[e.first + a][e.second - b]) { Q.push(pair(e.first + a, e.second - b)); marked[e.first + a][e.second - b] = true; myCount[e.first + a][e.second - b] = myCount[e.first][e.second] + 1; } if(e.first + b < N && e.second + a < N && !marked[e.first + b][e.second + a]) { Q.push(pair(e.first+b,e.second+a)); marked[e.first + b][e.second + a] = true; myCount[e.first + b][e.second + a] = myCount[e.first][e.second] + 1; } if(e.first + b < N && e.second - a >= 0 && !marked[e.first + b][e.second - a]) { Q.push(pair(e.first+b,e.second-a)); marked[e.first + b][e.second - a] = true; myCount[e.first + b][e.second - a] = myCount[e.first][e.second] + 1; } if(e.first - a >= 0 && e.second + b < N && !marked[e.first - a][e.second + b]) { Q.push(pair(e.first - a, e.second + b)); marked[e.first - a][e.second + b] = true; myCount[e.first - a][e.second + b] = myCount[e.first][e.second] + 1; } if(e.first - a >= 0 && e.second - b >= 0 && !marked[e.first - a][e.second - b]) { Q.push(pair(e.first-a,e.second-b)); marked[e.first - a][e.second - b] = true; myCount[e.first - a][e.second - b] = myCount[e.first][e.second] + 1; } if(e.first - b >= 0 && e.second + a < N && !marked[e.first - b][e.second + a]) { Q.push(pair(e.first - b, e.second + a)); marked[e.first - b][e.second + a] = true; myCount[e.first - b][e.second + a] = myCount[e.first][e.second] + 1; } if(e.first - b >= 0 && e.second - a >= 0 && !marked[e.first - b][e.second - a]) { Q.push(pair(e.first - b, e.second - a)); marked[e.first - b][e.second - a] = true; myCount[e.first - b][e.second - a] = myCount[e.first][e.second] + 1; } } if(minLength > 0) { return minLength; } return -1; } int main(){ cin >> N; // your code goes here int myLength = 0; for(int i = 1; i <= N; i ++) { for(int j = 1; j <= N; j ++) { if(i <= N - 1 && j <= N - 1) { int length = bfs(i,j); cout << length << " "; } } if(i <= N - 1) { cout << endl; } } return 0; }