Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 404 Solved: 118
Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼。这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方形格子的 。某些格子上有惊人的坚固的莲花,还有一些岩石,其余的只是美丽,纯净,湛蓝的水。 贝茜正在练习芭蕾舞,她从一个莲花跳跃到另一个莲花,当前位于一个莲花。她希望在莲花上一个一个的跳,目标是另一个给定莲花。她能跳既不入水,也不到一个岩石上。 令门外汉惊讶的是,贝茜的每次的跳跃像中国象棋的马一样:横向移动1,纵向移动2,或纵向移动1,横向移动2。贝茜有时可能会有多达8个选择的跳跃。 Farmer John 在观察贝茜的芭蕾舞联系,他意识到有时候贝茜有可能跳不到她想去的目的地,因为路上有些地方没有莲花。于是他想要添加几个莲花使贝茜能够完成任务。一贯节俭的Farmer John想添加最少数量的莲花。当然,莲花不能放在石头上。 请帮助Farmer John确定必须要添加的莲花的最少数量。在添加的莲花最少基础上,算出贝茜从起始点跳到目标点需要的最少的步数。最后,还要算出满足添加的莲花的最少数量时,跳跃最少步数的跳跃路径的条数。
第 1 行: 两个整数 M , N
第 2..M + 1 行:第 i + 1 行,第 i + 1 行 有 N 个整数,表示该位置的状态: 0 为水; 1 为莲花; 2 为岩石; 3 为贝茜开始的位置; 4 为贝茜要去的目标位置.
第 1 行: 一个整数: 需要添加的最少的莲花数. 如果无论如何贝茜也无法跳到,输出 -1.
第 2 行: 一个整数: 在添加的莲花最少基础上,贝茜从起始点跳到目标点需要的最少的步数。如果第1行输出-1,这行不输出。 第 3 行: 一个整数: 添加的莲花的最少数量时,跳跃步数为第2行输出的值的跳跃路径的条数 如果第1行输出-1,这行不输出。
4 8 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 4 0 0 3 0 0 0 0 0 1 0
2 6 2 输出说明 至少要添加2朵莲花,放在了'x'的位置。 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 x 0 0 0 2 0 1 0 0 0 0 0 2 0 1 0 0 0 0 x 4 0 0 0 0 x 0 x 4 0 0 3 0 0 0 0 0 1 0 3 0 0 0 0 0 1 0 贝茜至少要条6步,有以下两种方案 0 0 0 C 0 0 0 0 0 0 0 C 0 0 0 0 0 B 0 0 0 2 0 F 0 0 0 0 0 2 0 F 0 0 0 0 D G 0 0 0 0 B 0 D G 0 0 A 0 0 0 0 0 E 0 A 0 0 0 0 0 E 0
题解:一道很萌的深搜题,其实道理也很简单,就是在搜索最优方案的时候顺带记录比对下总步数,以及方案数量即可。。。
居然神奇地拿了Rank5,估计很快就被虐下去的TT
1 /**************************************************************
2 Problem: 1632
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:0 ms
7 Memory:376 kb
8 ****************************************************************/
9
10 const
11 xt:array[1..8] of longint=(1,1,-1,-1,2,2,-2,-2);
12 yt:array[1..8] of longint=(2,-2,2,-2,1,-1,1,-1);
13 var
14 i,j,k,l,m,n,ans,f,r,ex,ey:longint;
15 x,y:array[0..10010] of longint;
16 a,b,c,d:array[0..60,0..60] of longint;
17 e:array[0..40,0..40] of int64;
18 procedure bfs;inline;
19 var nx,ny,ty,tx,i,j,k,l,ta:longint;
20 begin
21
22 while f<>r do
23 begin
24 nx:=x[f];ny:=y[f];inc(f);
25 for k:=1 to 8 do
26 begin
27 tx:=nx+xt[k];ty:=ny+yt[k];
28 if (tx<1) or (ty<1) or (tx>n) or (ty>m) or (a[tx,ty]=2) then continue;
29 ta:=b[nx,ny]+longint(a[tx,ty]=0);
30 if ta<b[tx,ty] then
31 begin
32 b[tx,ty]:=ta;
33 c[tx,ty]:=c[nx,ny]+1;
34 e[tx,ty]:=e[nx,ny];
35 if d[tx,ty]<>0 then continue;
36 d[tx,ty]:=1;x[r]:=tx;
37 y[r]:=ty;inc(r);
38 end
39 else
40 begin
41 if ta=b[tx,ty] then
42 begin
43 if (c[nx,ny]+1)<c[tx,ty] then
44 begin
45 c[tx,ty]:=c[nx,ny]+1;
46 e[tx,ty]:=e[nx,ny];
47 if d[tx,ty]<>0 then continue;
48 d[tx,ty]:=1;
49 x[r]:=tx;y[r]:=ty;
50 inc(r);
51 end
52 else
53 begin
54 if (c[nx,ny]+1)=c[tx,ty] then
55 begin
56 inc(e[tx,ty],e[nx,ny]);
57 if d[tx,ty]<>0 then continue;
58 d[tx,ty]:=1;
59 x[r]:=tx;y[r]:=ty;inc(r);
60 end;
61 end;
62 end;
63 end;
64 end;
65 d[nx,ny]:=0;
66 end;
67 end;
68 begin
69 f:=0;r:=1;
70 readln(n,m);
71 for i:=1 to n do
72 for j:=1 to m do
73 begin
74 read(a[i,j]);
75 if j=m then readln;
76 b[i,j]:=maxlongint;c[i,j]:=maxlongint;
77 if a[i,j]=3 then
78 begin
79 d[i,j]:=1;
80 x[0]:=i;y[0]:=j;b[i,j]:=0;
81 c[i,j]:=0;
82 e[i,j]:=1;
83 end
84 else
85 if a[i,j]=4 then
86 begin
87 ex:=i;ey:=j;
88 end;
89 end;
90 bfs;
91 if b[ex,ey]=maxlongint then
92 begin
93 writeln(-1);
94 halt;
95 end
96 else
97 begin
98 writeln(b[ex,ey]);
99 writeln(c[ex,ey]);
100 writeln(e[ex,ey]);
101 end;
102 end.