-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1298. Knight.c
97 lines (83 loc) · 2.08 KB
/
1298. Knight.c
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
#include <stdio.h>
#define n 64
int dx[8]={1,2,2,1,-1,-2,-2,-1};
int dy[8]={2,1,-1,-2,-2,-1,1,2};
int a[n][n]={0};
int N;
int Find(int x, int y);
int Cal(int x, int y, int k);
int main( void )
{
int i, j;
int x, y;
scanf("%d", &N);
if (N == 1)
{
printf("a1");
return 0;
}
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
a[i][j]=1;
if (Cal(i,j, 2))
{
x = i;
y = j;
while (1)
{
printf("%c%d\n", 97+y, x+1);
if (a[x][y] == N*N) return 0;
for (i = 0; i < 8; i++)
if ((x+dx[i]>=0) && (x+dx[i]<N) &&
(y+dy[i]>=0) && (y+dy[i]<N) &&
(a[x+dx[i]][y+dy[i]] == a[x][y] + 1))
{
x = x + dx[i];
y = y + dy[i];
break;
}
}
}
for (x = 0; x < N; x++)
for (y = 0; y < N; y++)
a[x][y] = 0;
}
printf("IMPOSSIBLE");
return 0;
}
int Find(int x, int y)
{
int f,i;
int flag;
f=0;
for (i=0; i<8; i++)
if ((x+dx[i]>=0) && (x+dx[i]<N) &&
(y+dy[i]>=0) && (y+dy[i]<N) &&
(a[x+dx[i]][y+dy[i]]==0)) f++;
return f;
}
int Cal(int x, int y, int k)
{
int min,rx,ry,i,rez;
int flag = 0;
min=9;
if (k>N*N) return 1;
for (i=0; i<8; i++)
if ((x+dx[i]>=0) && (x+dx[i]<N) &&
(y+dy[i]>=0) && (y+dy[i]<N) &&
(a[x+dx[i]][y+dy[i]]==0))
{
flag = 1;
rez=Find(x+dx[i],y+dy[i]);
if (rez<min)
{
min=rez;
rx=x+dx[i];
ry=y+dy[i];
}
}
if (!flag) return 0;
a[rx][ry]=k;
return Cal(rx,ry,k+1);
}