tyvj P1131 - 虫食算
P1131 - 虫食算From sideman Normal (ACM) 总时限:11s 内存限制:128MB |
|||||
|---|---|---|---|---|---|
|
|||||
Delphi语言: 高亮代码由发芽网提供
program tyvj1131;
var f:array['A'..'Z'] of integer;
q:array[0..26] of char;
st:array[1..3,1..26] of char;
show:array['A'..'Z'] of boolean;
mark:array[0..26] of boolean;
len,i,j,n,k:integer;stt:string;
function judge1:boolean;
var i:integer;r:boolean;
begin
for i:=n downto 1 do begin
r:=true;
for j:=1 to 3 do
if f[st[j,i]]=-1 then r:=false;
if (r) and ((f[st[1,i]]+f[st[2,i]])mod n<>f[st[3,i]])and((f[st[1,i]]+f[st[2,i]]+1)mod n<>f[st[3,i]]) then
exit(false);
end;
exit(true);
end;
function judge2:boolean;
begin
k:=0;
for i:=n downto 2 do begin
k:=k+f[st[1,i]]+f[st[2,i]];
if (k mod n)<>f[st[3,i]] then exit(false);
k:=k div n;
end;
k:=k+f[st[1,1]]+f[st[2,1]];
if k<>f[st[3,1]] then exit(false);
exit(true);
end;
procedure prinf;
var i:integer;
begin
for i:=1 to n-1 do write(f[chr(64+i)],' ');
writeln(f[chr(n+64)]);
end;
procedure dfs(kk:integer);
var i:integer;
begin
if not judge1 then exit;
if (kk>n) and (judge2) then begin
prinf;halt
end;
for i:=n-1 downto 0 do
if (not mark[i]) then begin
f[q[kk]]:=i;mark[i]:=true;
dfs(kk+1);
mark[i]:=false;f[q[kk]]:=-1;
end;
end;
begin
readln(n);
for i:=1 to 3 do begin
readln(stt);
for j:=1 to n do st[i,j]:=stt[j];
end;
fillchar(show,sizeof(show),false);
fillchar(mark,sizeof(mark),false);
for i:=1 to n do f[chr(64+i)]:=-1;
len:=0;
for i:=n downto 1 do
for j:=1 to 3 do
if not show[st[j,i]]then begin
inc(len);q[len]:=st[j,i];show[st[j,i]]:=true;
end;
dfs(1);
end.
var f:array['A'..'Z'] of integer;
q:array[0..26] of char;
st:array[1..3,1..26] of char;
show:array['A'..'Z'] of boolean;
mark:array[0..26] of boolean;
len,i,j,n,k:integer;stt:string;
function judge1:boolean;
var i:integer;r:boolean;
begin
for i:=n downto 1 do begin
r:=true;
for j:=1 to 3 do
if f[st[j,i]]=-1 then r:=false;
if (r) and ((f[st[1,i]]+f[st[2,i]])mod n<>f[st[3,i]])and((f[st[1,i]]+f[st[2,i]]+1)mod n<>f[st[3,i]]) then
exit(false);
end;
exit(true);
end;
function judge2:boolean;
begin
k:=0;
for i:=n downto 2 do begin
k:=k+f[st[1,i]]+f[st[2,i]];
if (k mod n)<>f[st[3,i]] then exit(false);
k:=k div n;
end;
k:=k+f[st[1,1]]+f[st[2,1]];
if k<>f[st[3,1]] then exit(false);
exit(true);
end;
procedure prinf;
var i:integer;
begin
for i:=1 to n-1 do write(f[chr(64+i)],' ');
writeln(f[chr(n+64)]);
end;
procedure dfs(kk:integer);
var i:integer;
begin
if not judge1 then exit;
if (kk>n) and (judge2) then begin
prinf;halt
end;
for i:=n-1 downto 0 do
if (not mark[i]) then begin
f[q[kk]]:=i;mark[i]:=true;
dfs(kk+1);
mark[i]:=false;f[q[kk]]:=-1;
end;
end;
begin
readln(n);
for i:=1 to 3 do begin
readln(stt);
for j:=1 to n do st[i,j]:=stt[j];
end;
fillchar(show,sizeof(show),false);
fillchar(mark,sizeof(mark),false);
for i:=1 to n do f[chr(64+i)]:=-1;
len:=0;
for i:=n downto 1 do
for j:=1 to 3 do
if not show[st[j,i]]then begin
inc(len);q[len]:=st[j,i];show[st[j,i]]:=true;
end;
dfs(1);
end.
浙公网安备 33010602011771号