-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN.cpp
More file actions
112 lines (92 loc) · 2.86 KB
/
Copy pathN.cpp
File metadata and controls
112 lines (92 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
char board[8][8]; // Chess board
int flag = 0; // Flag to indicate if knight can reach destination
// Function to perform knight's tour
void knightTour(int i, int j, int count)
{
// Base cases for recursion
if (i >= 8 || j >= 8 || i < 0 || j < 0 || board[i][j] == 'o' || count < 0)
{
return;
}
board[i][j] = 'o'; // Mark current position as visited
count--; // Decrement count of remaining moves
// Recursive calls for all possible knight moves
knightTour(i + 1, j + 2, count);
knightTour(i + 1, j - 2, count);
knightTour(i - 1, j + 2, count);
knightTour(i - 1, j - 2, count);
knightTour(i + 2, j - 1, count);
knightTour(i + 2, j + 1, count);
knightTour(i - 2, j + 1, count);
knightTour(i - 2, j - 1, count);
}
// Function to check if knight can reach destination
void knightTour2(int i, int j, int count)
{
// Base cases for recursion
if (i > 8 || j > 8 || i < 0 || j < 0 || count < 0)
{
return;
}
if (board[i][j] == 'o') // If destination is reached
{
board[i][j] = 'X'; // Mark destination as visited
flag = 1; // Set flag to indicate success
return;
}
board[i][j] = 'o'; // Mark current position as visited
count--; // Decrement count of remaining moves
// Recursive calls for all possible knight moves
knightTour2(i + 1, j + 2, count);
knightTour2(i + 1, j - 2, count);
knightTour2(i - 1, j + 2, count);
knightTour2(i - 1, j - 2, count);
knightTour2(i + 2, j - 1, count);
knightTour2(i + 2, j + 1, count);
knightTour2(i - 2, j + 1, count);
knightTour2(i - 2, j - 1, count);
}
int main()
{
int tc;
scanf("%d", &tc);
getchar();
for (int cases = 1; cases <= tc; cases++)
{
int totalMoves;
scanf("%d", &totalMoves);
getchar();
char startX, destX;
int startY, destY;
scanf("%c%d %c%d", &startX, &startY, &destX, &destY);
getchar();
startY--; // Convert to 0-based indexing
destY--; // Convert to 0-based indexing
startX = startX - 'A'; // Convert to 0-based indexing
destX = destX - 'A'; // Convert to 0-based indexing
// Initialize chess board
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
board[i][j] = '.';
}
}
printf("Case #%d: ", cases);
// Perform knight's tour from starting position
knightTour(startX, startY, totalMoves);
// Check if knight can reach destination
knightTour2(destX, destY, totalMoves);
if (flag == 1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
flag = 0; // Reset flag for next test case
}
return 0;
}