hihoCoder #1240 Image Encryption

Description

A fancy square image encryption algorithm works as follow:

0. consider the image as an N x N matrix

1. choose an integer k∈ {0, 1, 2, 3}

2. rotate the square image k * 90 degree clockwise

3. if N is odd stop the encryption process

4. if N is even split the image into four equal sub-squares whose length is N / 2 and encrypt them recursively starting from step 0

Apparently different choices of the k serie result in different encrypted images. Given two images A and B, your task is to find out whether it is POSSIBLE that B is encrypted from A. B is possibly encrypted from A if there is a choice of k serie that encrypt A into B.

Input

Input may contains multiple testcases.

The first line of the input contains an integer T(1 <= T <= 10) which is the number of testcases.

The first line of each testcase is an integer N, the length of the side of the images A and B.

The following N lines each contain N integers, indicating the image A.

The next following N lines each contain N integers, indicating the image B.

For 20% of the data, 1 <= n <= 15

For 100% of the data, 1 <= n <= 100, 0 <= Aij, Bij <= 100000000

Output

For each testcase output Yes or No according to whether it is possible that B is encrypted from A.

Sample Input
3
2
1 2
3 4
3 1
4 2
2
1 2
4 3
3 1
4 2
4
4 1 2 3
1 2 3 4
2 3 4 1
3 4 1 2
3 4 4 1
2 3 1 2
1 4 4 3
2 1 3 2
Sample Output
Yes
No
Yes

 Solution:

  1 #include <cstdlib>
  2 #include <cstdio>
  3 #include <vector>
  4 using namespace std;
  5
  6
  7 void rotateImg(vector<vector<int> > &A) {
  8
  9     int N = A.size();
 10     int r = N >> 1;
 11     int c = (N + 1) >> 1;
 12     for (int i = 0; i < r; ++i) {
 13         for (int j = 0; j < c; ++j) {
 14             int tmp = A[i][j];
 15             A[i][j] = A[N - 1 - j][i];
 16             A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j];
 17             A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i];
 18             A[j][N - 1 - i] = tmp;
 19         }
 20     }
 21
 22 }
 23
 24 bool isEqual(vector<vector<int> > A, vector<vector<int> > B) {
 25     int N = A.size();
 26     for (int i = 0; i < N; ++i) {
 27         for (int j = 0; j < N; ++j) {
 28             if (A[i][j] != B[i][j]) return false;
 29         }
 30     }
 31     return true;
 32 }
 33
 34 vector<vector<int> > split(vector<vector<int> > A, int x, int y) {
 35     int N = A.size() >> 1;
 36     vector<vector<int> > v(N, vector<int>(N, 0));
 37     for (int i = 0; i < N; ++i) {
 38         for (int j = 0; j < N; ++j) {
 39             v[i][j] = A[i + x][j + y];
 40         }
 41     }
 42     return v;
 43 }
 44
 45 bool isEncrypted(vector<vector<int> > A, vector<vector<int> > B) {
 46     int N = A.size();
 47     if (N == 1) return A[0][0] == B[0][0];
 48     if (N & 0x01) {
 49         vector<vector<int> > AA = A;
 50         for (int k = 0; k < 4; ++k) {
 51             if (isEqual(AA, B)) return true;
 52             rotateImg(AA);
 53         }
 54         return false;
 55     }
 56     else {
 57         int N_2 = N >> 1;
 58         vector<vector<int> > AA = A;
 59         int x[] = { 0, N_2, 0, N_2 };
 60         int y[] = { 0, 0, N_2, N_2 };
 61         for (int k = 0; k < 4; ++k) {
 62             bool flag = true;
 63             for (int kk = 0; kk < 4; ++kk) {
 64                 if (!isEncrypted(split(AA, x[kk], y[kk]), split(B, x[kk], y[kk]))) {
 65                     flag = false;
 66                     break;
 67                 }
 68             }
 69             if (flag) return true;
 70             rotateImg(AA);
 71         }
 72         return false;
 73     }
 74 }
 75
 76
 77
 78
 79 int main() {
 80     int T;
 81     scanf("%d", &T);
 82     while (T--) {
 83         int N;
 84         scanf("%d", &N);
 85         vector<vector<int> > A(N, vector<int>(N, 0));
 86         vector<vector<int> > B(N, vector<int>(N, 0));
 87         for (int i = 0; i < N; ++i) {
 88             for (int j = 0; j < N; ++j) {
 89                 int v;
 90                 scanf("%d", &v);
 91                 A[i][j] = v;
 92             }
 93         }
 94
 95         for (int i = 0; i < N; ++i) {
 96             for (int j = 0; j < N; ++j) {
 97                 int v;
 98                 scanf("%d", &v);
 99                 B[i][j] = v;
100             }
101         }
102
103         if (isEncrypted(A, B)) printf("Yes\n");
104         else printf("No\n");
105     }
106 }
时间: 2024-08-07 02:03:43

hihoCoder #1240 Image Encryption的相关文章

[hihoCoder#1381]Little Y&#39;s Tree

[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一

backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx>   <inf>Log Shrink</inf>   <Sql> --  ======================================================================== -- Shrink of log file E:\SQ

hihoCoder 1175:拓扑排序二

题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头. 最后终于调通了.总结起来,原因只有一个:不够仔细. 思路不用细说了,就是拓扑排序的简单应用.然而,一些不起眼的细节才是让你掉坑里的真正原因. 猜猜哪儿可能出bug? // A simple problem, but you can't be too careful with it. #inclu

十三:Transparent Encryption in HDFS(转)

透明加密:http://blog.csdn.net/linlinv3/article/details/44963429 hadoop透明加密  kms 简介 Hadoop Key Management Server(KMS)是一个基于HadoopKeyProvider API编写的密钥管理服务器.他提供了一个client和一个server组件,client和server之间基于HTTP协议使用REST API通信.Client是一个KeyProvider的实现,使用KMS HTTP REST A

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x值是多少.这里还要再辅助一个val[k]表示处理到当前情况只错了k次的最小值是多少因为改变的不止是和弦还有初始值,可以看一下代码理解一下. #include <iostream> #include <cstring> #include <cstdio> #include &

hihocoder #1190 : 连通性&#183;四 点双联通分量

http://hihocoder.com/problemset/problem/1190?sid=1051696 先抄袭一下 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了点问题,当服务器(上次是连接)发生宕机的时候,在同一组的服务器有可能连接不上,所以他们希望重新进行一次分组.这一次老师希望对连接进行分组,并把一个组内的所有连接关联的服务器也视为这个组内

Hihocoder 太阁最新面经算法竞赛18

Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus 描述 Given an NxN 01 matrix, find the biggest plus (+) consisting of 1s in the matrix. size 1 plus size 2 plus size 3 plus size 4 plus 1 1 1 1 111 1 1

hihoCoder 1393 网络流三&#183;二分图多重匹配(Dinic求二分图最大多重匹配)

#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含小Hi和小Ho),编号依次为1..N. 运动会一共有M项不同的比赛,编号为1..M.第i项比赛每个班需要派出m[i]名选手参加. 根据小Hi和小Ho的统计,编号为i的学生表示最多同时参加

hihoCoder 1430 : A Boring Problem(一琐繁题)

hihoCoder #1430 : A Boring Problem(一琐繁题) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 As a student of the school of electronics engineering and computer science in Peking University, Kyle took the course named Advanced Algebra in his freshma