uva11694

本题大致意思可见紫皮书第7章习题。
我的做法比较特殊,我一开始看漏了“不会构成环”(当时没带紫皮书去机房,原因复杂),用状压DP做。后来一看样例,不对,然后在DP上加了个前继,最后暴搜确定方案并逐一判环。
状压DP,f[i,j]表示第i行状态为j是否可行,j的二进制位下0是’\’,1是’/’,然后预处理每种状态对上方与下方的度数贡献。
确定方案后用隐式图+并查集判环

const maxn=10;
type
 node=record
  data,next:longint
 end;
 point=record
  x,y:longint
 end;
var
 f:array[0..maxn,0..1 shl maxn-1] of boolean;
 h:array[0..maxn,0..1 shl maxn-1] of longint;
 d:array[0..1000000] of node;
 s,n,i,j,k,z,l,o,xb:longint;
 a,ans:array[0..maxn+1,0..maxn+1] of char;
 b,c:array[0..maxn+1,0..1 shl maxn-1] of longint;
 e:array[0..maxn] of longint;
 g:array[0..maxn+1,0..maxn+1] of point;
 t,fl:boolean;
operator <>(a,b:point)c:boolean;
begin
 c:=(a.x<>b.x)or(a.y<>b.y)
end;
procedure insert(var x:longint;y:longint);
begin
 inc(xb);
 d[xb].data:=y;
 d[xb].next:=x;
 x:=xb
end;
function gfa(x:point):point;
begin
 if x<>g[x.x,x.y] then g[x.x,x.y]:=gfa(g[x.x,x.y]);
 exit(g[x.x,x.y])
end;
function check:boolean;
var
 i,j:longint;
 u,v:point;
begin
 for i:=1 to n+1 do
  for j:=1 to n+1 do
  begin
   g[i,j].x:=i;
   g[i,j].y:=j
  end;
 for i:=1 to n do
  for j:=1 to n do
   if 1 shl (j-1) and e[i]>0 then
   begin
    u:=gfa(g[i,j+1]);
    v:=gfa(g[i+1,j]);
    if u<>v then g[u.x,u.y]:=v
     else exit(false)
   end else
   begin
    u:=gfa(g[i,j]);
    v:=gfa(g[i+1,j+1]);
    if u<>v then g[u.x,u.y]:=v
     else exit(false)
   end;
 exit(true)
end;
procedure out;
var
 k,l:longint;
begin


 for l:=1 to n do
  for k:=1 to n do
   if e[l] and (1 shl (k-1))>0 then ans[l,k]:='/'
    else ans[l,k]:='\';
 for k:=1 to n do
 begin
  for l:=1 to n do write(ans[k,l]);
  writeln
 end
end;
procedure dfs(x,y:longint);
var
 i:longint;
begin
 if x=1 then
 begin
  if check then
  begin
   out;
   fl:=true
  end;
  exit
 end;
 i:=h[x,y];
 while i>0 do
 begin
  e[x-1]:=d[i].data;
  dfs(x-1,d[i].data);
  if fl then exit;
  i:=d[i].next
 end
end;
begin
 readln(z);
 while z>0 do
 begin
  dec(z);
  readln(n);
  if n=0 then break;
  fillchar(f,sizeof(f),false);
  fillchar(h,sizeof(h),0);
  fillchar(b,sizeof(b),0);
  fillchar(c,sizeof(c),0);
  xb:=0;
  for i:=1 to n+1 do
  begin
   for j:=1 to n+1 do read(a[i,j]);
   readln
  end;
  s:=1 shl n-1;
  for i:=0 to s do
  begin
   for j:=1 to n do
    if i and (1 shl (j-1))=0 then
    begin
     inc(b[j,i]);
     inc(c[j+1,i])
    end else
    begin
     inc(b[j+1,i]);
     inc(c[j,i])
    end;
   t:=true;
   for j:=1 to n+1 do
    if a[1,j]<>'.' then if b[j,i]<>ord(a[1,j])-48 then
    begin
     t:=false;
     break
    end;
   f[1,i]:=t
  end;


  for i:=2 to n-1 do
    for j:=0 to s do
     for k:=0 to s do
      if f[i-1,k] then
      begin
       t:=true;
       for l:=1 to n+1 do
        if a[i,l]<>'.' then if ord(a[i,l])-48<>c[l,k]+b[l,j] then
        begin
         t:=false;
         break
        end;
       if t then
       begin
        f[i,j]:=true;
        insert(h[i,j],k)
       end
      end;
  fl:=false;
  for i:=0 to s do
  begin
   if i=18 then
   begin
    fl:=fl
   end;
   if f[n-1,i] then
    for j:=0 to s do
    begin
     if j=5 then
     begin
      fl:=fl
     end;
     t:=true;
     for k:=1 to n+1 do
      if (a[n,k]<>'.')and(ord(a[n,k])-48<>c[k,i]+b[k,j])or(a[n+1,k]<>'.')and
       (c[k,j]<>ord(a[n+1,k])-48) then
      begin
       t:=false;
       break
      end;
     if t then
     begin
      e[n]:=j;
      e[n-1]:=i;
      dfs(n-1,i)
     end;
     if fl then break
    end;
   if fl then break
  end
 end
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值