2009年4月16日星期四

2009年4月8日星期三

继续暴力搜索,SGU 125

题目大意:有两个 NxN (N<=3) 的矩阵 A、B。1 <= A[i, j] <= 9,B[i, j] 表示 A 中与 A[i, j] 相邻的<=4个格子内比 A[i, j] 大的格子数。给出 B,求 A。

为什么叫作继续呢。。。。。我也不好多说了。。。。。结果是 23ms 的 AC,能排上第一页了,不错不错。。。


program sgu_125;
{$APPTYPE CONSOLE}
const
 NS = 'NO SOLUTION';
 
var
 N: Integer;
 i, j: Integer;
 a11, a12, a13, a21, a22, a23, a31, a32, a33: Integer;
 b11, b12, b13, b21, b22, b23, b31, b32, b33: Integer;

begin
 readln(N);
 if N = 1 then
  begin
   readln(a11);
   if a11 <> 0 then
    writeln(NS)
   else
    writeln(1)
  end
 else if N = 2 then
  begin
   readln(b11, b12);
   readln(b21, b22);
   for a11 := 1 to 9 do
    for a12 := 1 to 9 do
     for a21 := 1 to 9 do
      if ord(a21 > a11) + ord(a12 > a11) = b11 then
       for a22 := 1 to 9 do
        if ord(a11 > a12) + ord(a22 > a12) = b12 then
         if ord(a11 > a21) + ord(a22 > a21) = b21 then
          if ord(a12 > a22) + ord(a21 > a22) = b22 then
           begin
            writeln(a11, ' ', a12);
            writeln(a21, ' ', a22);
            readln;
            halt
           end;
   writeln(NS)
  end
 else
  begin
   readln(b11, b12, b13);
   readln(b21, b22, b23);
   readln(b31, b32, b33);
   for a11 := 1 to 9 do
    for a12 := 1 to 9 do
     for a21 := 1 to 9 do
      if ord(a12 > a11) + ord(a21 > a11) = b11 then
       for a22 := 1 to 9 do
        if ord(a12 > a22) + ord(a21 > a22) <= b22 then
         for a13 := 1 to 9 do
          if ord(a11 > a12) + ord(a13 > a12) + ord(a22 > a12) = b12 then
           for a31 := 1 to 9 do
            if ord(a11 > a21) + ord(a31 > a21) + ord(a22 > a21) = b21 then
             for a23 := 1 to 9 do
              if (ord(a12 > a13) + ord(a23 > a13) = b13) and
               (ord(a12 > a22) + ord(a21 > a22) + ord(a23 > a22) <= b22) then
               for a33 := 1 to 9 do
                if ord(a13 > a23) + ord(a22 > a23) + ord(a33 > a23) = b23 then
                 for a32 := 1 to 9 do
                  if (ord(a22 > a32) + ord(a33 > a32) + ord(a31 > a32) = b32) and
                   (ord(a12 > a22) + ord(a21 > a22) + ord(a23 > a22) + ord(a32 > a22) = b22) and
                   (ord(a32 > a31) + ord(a21 > a31) = b31) and
                   (ord(a32 > a33) + ord(a23 > a33) = b33) then
                   begin
                    writeln(a11, ' ', a12, ' ', a13);
                    writeln(a21, ' ', a22, ' ', a23);
                    writeln(a31, ' ', a32, ' ', a33);
                    halt
                   end;
   writeln(NS)
  end
end.

2009年4月1日星期三

URAL 1127

小岛叫我做下,说不好做,可惜10分钟就ac了。。。时间空间复杂度都极小。。。至于其中缘由,见下程序。。。。。。。。。
题目大致意思是给你一些相同大小的正方体,每个正方体六个面上涂有不同颜色,选一些正方体堆成一个 1x1xK 的塔,要求前后左右四个面必须分别是一种颜色,问 K 最大为多少。

既然有人问了,就说明下,Q[a, b, c, d] 表示四个面颜色为 a、b、c、d (顺时针或逆时针)时的 K 的最大值。


program ural_1127;
const
 Front = 1;
 Right = 2;
 Left  = 3;
 Rear  = 4;
 Top  = 5;
 Bottom = 6;
var
 Ans, N: Longint;
 i, j, k, l: Longint;
 Q: array [1..10, 1..10, 1..10, 1..10] of Longint;
 D: array [1..6] of Longint;
 C: Char;

begin
 fillchar(Q, sizeof(Q), 0);
 readln(N);
 for i := 1 to N do
  begin
   for j := 1 to 6 do
    begin
     read(C);
     case C of
      'A': D[j] := 1;
      'B': D[j] := 2;
      'C': D[j] := 3;
      'G': D[j] := 4;
      'O': D[j] := 5;
      'R': D[j] := 6;
      'S': D[j] := 7;
      'V': D[j] := 8;
      'W': D[j] := 9;
      'Y': D[j] := 10
     end
    end;
   readln;
   // Front - Right - Rear - Left
   inc(Q[D[Front], D[Right], D[Rear], D[Left]]);
   inc(Q[D[Right], D[Rear], D[Left], D[Front]]);
   inc(Q[D[Left], D[Front], D[Right], D[Rear]]);
   inc(Q[D[Rear], D[Left], D[Front], D[Right]]);
   
   inc(Q[D[Left], D[Rear], D[Right], D[Front]]);
   inc(Q[D[Rear], D[Right], D[Front], D[Left]]);
   inc(Q[D[Right], D[Front], D[Left], D[Rear]]);
   inc(Q[D[Front], D[Left], D[Rear], D[Right]]);
   
   // Front - Top - Rear - Bottom
   inc(Q[D[Front], D[Top], D[Rear], D[Bottom]]);
   inc(Q[D[Top], D[Rear], D[Bottom], D[Front]]);
   inc(Q[D[Bottom], D[Front], D[Top], D[Rear]]);
   inc(Q[D[Rear], D[Bottom], D[Front], D[Top]]);

   inc(Q[D[Bottom], D[Rear], D[Top], D[Front]]);
   inc(Q[D[Rear], D[Top], D[Front], D[Bottom]]);
   inc(Q[D[Top], D[Front], D[Bottom], D[Rear]]);
   inc(Q[D[Front], D[Bottom], D[Rear], D[Top]]);
   
   // Left - Top - Right - Bottom
   inc(Q[D[Left], D[Top], D[Right], D[Bottom]]);
   inc(Q[D[Top], D[Right], D[Bottom], D[Left]]);
   inc(Q[D[Bottom], D[Left], D[Top], D[Right]]);
   inc(Q[D[Right], D[Bottom], D[Left], D[Top]]);

   inc(Q[D[Bottom], D[Right], D[Top], D[Left]]);
   inc(Q[D[Right], D[Top], D[Left], D[Bottom]]);
   inc(Q[D[Top], D[Left], D[Bottom], D[Right]]);
   inc(Q[D[Left], D[Bottom], D[Right], D[Top]])
  end;
 Ans := 0;
 for i := 1 to 10 do
  for j := 1 to 10 do
   for k := 1 to 10 do
    for l := 1 to 10 do
     if Q[i, j, k, l] > Ans then
      Ans := Q[i, j, k, l];
 writeln(Ans)
end.