求VIJOS(p1016)北京2008挂钟问题的宽搜解决方法
答案:1 悬赏:70 手机版
解决时间 2021-11-12 08:45
- 提问者网友:像風在裏
- 2021-11-11 12:42
求VIJOS(p1016)北京2008挂钟问题的宽搜解决方法
最佳答案
- 五星知识达人网友:轻熟杀无赦
- 2021-11-11 13:08
看一看我的
const
dir:array[1..9,1..9]of integer=((1,1,0,1,1,0,0,0,0),
(1,1,1,0,0,0,0,0,0),
(0,1,1,0,1,1,0,0,0),
(1,0,0,1,0,0,1,0,0),
(0,1,0,1,1,1,0,1,0),
(0,0,1,0,0,1,0,0,1),
(0,0,0,1,1,0,1,1,0),
(0,0,0,0,0,0,1,1,1),
(0,0,0,0,1,1,0,1,1));
dd:array[1..9]of longint=(1,4,16,64,256,1024,4096,16324,65536);
type
aa=array[1..9]of longint;
var
i,j,k,st,ta:longint;
que:array[0..300000,1..9]of longint;
pre:array[0..300000]of longint;
ch:array[0..300000]of longint;
f:array[0..300000]of boolean;
function deal(a:aa):longint;
var sum,i:longint;
begin
sum:=0;
for i:=1 to 9 do
sum:=sum+a[9-i+1]*dd[i];
deal:=sum;
end;
procedure out(m:longint);
begin
if m=0 then exit;
out(pre[m]);
write(ch[m],' ');
end;
procedure check;
var
i,j,k:longint;
a:aa;
begin
st:=0; ta:=0;
pre[0]:=0;
repeat
for i:=1 to 9 do
begin
for j:=1 to 9 do
a[j]:=(que[st,j]+dir[i,j]) mod 4;
k:=deal(a);
if not f[k]
then begin
f[k]:=true;
inc(ta);
que[ta]:=a;
pre[ta]:=st;
ch[ta]:=i;
end;
if f[0] then
begin
out(ta);
halt;
end;
end;
inc(st);
until st>ta;
end;
begin
for i:=1 to 9 do
read(que[0,i]);
fillchar(f,sizeof(f),false);
f[deal(que[0])]:=true;
check;
end.
const
dir:array[1..9,1..9]of integer=((1,1,0,1,1,0,0,0,0),
(1,1,1,0,0,0,0,0,0),
(0,1,1,0,1,1,0,0,0),
(1,0,0,1,0,0,1,0,0),
(0,1,0,1,1,1,0,1,0),
(0,0,1,0,0,1,0,0,1),
(0,0,0,1,1,0,1,1,0),
(0,0,0,0,0,0,1,1,1),
(0,0,0,0,1,1,0,1,1));
dd:array[1..9]of longint=(1,4,16,64,256,1024,4096,16324,65536);
type
aa=array[1..9]of longint;
var
i,j,k,st,ta:longint;
que:array[0..300000,1..9]of longint;
pre:array[0..300000]of longint;
ch:array[0..300000]of longint;
f:array[0..300000]of boolean;
function deal(a:aa):longint;
var sum,i:longint;
begin
sum:=0;
for i:=1 to 9 do
sum:=sum+a[9-i+1]*dd[i];
deal:=sum;
end;
procedure out(m:longint);
begin
if m=0 then exit;
out(pre[m]);
write(ch[m],' ');
end;
procedure check;
var
i,j,k:longint;
a:aa;
begin
st:=0; ta:=0;
pre[0]:=0;
repeat
for i:=1 to 9 do
begin
for j:=1 to 9 do
a[j]:=(que[st,j]+dir[i,j]) mod 4;
k:=deal(a);
if not f[k]
then begin
f[k]:=true;
inc(ta);
que[ta]:=a;
pre[ta]:=st;
ch[ta]:=i;
end;
if f[0] then
begin
out(ta);
halt;
end;
end;
inc(st);
until st>ta;
end;
begin
for i:=1 to 9 do
read(que[0,i]);
fillchar(f,sizeof(f),false);
f[deal(que[0])]:=true;
check;
end.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯