[題目描述]
Transformations
方塊轉換
一塊N x N(1<=N<=10)正方形的黑白瓦片的圖案要被轉換成新的正方形圖案。寫一個程序來找出將原始圖案按照以下列轉換方法轉換成新圖案的最小方式:
#1:轉90度:圖案按順時針轉90度。
#2:轉180度:圖案按順時針轉180度。
#3:轉270度:圖案按順時針轉270度。
#4:反射:圖案在水平方向翻轉(形成原圖案的鏡像)。
#5:組合:圖案在水平方向翻轉,然後按照#1-#3之一轉換。
#6:不改變:原圖案不改變。
#7:無效轉換:無法用以上方法得到新圖案。
如果有多種可用的轉換方法,請選擇序號最小的那個。
PROGRAM NAME: transform
INPUT FORMAT
第一行: | 單獨的一個整數N。 |
第二行到第N+1行: | N行每行N個字符(不是“@”就是“-”);這是轉換前的正方形。 |
第N+2行到第2*N+1行: | N行每行N個字符(不是“@”就是“-”);這是轉換後的正方形。 |
SAMPLE INPUT (file transform.in)
3
@-@
---
@@-
@-@
@--
--@
OUTPUT FORMAT
單獨的一行包括1到7之間的一個數字(在上文已描述)表明需要將轉換前的正方形變爲轉換後的正方形的
轉換方法。
SAMPLE OUTPUT (file transform.out)
1
[解題思路]
枚舉1~7的方案,模擬比較即可。
注意一定要順序枚舉方案,因爲多種方案時,取序號最小的。
[Code]
{
ID: zane2951
PROG: transform
LANG: PASCAL
}
program transform;
type
matrix=array[0..11,0..11] of char;
var
f,g,k:matrix;
i,j,n:longint;
//----------check----------
function check(x,y:matrix):boolean;
var
i,j:longint;
begin
for i:=1 to n do
for j:=1 to n do
if x[i,j]<>y[i,j] then exit(false);
exit(true);
end;
//---------change----------
function change(x:matrix):matrix;
var
i,j,ce:longint;
p:matrix;
begin
for i:=1 to n do
begin
ce:=n-i+1;
for j:=1 to n do p[j,ce]:=x[i,j];
end;
exit(p);
end;
//---------reflex----------
function reflex(x:matrix):matrix;
var
i,j:longint;
p:matrix;
tmp:char;
begin
p:=x;
for i:=1 to n do
for j:=1 to n>>1 do
begin
tmp:=p[i,j]; p[i,j]:=p[i,n-j+1]; p[i,n-j+1]:=tmp;
end;
exit(p);
end;
//----------main-----------
begin
assign(input,'transform.in'); reset(input);
assign(output,'transform.out'); rewrite(output);
readln(n);
for i:=1 to n do
begin
for j:=1 to n do read(f[i,j]);
readln;
end;
for i:=1 to n do
begin
for j:=1 to n do read(g[i,j]);
readln;
end;
k:=change(f);
if check(k,g) then begin writeln(1); halt; end;
k:=change(k);
if check(k,g) then begin writeln(2); halt; end;
k:=change(k);
if check(k,g) then begin writeln(3); halt; end;
k:=reflex(f);
if check(k,g) then begin writeln(4); halt; end;
k:=change(k);
if check(k,g) then begin writeln(5); halt; end;
k:=change(k);
if check(k,g) then begin writeln(5); halt; end;
k:=change(k);
if check(k,g) then begin writeln(5); halt; end;
if check(f,g) then begin writeln(6); halt; end;
writeln(7);
close(input); close(output);
end.