这一题除了可以用f[k,i,j]表示第k步能否达到[i,j]点外没有什么好说的......直接上代码......
program P1026;
const
{1:NORTH,2:SOUTH,3:WEST,4:EAST}
dx:array[1..4] of longint=(-1,1,0,0);
dy:array[1..4] of longint=(0,0,-1,1);
var
Direct:array[0..1000] of longint;
map:array[0..51,0..51] of boolean;
position:array[1..1001,0..51,0..51] of boolean;
n,m,Step_Num:longint;
procedure ScreenInput;//读入地图及逃跑方向
var
i,j,x,y:longint;
c:char;
s:string;
begin
readln(n,m);
for i:=0 to 51 do
for j:=0 to 51 do
begin
map[i,j]:=false;
position[1,i,j]:=false;
end;
for i:=1 to n do
begin
for j:=1 to m do
begin
read(c);
if c='X' then map[i,j]:=false;
if c='.' then map[i,j]:=true;
if c='*' then
begin
x:=i;y:=j;
map[i,j]:=true;
end;
end;
readln;
end;
readln(Step_Num);
for i:=1 to Step_Num do
begin
readln(s);
if s='NORTH' then Direct[i]:=1;
if s='SOUTH' then Direct[i]:=2;
if s='WEST' then Direct[i]:=3;
if s='EAST' then Direct[i]:=4;
end;
position[1,x,y]:=true;
end;
procedure print(k:longint);//输出处理后的地图
var
i,j:longint;
ScreenOut:array[1..50,1..50] of char;
begin
for i:=1 to n do
for j:=1 to m do
if position[k,i,j] then ScreenOut[i,j]:='*'
else ScreenOut[i,j]:='+';//将当前还没有处理过的标记为'+'
for i:=1 to n do
for j:=1 to m do
if ScreenOut[i,j]='+' then
begin
if map[i,j] then ScreenOut[i,j]:='.' else ScreenOut[i,j]:='X';//将原地图与现地图没有处理过的地方重叠
end;
for i:=1 to n do//输出处理过的地图
begin
for j:=1 to m do write(ScreenOut[i,j]);
writeln;
end;
end;
procedure work(k:longint);
var
i,j,x,y:longint;
begin
if k>Step_Num then
begin
print(k);
exit;
end;
for i:=1 to n do
for j:=1 to m do
if position[k,i,j] then
begin
x:=i+dx[Direct[k]];y:=j+dy[Direct[k]];
while map[x,y] do
begin
position[k+1,x,y]:=true;//下一步能达到该位置
x:=x+dx[Direct[k]];y:=y+dy[Direct[k]];//拓展下一种状态
end;
end;
work(k+1);
end;
begin
ScreenInput;
work(1);
end.