洛谷 P2630 图像变换

题目描述

给定3行3列的图像各像素点灰度值,给定最终图像,求最短、字典序最小的操作序列。

其中,可能的操作及对应字符有如下四种:

A:顺时针旋转90度;

B:逆时针旋转90度;

C:左右翻转;

D:上下翻转。

输入输出格式

输入格式:

 

一个矩阵,表示初始的图像。

一个矩阵,表示最终的图像。

 

输出格式:

 

最短、字典序最小的操作序列,保证长度不超过100000000,不保证有解(若长度不超过

100000000无解则输出“Poland cannot into space!!!”)。

 

输入输出样例

输入样例#1: 复制
3 4 5
6 7 8
1 2 3
1 2 3
6 7 8
3 4 5
输出样例#1: 复制
D
思路:最多顺时针转2次,逆时针转一次。上下反转一次,左右翻转一次。
然后就很好处理了,判断所有的情况的组合就好了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int map[4][4],tmp[4][4];
int judge(){
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
            if(map[i][j]!=tmp[i][j])    return false;
    return true;
}
void zhuan(){
    int bns[4][4]={0};
    bns[1][1]=tmp[3][1];bns[1][2]=tmp[2][1];bns[1][3]=tmp[1][1];
    bns[2][1]=tmp[3][2];bns[2][2]=tmp[2][2];bns[2][3]=tmp[1][2];
    bns[3][1]=tmp[3][3];bns[3][2]=tmp[2][3];bns[3][3]=tmp[1][3];
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)    tmp[i][j]=bns[i][j];
}
void swap1(){ for(int i=1;i<=3;i++)    swap(tmp[i][1],tmp[i][3]); }
void swap2(){ for(int j=1;j<=3;j++)    swap(tmp[1][j],tmp[3][j]); }
int main(){
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)    scanf("%d",&map[i][j]);
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)    scanf("%d",&tmp[i][j]);
    zhuan(); if(judge()){ cout<<"A";return 0;   }
    zhuan(); if(judge()){ cout<<"AA";return 0;  }
    zhuan(); if(judge()){ cout<<"B";return 0;   } zhuan();
    swap1(); if(judge()){ cout<<"C";return 0;   }
    zhuan(); if(judge()){ cout<<"AC";return 0;  }
    zhuan(); if(judge()){ cout<<"D";return 0; }
    zhuan(); if(judge()){ cout<<"AC";return 0;  }    zhuan(); 
    swap2(); if(judge()){ cout<<"D";return 0;   }
    zhuan(); if(judge()){ cout<<"AD";return 0;  }
    zhuan(); if(judge()){ cout<<"AB";return 0; }
    zhuan(); if(judge()){ cout<<"BD";return 0;  } zhuan();
    swap1();swap2(); if(judge()){ cout<<"CD";return 0; }
    zhuan(); if(judge()){ cout<<"ACD";return 0; }
    zhuan(); if(judge()){ cout<<"AACD";return 0;}
    zhuan(); if(judge()){ cout<<"BCD";return 0; }
    cout<<"Poland cannot into space!!!";
}
 
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
原文地址:https://www.cnblogs.com/cangT-Tlan/p/8029459.html