Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 5;
- int xMove[8] = {2, 1, -1, -2, -2, -1, 1, 2};
- int yMove[8] = {1, 2, 2, 1, -1, -2, -2, -1};
- int matrix[N][N];
- bool isSafe(int x, int y) {
- return x >= 0 && y >= 0 && x <= N - 1 && y <= N-1;
- }
- int dfs(int x, int y, int cnt) {
- for(int i=0; i < 8; i++) {
- int nextX = x + xMove[i];
- int nextY = y + yMove[i];
- if(cnt == N*N) return 1;
- if(isSafe(nextX, nextY)) {
- if(matrix[nextX][nextY] == -1) {
- matrix[nextX][nextY] = cnt + 1;
- if(dfs(nextX, nextY, cnt+1) == 1)
- return 1;
- else
- matrix[nextX][nextY] = -1; // Solution 1.
- }
- }
- }
- matrix[x][y] = -1; // Solution 2.
- return 0;
- }
- int main()
- {
- int x, y; cin >> x >> y;
- for (int i = 0; i < N; i++)
- for (int j = 0; j < N; j++)
- matrix[i][j] = -1;
- matrix[0][0] = 1;
- dfs(0, 0, 1);
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- cout << matrix[i][j] << ' ';
- }
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment