加密网格

题目见<<算法艺术与信息学竞赛>>

View Code
  1 program jiami;
  2 const
  3   maxn=10;
  4 type
  5   sta=record
  6        x,y:integer;
  7        end;
  8 
  9 var
 10     map,m2,tar:array[1..maxn*2,1..maxn*2]of char;
 11     q:array[1..maxn]of sta;
 12     boo:array[1..maxn*maxn*4]of boolean;
 13     che:array[1..maxn*2,1..maxn*2]of boolean;
 14     nx,ny,n,i,j:longint;
 15     flag:boolean;
 16 
 17 function can:boolean;
 18 var
 19  i,j,k,mm:longint;
 20  kk:string;
 21 begin
 22     can:=true;
 23     mm:=0;
 24     for k:=1 to 4 do
 25      begin
 26 
 27          for i:=1 to 2*n do
 28           for j:=1 to 2*n do
 29            begin
 30               str(k,kk);
 31               if m2[i,j]=kk then
 32                begin
 33                    inc(mm);
 34                    nx:=(mm div (2*n))+1;ny:=mm mod (2*n);
 35                    if ny=0 then begin ny:=2*n;nx:=nx-1;end;
 36 
 37                    if map[nx,ny]<>tar[i,j] then exit(false);
 38                    //按照加密版的规则产生的结果是否与题给answer一致
 39                end;
 40            end;
 41      end;
 42 end;
 43 
 44 function check:boolean;
 45 var
 46   i,j:integer;
 47 begin
 48         check:=true;
 49        fillchar(che,sizeof(che),0);
 50        //检验所求模版每一个格子是否满足条件 同时加上标记
 51        for i:=1 to n*n do
 52         begin
 53             nx:=q[i].x;ny:=q[i].y;
 54             if ny=0 then begin ny:=2*n;nx:=nx-1;end;
 55 
 56             if not che[nx,ny] then
 57               begin
 58                 che[nx,ny]:=true;
 59                 m2[nx,ny]:='1';
 60                 //1 代表对应坐标是没有旋转的第一次填数中的点
 61               end else exit(false);
 62             if not che[ny,2*n+1-nx]then
 63                 begin
 64                   che[ny,2*n+1-nx]:=true;
 65                   m2[ny,2*n+1-nx]:='2';
 66                   //2 代表旋转90度后的第二次填数中的点
 67                 end else exit(false);
 68             if not che[n*2+1-nx,n*2+1-ny] then
 69                begin
 70                 che[n*2+1-nx,n*2+1-ny]:=true;
 71                 m2[n*2+1-nx,n*2+1-ny]:='3';
 72                 //3 代表旋转180度后的第三次填数中的点
 73                end else exit(false);
 74             if not che[n*2+1-ny,nx] then
 75               begin
 76                che[n*2+1-ny,nx]:=true;
 77                m2[n*2+1-ny,nx]:='4';
 78                //4代表旋转270度后的第四次填数中的点
 79               end else exit(false);
 80         end;
 81 
 82        if (not can) then exit(false);
 83 end;
 84 
 85 procedure print;
 86 var
 87  i,j:longint;
 88 begin
 89       for i:=1 to 2*n do
 90        begin
 91         for j:=1 to 2*n do
 92          if m2[i,j]='1' then write('*')else write('.');
 93          writeln;
 94        end;
 95 end;
 96 
 97 procedure play(d,k:longint);
 98 var
 99  i,j:integer;
100 begin
101     if d=n*n+1 then
102      if check then
103       begin
104            print;halt;
105       end else exit;
106 
107     for i:=k to 4*n*n-(n*n-(d-1)) do
108     if boo[i] then
109      begin
110         boo[i]:=false;
111         nx:=(i div (n*2))+1;ny:=i mod (n*2);
112         if ny=0 then begin ny:=2*n; nx:=nx-1;end;
113 
114         q[d].x:=nx;q[d].y:=ny;
115         play(d+1,i+1);
116         boo[i]:=true;
117      end;
118 end;
119 
120 begin
121     assign(input,'win.in');
122     assign(output,'win.out');
123     reset(input);
124     rewrite(output);
125 
126     readln(n);
127     for i:=1 to 2*n do
128      begin
129        for j:=1 to 2*n do  read(map[i,j]);readln;
130      end;
131     for i:=1 to 2*n do
132      begin
133        for j:=1 to 2*n do  read(tar[i,j]);readln;
134      end;
135 
136     fillchar(boo,sizeof(boo),1);
137 
138     //xuan zhuan  shi ziji
139     for i:=1 to n*n*4 do
140     begin
141          nx:=(i div (n*2))+1;ny:=i mod (n*2);
142          if ny=0 then begin ny:=n*2;nx:=nx-1;end;
143 
144          if (ny=2*n+1-nx)and(nx=ny)then boo[i]:=false;
145     end;
146 
147     //main
148     play(1,1);
149 
150     close(input);
151     close(output);
152 
153 
154 end.

 

posted @ 2012-05-20 16:03  翱翔的感觉  阅读(178)  评论(0)    收藏  举报