10477 - The Hybrid Knight
Moderator: Board moderators
10477
const
mv: array[0..2, 1..8, 1..2]of longint =
(
((-2, -1), (-2, 1), (-1, 2), (1, 2),
(2, 1), (2, -1), (1, -2), (-1, -2)),
((-3, -1), (-3, 1), (-1, 3), (1, 3),
(3, 1), (3, -1), (1, -3), (-1, -3)),
((-1, 0), (1, 0), (0, -1), (0, 1),
(-1, -1), (-1, 1), (1, -1), (1, 1))
);
err = $7fffffff;
type
TMap = array[0..20 * 20 - 1, 0..2]of longint;
var
n: longint;
q: array[1..20 * 20 * 3 + 2, 1..2]of longint;
h, t: longint;
procedure travel(x0, y0: longint; var map: TMap);
var
i, j, k: longint;
begin
for i := 0 to n * n - 1 do
for k := 0 to 2 do
map[i, k] := err;
for k := 0 to 2 do
begin
map[x0 * n + y0, k] := 0;
q[k + 1, 1] := x0 * n + y0;
q[k + 1, 2] := k;
end;
h := 1; t := 3;
repeat
x0 := q[h, 1] div n;
y0 := q[h, 1] mod n;
for k := 1 to 8 do
begin
i := x0 + mv[q[h, 2], k, 1];
j := y0 + mv[q[h, 2], k, 2];
if(i < 0)or(i >= n)or(j < 0)or(j >= n)then continue;
i := i * n + j;
j := (q[h, 2] + 1) mod 3;
if map[i, j] < err then continue;
map[i, j] := map[q[h, 1], q[h, 2]] + 1;
inc(t);
q[t, 1] := i;
q[t, 2] := j;
end;
inc(h);
until h > t;
end;
var
dist: array[0..20 * 20 - 1, 0..20 * 20 - 1]of longint;
map: TMap;
i, j, k, min: longint;
dataid: longint;
begin
dataid := 0;
repeat
read(n);
if n < 4 then break;
inc(dataid);
writeln('Set ', dataid, ':');
fillchar(dist, sizeof(dist), 0);
for i := 0 to n * n - 1 do
begin
travel(i div n, i mod n, map);
for j := 0 to n * n - 1 do
begin
min := err;
for k := 0 to 2 do
if map[j, k] < min then
min := map[j, k];
dist[i, j] := min;
end;
end;
read(k);
while k > 0 do
begin
read(i, j);
dec(i); dec(j);
if dist[i, j] < err then writeln(dist[i, j])
else writeln('?');
dec(k);
end;
until false;
end.
mv: array[0..2, 1..8, 1..2]of longint =
(
((-2, -1), (-2, 1), (-1, 2), (1, 2),
(2, 1), (2, -1), (1, -2), (-1, -2)),
((-3, -1), (-3, 1), (-1, 3), (1, 3),
(3, 1), (3, -1), (1, -3), (-1, -3)),
((-1, 0), (1, 0), (0, -1), (0, 1),
(-1, -1), (-1, 1), (1, -1), (1, 1))
);
err = $7fffffff;
type
TMap = array[0..20 * 20 - 1, 0..2]of longint;
var
n: longint;
q: array[1..20 * 20 * 3 + 2, 1..2]of longint;
h, t: longint;
procedure travel(x0, y0: longint; var map: TMap);
var
i, j, k: longint;
begin
for i := 0 to n * n - 1 do
for k := 0 to 2 do
map[i, k] := err;
for k := 0 to 2 do
begin
map[x0 * n + y0, k] := 0;
q[k + 1, 1] := x0 * n + y0;
q[k + 1, 2] := k;
end;
h := 1; t := 3;
repeat
x0 := q[h, 1] div n;
y0 := q[h, 1] mod n;
for k := 1 to 8 do
begin
i := x0 + mv[q[h, 2], k, 1];
j := y0 + mv[q[h, 2], k, 2];
if(i < 0)or(i >= n)or(j < 0)or(j >= n)then continue;
i := i * n + j;
j := (q[h, 2] + 1) mod 3;
if map[i, j] < err then continue;
map[i, j] := map[q[h, 1], q[h, 2]] + 1;
inc(t);
q[t, 1] := i;
q[t, 2] := j;
end;
inc(h);
until h > t;
end;
var
dist: array[0..20 * 20 - 1, 0..20 * 20 - 1]of longint;
map: TMap;
i, j, k, min: longint;
dataid: longint;
begin
dataid := 0;
repeat
read(n);
if n < 4 then break;
inc(dataid);
writeln('Set ', dataid, ':');
fillchar(dist, sizeof(dist), 0);
for i := 0 to n * n - 1 do
begin
travel(i div n, i mod n, map);
for j := 0 to n * n - 1 do
begin
min := err;
for k := 0 to 2 do
if map[j, k] < min then
min := map[j, k];
dist[i, j] := min;
end;
end;
read(k);
while k > 0 do
begin
read(i, j);
dec(i); dec(j);
if dist[i, j] < err then writeln(dist[i, j])
else writeln('?');
dec(k);
end;
until false;
end.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
Try:
Your answer: 4
Correct answer: 5
I'm not gonna look into your code. If you realy expect that anyone does, then don't make it too difficult for them:
- Use the formatting tags,
- Use descriptive names for your variables and put in some comment.
You're a Pascal programmer, not a C programmer, so there's no reason to make it hard on yourself and others!
Code: Select all
20 1
1 10
0 1
Correct answer: 5
I'm not gonna look into your code. If you realy expect that anyone does, then don't make it too difficult for them:
- Use the formatting tags,
- Use descriptive names for your variables and put in some comment.
You're a Pascal programmer, not a C programmer, so there's no reason to make it hard on yourself and others!
-
- New poster
- Posts: 6
- Joined: Thu Apr 17, 2003 1:06 pm
10477
Hello, i solve the problem 10477. but i got TLE.
Who can i overcome it.
May be my algorithm is Bad. Is their anybody who provide me a good algorithm ?
Who can i overcome it.

May be my algorithm is Bad. Is their anybody who provide me a good algorithm ?
-
- New poster
- Posts: 6
- Joined: Thu Apr 17, 2003 1:06 pm
So far as I understand even one DFS per query would get TLE. I used BFS to get single source shortest path and filled up a table which contains all pair shortest path.
However, the coding was optimized by doing BFS from one fourth of the source vertices. We can use the symmetry of the board if we know the all single source shortest paths for one quadrant of a board.
Think about it.
However, the coding was optimized by doing BFS from one fourth of the source vertices. We can use the symmetry of the board if we know the all single source shortest paths for one quadrant of a board.
Think about it.
K M Hasan
http://www.cs.umanitoba.ca/~kmhasan/
http://www.cs.umanitoba.ca/~kmhasan/
-
- Experienced poster
- Posts: 187
- Joined: Wed Dec 11, 2002 2:03 pm
- Location: Mount Papandayan, Garut
10477 - hybrid knight
hi...
i use BFS, but got WA. however what i'm confuse is in this statement :
thanks before,
regards,
titid gede
i use BFS, but got WA. however what i'm confuse is in this statement :
here, what if the meaning of final move?However if the final move is made with the mutant pawn, the pawn has options to use its attacking move
thanks before,
regards,
titid gede
Kalo mau kaya, buat apa sekolah?
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
-
- Experienced poster
- Posts: 128
- Joined: Fri Nov 15, 2002 7:45 am
- Location: Kyrgyzstan
-
- Experienced poster
- Posts: 187
- Joined: Wed Dec 11, 2002 2:03 pm
- Location: Mount Papandayan, Garut
-
- Experienced poster
- Posts: 128
- Joined: Fri Nov 15, 2002 7:45 am
- Location: Kyrgyzstan
Hi!
In my program I enqueued only one state but that was a special state - there were three possibilities to go from it to usual states.
By the way what if your program will be given a test where a pawn can use its attacking possibility on the first move? Did you handle this properly?
Good luck!
Andrey.
In my program I enqueued only one state but that was a special state - there were three possibilities to go from it to usual states.
By the way what if your program will be given a test where a pawn can use its attacking possibility on the first move? Did you handle this properly?
Good luck!
Andrey.