USACO 1.2 方块转换

本文介绍了一种用于转换NxN正方形黑白瓦片图案的算法。该算法通过旋转和平面镜像等操作实现图案转换,并详细说明了如何判断最优转换路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案。写一个程序来找出将原始 
图案按照以下列转换方法转换成新图案的最小方式: 
#1:转90度:图案按顺时针转90度。 
#2:转180度:图案按顺时针转180度。 
#3:转270度:图案按顺时针转270度。 
#4:反射:图案在水平方向翻转(形成原图案的镜像)。 
#5:组合:图案在水平方向翻转,然后按照#1-#3之一转换。 
#6:不改变:原图案不改变。 
#7:无效转换:无法用以上方法得到新图案。 
如果有多种可用的转换方法,请选择序号最小的那个。 

Input

第一行: 单独的一个整数N。 
第二行到第N+1行: N行每行N个字符(不是“@”就是“-”);这是转换前的正方形。 
第N+2行到第2*N+1行: N行每行N个字符(不是“@”就是“-”);这是转换后的正方形。 

Output

单独的一行包括1到7之间的一个数字(在上文已描述)表明需要将转换前的正方形变为转换后的正方形的转换方法。 

Sample Input

 


@-@ 
--- 
@@- 
@-@ 
@-- 
--@

 

Sample Output

 

1


解题思路:先读入原来和转换后的图形,储存在两个二维字符数组里面,然后分别写出六种方案的过程,用原来的图形去一一比对,如果符合转换后的图形就输出相应的数字,如果都不行就输出7


程序:
var
  n,i,flag,flag_c:integer;
  a,b,c,d:array[0..10,0..10]of char;
procedure init;
  var
    i,j:integer;
    s:array[0..10] of string;
  begin
    readln(n);
    for i:=1 to n do
      readln(s[i]);
    for i:=1 to n do
      for j:=1 to n do
        a[i,j]:=s[i][j];
    for i:=1 to n do
      readln(s[i]);
    for i:=1 to n do
      for j:=1 to n do
        b[i,j]:=s[i][j];
end;

function same:boolean;
  var
    i,j:integer;
  begin
    for i:=1 to n do
      for j:=1 to n do
        if b[i,j]<>c[i,j] then exit(false);
    same:=true;
end;

function jiushi:boolean;
  var
    i,j:integer;
  begin
    for i:=n downto 1 do
      for j:=1 to n do
        c[j,i]:=a[n-i+1,j];
    if same then exit(true) else exit(false);
end;

function yibaiba:boolean;
  var
    i,j:integer;
  begin
    for i:=n downto 1 do
      for j:=1 to n do
        c[j,i]:=a[n-j+1,n-i+1];
    if same then exit(true) else exit(false);
end;

function erbaiqi:boolean;
  var
    i,j:integer;
  begin
    for i:=n downto 1 do
      for j:=n downto 1 do
        c[n-j+1,i]:=a[i,j];
    if same then exit(true) else exit(false);
end;

function jingxiang:boolean;
  var
    i,j:integer;
  begin
    for i:=1 to n do
      for j:=1 to n do
        c[j,n-i+1]:=a[j,i];
    if same then exit(true) else exit(false);
end;

function zuhe:boolean;
  var
    i,j:integer;
  begin
    for i:=1 to n do
      for j:=1 to n do
        d[i,j]:=a[i,j];
    for i:=1 to n do
      for j:=1 to n do
        a[i,j]:=c[i,j];
    if jiushi then exit(true);
    if yibaiba then exit(true);
    if erbaiqi then exit(true);
    zuhe:=false;
  end;

procedure main;
  begin
    if jiushi then begin writeln(1); halt; end
      else if yibaiba then begin writeln(2); halt; end
        else if erbaiqi then begin writeln(3); halt; end
          else if jingxiang then begin writeln(4); halt; end
            else if zuhe then begin writeln(5); halt; end
              else if same then begin writeln(6); halt; end
                else writeln(7);
end;

begin
  init;
  main;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值