[PKU 1915 2243] 搜索之BFS & A*(续)

{

其实 能用BFS和A*解决的搜索问题很多

比如有一个很simple的骑士巡游问题

就可以这么做

本文只是贴个代码 狗尾续貂 扩充一下

以后有类似的问题也会在这篇文章里补充

}

原题 http://acm.pku.edu.cn/JudgeOnline/problem?id=1915

  //2243与这个题基本类似 注意读入输出的细节

用裸BFS 双向BFS A*皆可

如果用A*

应该自己试着去构造一些启发函数

多多尝试 总有一个最好的

我用的就是很简单的曼哈顿距离作启发

效果不错 值得推荐

但不否认有更好的启发函数 下面贴代码

裸BFS

 

BFS
   
     
1 const maxq = 100000 ;
2 maxs = 300 ;
3 dx: array [ 1 .. 8 ] of longint = ( 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 );
4 dy: array [ 1 .. 8 ] of longint = ( 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 );
5   var n,s,i,gx,gy,t,h,tx,ty:longint;
6 x,y,dep: array [ 1 ..maxq] of longint;
7 hash: array [ 0 ..maxs - 1 , 0 ..maxs - 1 ] of longint;
8   procedure main;
9   var i:longint;
10   begin
11 readln(s);
12 readln(x[ 1 ],y[ 1 ]);
13 readln(gx,gy);
14   if (x[ 1 ] = gx) and (y[ 1 ] = gy)
15 then begin
16 writeln( 0 );
17 exit;
18 end ;
19 fillchar(hash,sizeof(hash), 0 );
20 hash[x[ 1 ],y[ 1 ]]: = 1 ;
21 t: = 1 ; h: = 1 ; dep[ 0 ]: = 0 ;
22   while h <= t do
23 begin
24 for i: = 1 to 8 do
25 begin
26 tx: = x[h] + dx[i];
27 ty: = y[h] + dy[i];
28 if (tx = gx) and (ty = gy)
29 then begin
30 writeln(dep[h] + 1 );
31 exit;
32 end ;
33 if (tx >= 0 ) and (tx <= s - 1 ) and (ty >= 0 ) and (ty <= s - 1 ) and (hash[tx,ty] = 0 )
34 then begin
35 hash[tx,ty]: = 1 ;
36 inc(t);
37 dep[t]: = dep[h] + 1 ;
38 x[t]: = tx; y[t]: = ty;
39 end ;
40 end ;
41 inc(h);
42 end ;
43   end ;
44   begin
45 assign(input, ' knight.in ' ); reset(input);
46 assign(output, ' knight.out ' ); rewrite(output);
47 readln(n);
48   for i: = 1 to n do main;
49 close(input); close(output);
50 end .
51

双向BFS

 

 

BFS-双向
   
     
1 const maxq = 50000 ;
2 maxs = 300 ;
3 dx: array [ 1 .. 8 ] of longint = ( 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 );
4 dy: array [ 1 .. 8 ] of longint = ( 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 );
5 var x,y,dep: array [ 1 ..maxq, 1 .. 2 ] of longint;
6 hash,kind: array [ 0 ..maxs - 1 , 0 ..maxs - 1 ] of longint;
7 n,s,i,h1,t1,h2,t2,tx,ty:longint;
8 procedure main;
9 var i:longint;
10 begin
11 readln(s);
12 readln(x[ 1 , 1 ],y[ 1 , 1 ]);
13 readln(x[ 1 , 2 ],y[ 1 , 2 ]);
14 if (x[ 1 , 1 ] = x[ 1 , 2 ]) and (y[ 1 , 1 ] = y[ 1 , 2 ])
15 then begin
16 writeln( 0 );
17 exit;
18 end ;
19 fillchar(hash,sizeof(hash), 0 );
20 hash[x[ 1 , 1 ],y[ 1 , 1 ]]: = 1 ; hash[x[ 1 , 2 ],y[ 1 , 2 ]]: = 1 ;
21 kind[x[ 1 , 1 ],y[ 1 , 1 ]]: = 1 ; kind[x[ 1 , 2 ],y[ 1 , 2 ]]: = 2 ;
22 h1: = 1 ; t1: = 1 ; h2: = 1 ; t2: = 1 ;
23 dep[ 1 , 1 ]: = 0 ; dep[ 1 , 2 ]: = 0 ;
24 while (h1 <= t1) and (h2 <= t2) do
25 begin
26 if dep[t1, 1 ] < dep[t2, 2 ]
27 then begin
28 for i: = 1 to 8 do
29 begin
30 tx: = x[h1, 1 ] + dx[i];
31 ty: = y[h1, 1 ] + dy[i];
32 if (tx < 0 ) or (tx > s - 1 ) or (ty < 0 ) or (ty > s - 1 ) then continue;
33 if hash[tx,ty] <> 0
34 then begin
35 if kind[tx,ty] = 2
36 then begin
37 writeln(dep[h1, 1 ] + 1 + dep[hash[tx,ty], 2 ]);
38 exit;
39 end ;
40 continue;
41 end ;
42 inc(t1);
43 x[t1, 1 ]: = tx; y[t1, 1 ]: = ty;
44 hash[tx,ty]: = t1;
45 kind[tx,ty]: = 1 ;
46 dep[t1, 1 ]: = dep[h1, 1 ] + 1 ;
47 end ;
48 inc(h1);
49 end
50 else begin
51 for i: = 1 to 8 do
52 begin
53 tx: = x[h2, 2 ] + dx[i];
54 ty: = y[h2, 2 ] + dy[i];
55 if (tx < 0 ) or (tx > s - 1 ) or (ty < 0 ) or (ty > s - 1 ) then continue;
56 if hash[tx,ty] <> 0
57 then begin
58 if kind[tx,ty] = 1
59 then begin
60 writeln(dep[h2, 2 ] + 1 + dep[hash[tx,ty], 1 ]);
61 exit;
62 end ;
63 continue;
64 end ;
65 inc(t2);
66 x[t2, 2 ]: = tx; y[t2, 2 ]: = ty;
67 hash[tx,ty]: = t2;
68 kind[tx,ty]: = 2 ;
69 dep[t2, 2 ]: = dep[h2, 2 ] + 1 ;
70 end ;
71 inc(h2);
72 end ;
73 end ;
74 end ;
75 begin
76 assign(input, ' knight.in ' ); reset(input);
77 assign(output, ' knight.out ' ); rewrite(output);
78 readln(n);
79 for i: = 1 to n do main;
80 close(input); close(output);
81 end .
82

A*

 

 

A*
   
     
1 const maxq = 100000 ;
2 maxs = 300 ;
3 dx: array [ 1 .. 8 ] of longint = ( 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 );
4 dy: array [ 1 .. 8 ] of longint = ( 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 );
5 var x,y,dep,h,f: array [ 1 ..maxq] of longint;
6 hash: array [ 0 ..maxs - 1 , 0 ..maxs - 1 ] of longint;
7 n,s,i,gx,gy,hs,qs,tx,ty:longint;
8 procedure main;
9 var i,j,now,temp:longint;
10 begin
11 readln(s);
12 readln(x[ 1 ],y[ 1 ]);
13 readln(gx,gy);
14 if (x[ 1 ] = gx) and (y[ 1 ] = gy)
15 then begin
16 writeln( 0 );
17 exit;
18 end ;
19 fillchar(hash,sizeof(hash), 0 );
20 hash[x[ 1 ],y[ 1 ]]: = 1 ;
21 hs: = 1 ; h[ 1 ]: = 1 ; f[ 1 ]: = (abs(gx - x[ 1 ]) + abs(gy - y[ 1 ])) div 3 ;
22 qs: = 1 ; dep[ 1 ]: = 0 ;
23 while hs > 0 do
24 begin
25 now: = h[ 1 ];
26 if (x[now] = gx) and (y[now] = gy)
27 then begin
28 writeln(dep[now]);
29 exit;
30 end ;
31 h[ 1 ]: = h[hs]; f[ 1 ]: = f[hs];
32 dec(hs);
33 i: = 1 ;
34 while true do
35 begin
36 if i shl 1 > hs then break;
37 if (i shl 1 + 1 > hs) or (f[i shl 1 ] < f[i shl 1 + 1 ])
38 then begin
39 if f[i shl 1 ] < f[i]
40 then begin
41 temp: = h[i shl 1 ]; h[i shl 1 ]: = h[i]; h[i]: = temp;
42 temp: = f[i shl 1 ]; f[i shl 1 ]: = f[i]; f[i]: = temp;
43 i: = i shl 1 ;
44 end
45 else break;
46 end
47 else begin
48 if f[i shl 1 + 1 ] < f[i]
49 then begin
50 temp: = h[i shl 1 + 1 ]; h[i shl 1 + 1 ]: = h[i]; h[i]: = temp;
51 temp: = f[i shl 1 + 1 ]; f[i shl 1 + 1 ]: = f[i]; f[i]: = temp;
52 i: = i shl 1 + 1 ;
53 end
54 else break;
55 end ;
56 end ;
57 for i: = 1 to 8 do
58 begin
59 tx: = x[now] + dx[i];
60 ty: = y[now] + dy[i];
61 if (tx < 0 ) or (tx > s - 1 ) or (ty < 0 ) or (ty > s - 1 ) or (hash[tx,ty] > 0 ) and (dep[hash[tx,ty]] <= dep[now] + 1 ) then continue;
62 inc(qs);
63 x[qs]: = tx; y[qs]: = ty;
64 dep[qs]: = dep[now] + 1 ;
65 hash[tx,ty]: = qs;
66 inc(hs);
67 h[hs]: = qs;
68 f[hs]: = dep[qs] + (abs(gx - x[qs]) + abs(gy - y[qs])) div 3 ;
69 j: = hs;
70 while j > 1 do
71 begin
72 if f[j shr 1 ] > f[j]
73 then begin
74 temp: = h[j shr 1 ]; h[j shr 1 ]: = h[j]; h[j]: = temp;
75 temp: = f[j shr 1 ]; f[j shr 1 ]: = f[j]; f[j]: = temp;
76 j: = j shr 1 ;
77 end
78 else break;
79 end ;
80 end ;
81 end ;
82 end ;
83 begin
84 assign(input, ' knight.in ' ); reset(input);
85 assign(output, ' knight0.out ' ); rewrite(output);
86 readln(n);
87 for i: = 1 to n do main;
88 close(input); close(output);
89 end .
90

 

(未完待续)

 

 

你可能感兴趣的:(bfs)