讨论 / AC pascal 很长的程序
潘崇明 2016-02-12 17:27:02
点我顶贴 收藏 删除
var a,b,c:array[0..26] of char;

val:array['A'..'Z'] of longint;

use:array[0..26] of boolean;

i,j,k,n:longint;

find:boolean;

procedure print;

var i:longint;

begin

for i:=0 to n-2 do

write(val[chr(ord('A')+i)],' ');

writeln(val[chr(ord('A')+n-1)]);

find:=true;

end;

function check:boolean;

var k:longint;

begin

check:=true;

for k:=1 to n do

if (val[a[k]]<>-1)and(val[b[k]]<>-1)and(val[c[k]]<>-1)

then if ((val[a[k]]+val[b[k]]) mod n<>val[c[k]])

and((val[a[k]]+val[b[k]]+1) mod n<>val[c[k]]) then exit;

check:=false;

end;

procedure search(k,left:longint);

var i,j:longint;

cana,canb,canc:boolean;

begin

if k=0

then begin

if left=0 then print;

exit;

end;

if check then exit;

cana:=false;

if val[a[k]]<>-1 then cana:=true;

for i:=n-1 downto 0 do

if ((cana=false)and(use[i]))or((cana)and(i=val[a[k]]))

then begin

use[i]:=false;

val[a[k]]:=i;

canb:=false;

if val[b[k]]<>-1 then canb:=true;

for j:=n-1 downto 0 do

if ((canb=false)and(use[j]))or((canb)and(j=val[b[k]]))

then begin

use[j]:=false;

val[b[k]]:=j;

canc:=false;

if val[c[k]]<>-1 then canc:=true;

if ((canc=false)and(use[(i+j+left) mod n]))or((canc)

and ((i+j+left) mod n=val[c[k]]))

then begin

use[(i+j+left) mod n]:=false;

val[c[k]]:=(i+j+left) mod n;

search(k-1,(i+j+left) div n);

if find then exit;

if canc=false

then begin

use[(i+j+left) mod n]:=true;

val[c[k]]:=-1;

end;

end;

if canb=false then begin use[j]:=true; val[b[k]]:=-1; end;

end;

if cana=false

then begin

use[i]:=true;

val[a[k]]:=-1;

end;

end;

end;

begin

while not seekeof do

begin

readln(n);

for i:=1 to n do

read(a[i]);

readln;

for i:=1 to n do

read(b[i]);

readln;

for i:=1 to n do

read(c[i]);

readln;

fillchar(val,sizeof(val),byte(-1));

fillchar(use,sizeof(use),true);

find:=false;

search(n,0);

end;

end.

查看更多回复
提交回复