题意:

有两个\(n \times m\)的矩阵\(A,B\),都是由\(1 \times 2\)的砖块铺成,代表初始状态和结束状态

有一种操作可以把两个砖块拼成的\(2 \times 2\)的矩形旋转\(90^{\circ}\)

问如何操作才能使初始状态转化为结束状态,无解输出\(-1\)

分析:

不妨假设\(m\)为偶数,否则可以旋转整个矩阵使得矩阵的列数为偶数

先找一个中间过度状态矩阵\(C\),它的每个砖块都是水平方向的

求出\(A \to C\)和\(B \to C\)的操作序列,因为操作是可逆的,所以就得到了\(A \to C \to B\)的操作序列

从第一行开始逐个扫描,遇到一个竖直方向的砖块就将它旋转,这是可能有两种情况:

  • 右边的砖块也是竖直方向的,那么可以直接旋转
  • 右边砖块是水平的,那么就递归到子问题:将右边的水平砖块先旋转过来,再一起旋转

算法的正确性不会证=_=

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
using namespace std; const int maxn = 60;
typedef pair<int, int> PII;
#define ALL(x) (x).begin(), (x).end() int n, m, Max;
char s1[maxn][maxn], s2[maxn][maxn];
vector<PII> a, b; void rotate(char s[][maxn], int x, int y) {
if(s[x][y] == 'U') {
s[x][y] = s[x+1][y] = 'L';
s[x][y+1] = s[x+1][y+1] = 'R';
} else {
s[x][y] = s[x][y+1] = 'U';
s[x+1][y] = s[x+1][y+1] = 'D';
}
} bool check(char s[][maxn], int x, int y) {
if(s[x][y] == s[x][y+1] && s[x][y] == 'U' && s[x+1][y] == s[x+1][y+1] && s[x+1][y] == 'D') return true;
if(s[x][y] == s[x+1][y] && s[x][y] == 'L' && s[x][y+1] == s[x+1][y+1] && s[x][y+1] == 'R') return true;
return false;
} bool adjust(char s[][maxn], int x, int y, int flag, vector<PII>& a) {
if(x + 1 >= n || y + 1 >= m) return false;
if(check(s, x, y)) {
rotate(s, x, y);
a.emplace_back(x, y);
return true;
} else {
if(!adjust(s, x+1-flag, y+flag, flag^1, a)) return false;
rotate(s, x, y);
a.emplace_back(x, y);
return true;
}
} void op(char& c) {
switch(c) {
case 'L': c = 'U'; break;
case 'U': c = 'L'; break;
case 'R': c = 'D'; break;
case 'D': c = 'R'; break;
}
} void change(char s[][maxn]) {
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) op(s[i][j]);
for(int i = 0; i < Max; i++)
for(int j = 0; j < i; j++)
swap(s[i][j], s[j][i]);
} int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%s", s1[i]);
for(int i = 0; i < n; i++) scanf("%s", s2[i]);
Max = n > m ? n : m;
bool changed = false;
if(m & 1) { change(s1); change(s2); swap(n, m); changed = true; } for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j += 2) {
if(s1[i][j] != 'L') {
if(!adjust(s1, i, j, 1, a)) { puts("-1"); return 0; }
}
if(s2[i][j] != 'L') {
if(!adjust(s2, i, j, 1, b)) { puts("-1"); return 0; }
}
}
} reverse(ALL(b));
a.insert(a.end(), ALL(b));
printf("%d\n", (int)a.size());
for(pair<int, int> t : a) {
if(changed) swap(t.first, t.second);
printf("%d %d\n", t.first + 1, t.second + 1);
} return 0;
}

CodeForces 778D Parquet Re-laying 构造的更多相关文章

  1. CF 778D Parquet Re-laying——构造

    题目:http://codeforces.com/problemset/problem/778/D 完全没思路……就看了题解. 很好地思路是考虑操作可逆,所以起始状态和最终状态都变到一个中转状态,即都 ...

  2. Educational Codeforces Round 10 B. z-sort 构造

    B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...

  3. Codeforces 707C Pythagorean Triples(构造三条边都为整数的直角三角形)

    题目链接:http://codeforces.com/contest/707/problem/C 题目大意:给你一条边,问你能否构造一个包含这条边的直角三角形且该直角三角形三条边都为整数,能则输出另外 ...

  4. Codeforces 1246D/1225F Tree Factory (构造)

    题目链接 https://codeforces.com/contest/1246/problem/D 题解 首先考虑答案的下界是\(n-1-dep\) (\(dep\)为树的深度,即任何点到根的最大边 ...

  5. Codeforces - 1202D - Print a 1337-string... - 构造

    https://codeforces.com/contest/1202/problem/D 当时想的构造是中间两个3,然后前后的1和7组合出n,问题就是n假如是有一个比较大的质数因子或者它本身就是质数 ...

  6. Codeforces 743C - Vladik and fractions (构造)

    Codeforces Round #384 (Div. 2) 题目链接:Vladik and fractions Vladik and Chloe decided to determine who o ...

  7. Codeforces 1368E - Ski Accidents(构造+思维)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙构造题(不过可能我构造太烂了?) 首先考虑这个奇奇怪怪的 \(\dfrac{4}{7}\),以及这个每个点出度最多为 \(2\) 的条 ...

  8. Codeforces 1270E - Divide Points(构造+奇偶性)

    Codeforces 题目传送门 & 洛谷题目传送门 显然,直接暴力枚举是不可能的. 考虑将点按横纵坐标奇偶性分组,记 \(S_{i,j}=\{t|x_t\equiv i\pmod{2},y_ ...

  9. codeforces 622C. Optimal Number Permutation 构造

    题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...

随机推荐

  1. attachEvent方法的作用

    用于HTML内代码层和UI层分离.比如,你要给一个按钮增加一个单击事件,你会怎么做?<input type="button" id="theBtn" va ...

  2. centos6.5_64bit-禅道安装及数据库操作

    linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道. 从7.3版本开始,linux一键安装包分为32位和64位两个包,请大家根据操作系统的情况下载 ...

  3. ansible安装php

    环境:Centos 7.x 独立php-fpm.conf配置文件 [root@master playbook]# tree php php ├── php-fpm.conf └── php.yml p ...

  4. Last_IO_Errno: 1062

    主键冲突的错误 1062   模拟错误:   在主库上操作: create table test100(id int not null,name varchar(20),primary key(id) ...

  5. DP找最优配置,(POJ1018)

    题目链接:http://poj.org/problem?id=1018 这个DP,我的头都快晕了. dp[i][j]表示取到第i个设备,宽带为j时的最小价格. 状态转移方程: dp[i][k]=min ...

  6. 剑指offer52 构建乘积数组

    这个题的错误和c++ primier中名字的作用域例子相似.只是这里将int换成了vecto<int>这种形式. class Solution { public: vector<in ...

  7. Python中的__name__和__main__含义详解

    1背景 在写Python代码和看Python代码时,我们常常可以看到这样的代码: ? 1 2 3 4 5 def main():     ......   if __name == "__m ...

  8. Ubuntu 10.04上安装MongoDB

    MongoDB是一个可扩展.高性能的下一代数据库.MongoDB中的数据以文档形式存储,这样就能在单个数据对象中表示复杂的关系.文档可能由 以下几 部分组成:独立的基本类型属性.“内嵌文档”或文档数组 ...

  9. ES6的数组方法之Array.from

    首先说说什么是数组:数组在类型划分上归为Object,属于比较特殊的对象,数组的索引值类似于对象的key值. 数组的几个注意点: 1.数组的长度是可读属性,不可更改,数组的长度根据索引最大值. 2.数 ...

  10. swiper轮播始终居中active图片

    用的是vue-awesome-swiper 在vue项目中,参数方法与swiper一致.使用场景如下: 左侧小图一共八张,默认显示的是三张,始终保持activeimg在中间,提升用户体验度.swipe ...