【USACO题库】2.2.4 Party Lamps派对灯 (Standard IO)
时间限制: 1000 ms 空间限制: 262144 KB 具体限制简单的dfs,只需要枚举每种情况,最后在判断即可,但是有一个优化:
不会有C次,最多8次,优化为:
if c>8 then代码为:
begin
if odd(c) then
c:=5
else
c:=4;
end;
var大神的方法为:
n,c,i,l,j:longint;
a,b:array[0..1000]of longint;
ans:array[0..10000]of string;
s:string;
procedure does(n1:longint;var s:string);
var
i:longint;
begin
if n1=1 then
begin
for i:=1 to length(s) do
if s[i]='1' then s[i]:='0'
else s[i]:='1';
exit;
end;
if n1=2 then
begin
for i:=1 to length(s) do
if i mod 2=1 then
begin
if s[i]='1' then s[i]:='0'
else s[i]:='1';
end;
exit;
end;
if n1=3 then
begin
for i:=1 to length(s) do
if i mod 2=0 then
begin
if s[i]='1' then s[i]:='0'
else s[i]:='1';
end;
exit;
end;
if n1=4 then
begin
for i:=0 to length(s) div 2 do
if s[3*i+1]='1' then s[3*i+1]:='0'
else s[3*i+1]:='1';
if length(s)>n then delete(s,length(s),1);
exit;
end;
end;
function pd(s:string):boolean;
var
i:longint;
begin
for i:=1 to 100 do
begin
if a[i]=-1 then break;
if s[a[i]]='0' then exit(false);
end;
for i:=1 to 100 do
begin
if b[i]=-1 then break;
if s[b[i]]='1' then exit(false);
end;
exit(true);
end;
function pd1(s:string;l:longint):boolean;
var
i:longint;
begin
for i:=1 to l do
if ans[i]=s then exit(false);
exit(true);
end;
procedure dg(s:string;t:longint);
var
i:longint;
k:int64;
begin
if t=c then
begin
if pd(s) and pd1(s,l) then
begin
inc(l);
ans[l]:=s;
end;
exit;
end;
for i:=1 to 4 do
begin
does(i,s);
dg(s,t+1);
does(i,s);
end;
end;
begin
readln(n,c);
if c>8 then
begin
if odd(c) then
c:=5
else
c:=4;
end;
for i:=1 to 100 do
begin
read(a[i]);
if a[i]=-1 then break;
end;
for i:=1 to 100 do
begin
read(b[i]);
if b[i]=-1 then break;
end;
for i:=1 to n do s:=s+'1';
dg(s,0);
if l=0 then
begin
writeln('IMPOSSIBLE');
end;
for i:=1 to l-1 do
for j:=1 to l-i do
if ans[j]>ans[j+1] then
begin
ans[0]:=ans[j];
ans[j]:=ans[j+1];
ans[j+1]:=ans[0];
end;
for i:=1 to l do writeln(ans[i]);
end.
var
light:array[1..8,1..6]of longint=(
(0,0,0,0,0,0),
(0,0,1,1,1,0),
(0,1,0,1,0,1),
(0,1,1,0,1,1),
(1,0,0,1,0,0),
(1,0,1,0,1,0),
(1,1,0,0,0,1),
(1,1,1,1,1,1));
zxczs:array[1..8]of longint=(1,2,1,1,2,1,2,0);
mv:array[1..8]of boolean;
n,c,x,xc,xcc,i,j:longint;
begin
readln(n);
readln(c);
read(x);
fillchar(mv,sizeof(mv),false);
while x<>-1 do
begin
if x=-1 then readln;
x:=(x-1)mod 6+1;
for i:=1 to 8 do
if (not mv[i])and(light[i,x]<>1) then mv[i]:=true;
read(x);
if x=-1 then readln;
end;
read(x); xcc:=0;
fillchar(xc,sizeof(xc),255);
while x<>-1 do
begin
if x=-1 then readln;
x:=(x-1)mod 6+1;
for i:=1 to 8 do
if (not mv[i])and(light[i,x]<>0) then mv[i]:=true;
read(x);
if x=-1 then readln;
end;
for i:=1 to 8 do
begin
if (mv[i])or(not((c-zxczs[i]<0)or(c-zxczs[i]=1))) then continue;
mv[i]:=true;
end;
for i:=1 to 8 do
begin
xc:=0;
if mv[i] then continue;
for j:=1 to n do
begin
x:=(j-1)mod 6+1;
xc:=1; xcc:=1;
write(light[i,x]);
end;
if xc=1 then writeln;
end;
if xcc=0 then writeln('IMPOSSIBLE');
end.