USACO 3.2 Magic Squares (魔板)

Magic Squares 魔板

描述

在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:

1  2  3  4  
8  7  6  5

我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示。这是基本状态。

这里提供三种基本操作,分别用大写字母“A”,“B”,“C”来表示(可以通过这些操作改变魔板的状态):

“A”:交换上下两行;  
“B”:将最右边的一列插入最左边; 
“C”:魔板中央作顺时针旋转。

下面是对基本状态进行操作的示范:

A:  8  7  6  5  
    1  2  3  4  
B:  4  1  2  3  
    5  8  7  6  
C:  1  7  2  4  
    8  6  3  5

对于每种可能的状态,这三种基本操作都可以使用。

你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。

格式

PROGRAM NAME: msquare

INPUT FORMAT:

(file msquare.in)

只有一行,包括8个整数,用空格分开(这些整数在范围 1——8 之间),表示目标状态。

OUTPUT FORMAT:

(file msquare.out)

Line 1: 包括一个整数,表示最短操作序列的长度。

Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。

SAMPLE INPUT

2 6 8 4 5 7 3 1

SAMPLE OUTPUT

7 
BCABCCB

-----------------------------------------------------------------------------------------------------------------------------------

Magic Squares
IOI'96

Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:

1 2 3 4
8 7 6 5

In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration.

Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:

  • 'A': exchange the top and bottom row,
  • 'B': single right circular shifting of the rectangle,
  • 'C': single clockwise rotation of the middle four squares.

Below is a demonstration of applying the transformations to the initial squares given above:

A:
8 7 6 5
1 2 3 4
B:
4 1 2 3
5 8 7 6
C:
1 7 2 4
8 6 3 5

All possible configurations are available using the three basic transformations.

You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration.

PROGRAM NAME: msquare

INPUT FORMAT

A single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration.

SAMPLE INPUT (file msquare.in)

2 6 8 4 5 7 3 1

OUTPUT FORMAT

Line 1: A single integer that is the length of the shortest transformation sequence.
Line 2: The lexically earliest string of transformations expressed as a string of characters, 60 per line except possibly the last line.

SAMPLE OUTPUT (file msquare.out)

7
BCABCCB


 


 

一般的bfs....

 

{
ID:chenxia3
PROG:msquare
LANG:PASCAL
}
program JACkCHen;
type ll=array[1..8] of longint;
var t:array[1..60000,1..11] of longint;
    hash:array[1..8,1..8,1..8,1..8,1..8,1..8,1..8] of boolean;
    aim,s:ll;
    ans:array[1..1000] of longint;
    i,j,h,d,c:longint;


    lz:longint;
procedure doit(x,x1:longint; var s:ll);
var i1:longint;
begin
    case x of
    1:begin
        for i1:=1 to 4 do
          s[i1]:=t[x1,i1+4];
        for i1:=5 to 8 do
          s[i1]:=t[x1,i1-4];
      end;
    2:begin
        s[1]:=t[x1,4]; s[2]:=t[x1,1]; s[3]:=t[x1,2]; s[4]:=t[x1,3];
        s[5]:=t[x1,8]; s[6]:=t[x1,5]; s[7]:=t[x1,6]; s[8]:=t[x1,7];
      end;
    3:begin
        s[1]:=t[x1,1]; s[2]:=t[x1,6]; s[3]:=t[x1,2]; s[4]:=t[x1,4];
        s[5]:=t[x1,5]; s[6]:=t[x1,7]; s[7]:=t[x1,3]; s[8]:=t[x1,8];
      end;
    end;
end;

procedure print(y:longint);
begin
    if y=0 then exit;
    print(t[y,11]);
    if t[y,11]=0 then exit;
    inc(c);
    ans[c]:=t[y,10];
end;

function check(s:ll):boolean;
var i1:longint;
begin
    for i1:=1 to 8 do
    if s[i1]<>aim[i1] then exit(false);
    exit(true);
end;

begin
assign(input,'msquare.in'); assign(output,'msquare.out');
reset(input); rewrite(output);
for i:=1 to 4 do read(aim[i]);
for i:=8 downto 5 do read(aim[i]);

t[1,1]:=1; t[1,2]:=2; t[1,3]:=3; t[1,4]:=4;
t[1,5]:=8; t[1,6]:=7; t[1,7]:=6; t[1,8]:=5;
t[1,9]:=0; t[1,10]:=0; t[1,11]:=0;

d:=100;
for i:=1 to 8 do
if t[1,i]<>aim[i] then
    begin
      d:=1;
      break;
    end;
if d=100 then
    begin
      writeln(0);
      writeln;
      close(input); close(output);
      halt;
    end;

fillchar(hash,sizeof(hash),false);
hash[1,2,3,4,8,7,6]:=true;

h:=0; d:=1; c:=0;
repeat
    inc(h);
    for i:=1 to 3 do
      begin
        doit(i,h,s);
        if hash[s[1],s[2],s[3],s[4],s[5],s[6],s[7]] then continue;
        hash[s[1],s[2],s[3],s[4],s[5],s[6],s[7]]:=true;
        inc(d);
        for j:=1 to 8 do
        t[d,j]:=s[j];
        t[d,9]:=t[h,9]+1;
        t[d,10]:=i;
        t[d,11]:=h;

        if check(s) then
          begin
            writeln(t[d,9]);
            print(d);
            for j:=1 to c do
            begin
              write(chr(ans[j]+64));
              if j mod 60=0 then writeln
              else
              if j=c then writeln;
            end;
            close(input); close(output);
            halt;
          end;
      end;
until h>=d;
end.

posted @ 2009-09-24 09:45  jesonpeng  阅读(393)  评论(0编辑  收藏  举报