BFS+位运算38行AC!

没有hash,没有链表,BFS+位运算38行通过。

ps:BFS要从目标状态往初始状态搜。

var f:array[0..33554432] of word;
h,s :array[0..244580] of longint;
i,j,k,n,now,top,new,a :longint;
ch :char;
begin
readln(n);
fillchar(f,sizeof(f),0); fillchar(s,sizeof(s),0);
h[1]:=1<<25-1; now:=0; top:=1;
while now<top do begin
inc(now);
if s[now]+1<7 then for i:=1 to 5 do for j:=1 to 5 do begin
new:=h[now] xor(1<<((i-1)*5+j-1));
if i>1 then new:=new xor (1<<((i-2)*5+j-1));
if i<5 then new:=new xor (1<<((i-0)*5+j-1));
if j>1 then new:=new xor (1<<((i-1)*5+j-2));
if j<5 then new:=new xor (1<<((i-1)*5+j ));
if f[new]=0 then begin
inc(top);
h[top]:=new;
s[top]:=s[now]+1;
f[new]:=s[top];
end;
end;
end;
for k:=1 to n do begin
a:=0;
for i:=1 to 5 do begin
for j:=1 to 5 do begin
read(ch);
a:=(a or (ord(ch)-48))<<1;
end;
readln;
end;
if k<n then readln;
a:=a>>1;
if a=h[1] then writeln(0) else if f[a]>0 then writeln(f[a]) else writeln(-1) ;
end;

end.

上海红茶馆 via libjudge
编译成功
测试数据 #0: Accepted, time = 182 ms, mem = 68212 KiB, score = 10
测试数据 #1: Accepted, time = 211 ms, mem = 68212 KiB, score = 10
测试数据 #2: Accepted, time = 168 ms, mem = 68212 KiB, score = 10
测试数据 #3: Accepted, time = 211 ms, mem = 68212 KiB, score = 10
测试数据 #4: Accepted, time = 182 ms, mem = 68212 KiB, score = 10
测试数据 #5: Accepted, time = 211 ms, mem = 68212 KiB, score = 10
测试数据 #6: Accepted, time = 182 ms, mem = 68212 KiB, score = 10
测试数据 #7: Accepted, time = 211 ms, mem = 68212 KiB, score = 10
测试数据 #8: Accepted, time = 167 ms, mem = 68212 KiB, score = 10
测试数据 #9: Accepted, time = 211 ms, mem = 68212 KiB, score = 10
Summary: Accepted, time = 1936 ms, mem = 68212 KiB, score = 100

7 条评论

  • @ 2016-03-01 19:44:22
    // input code here
    
  • @ 2013-04-04 23:57:02

    要不要试着写段小代码在粘贴时判断一下,提示用户要不要高亮.

    • @ 2013-04-06 09:10:54

      干脆变为自动代码块算了。。。。

    • @ 2013-04-06 17:52:11

      我觉得不错(抠鼻)

  • @ 2013-04-04 21:41:45

    var f:array[0..33554432] of word;
    h,s :array[0..244580] of longint;
    i,j,k,n,now,top,new,a :longint;
    ch :char;
    begin
    readln(n);
    fillchar(f,sizeof(f),0); fillchar(s,sizeof(s),0);
    h[1]:=1<<25-1; now:=0; top:=1;
    while now<top do begin
    inc(now);
    if s[now]+1<7 then for i:=1 to 5 do for j:=1 to 5 do begin
    new:=h[now] xor(1<<((i-1)*5+j-1));
    if i>1 then new:=new xor (1<<((i-2)*5+j-1));
    if i<5 then new:=new xor (1<<((i-0)*5+j-1));
    if j>1 then new:=new xor (1<<((i-1)*5+j-2));
    if j<5 then new:=new xor (1<<((i-1)*5+j ));
    if f[new]=0 then begin
    inc(top);
    h[top]:=new;
    s[top]:=s[now]+1;
    f[new]:=s[top];
    end;
    end;
    end;

    for k:=1 to n do begin
    a:=0;
    for i:=1 to 5 do begin
    for j:=1 to 5 do begin
    read(ch);
    a:=(a or (ord(ch)-48))<<1;
    end;
    readln;
    end;
    if k<n then readln;
    a:=a>>1;
    if a=h[1] then writeln(0) else if f[a]>0 then writeln(f[a]) else writeln(-1) ;
    end;
    end.

  • @ 2013-04-02 15:50:32

    我会告诉你我有30行的AC代码 吗?
    var f:array[0..33554432] of word;
    h,s :array[0..244580] of longint;
    i,j,k,n,now,top,new,a :longint;ch :char;beginreadln(n);fillchar(f,sizeof(f),0); fillchar(s,sizeof(s),0);h[1]:=1<<25-1; now:=0; top:=1; while now<top do begininc(now);if s[now]+1<7 then for i:=1 to 5 do for j:=1 to 5 do begin
    new:=h[now] xor(1<<((i-1)*5+j-1));
    if i>1 then new:=new xor (1<<((i-2)*5+j-1));
    if i<5 then new:=new xor (1<<((i-0)*5+j-1));
    if j>1 then new:=new xor (1<<((i-1)*5+j-2));
    if j<5 then new:=new xor (1<<((i-1)*5+j ));
    if f[new]=0 then begin
    inc(top);
    h[top]:=new;
    s[top]:=s[now]+1;
    f[new]:=s[top];
    end;
    end;
    end;

    for k:=1 to n do begin
    a:=0;
    for i:=1 to 5 do begin
    for j:=1 to 5 do begin
    read(ch);
    a:=(a or (ord(ch)-48))<<1;
    end;
    readln;
    end;
    if k<n then readln;
    a:=a>>1;
    if a=h[1] then writeln(0) else if f[a]>0 then writeln(f[a]) else writeln(-1) ;
    end;

    end.

  • @ 2013-03-24 17:43:11

    你需要高亮代码。
    # include <iostream>
    using namespace std;
    int main()
    {
    return 0;
    }

  • @ 2013-03-24 11:48:17

    怎么高亮?

    • @ 2013-03-24 17:30:09

      选中代码按tab。

  • @ 2013-03-17 16:03:23

    Orz……话说没高亮的帖子不是好帖子 ;D

  • 1

信息

ID
1197
难度
6
分类
搜索 点击显示
标签
(无)
递交数
1684
已通过
508
通过率
30%
被复制
6
上传者