CF 778D Parquet Re-laying——构造
题目:http://codeforces.com/problemset/problem/778/D
完全没思路……就看了题解。
很好地思路是考虑操作可逆,所以起始状态和最终状态都变到一个中转状态,即都是横着的条,或者都是竖着的条。
比如要做成都是横着的条,考虑从左上到右下依次做好即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,lm=1e5,M=lm+;
int n,m,tot; bool fg;
char a[N][N],b[N][N];
struct Node{
int x,y;
Node(int x=,int y=):x(x),y(y) {}
}s[M],prn[M];
bool chk(int x,int y)
{
if(a[x][y]=='L'&&a[x+][y]=='L')return true;
if(a[x][y]=='U'&&a[x][y+]=='U')return true;
return false;
}
void rot(int x,int y)
{
s[++tot]=Node(x,y);
if(a[x][y]=='U')
{
a[x][y]=a[x+][y]='L';
a[x][y+]=a[x+][y+]='R';
}
else
{
a[x][y]=a[x][y+]='U';
a[x+][y]=a[x+][y+]='D';
}
}
void cz1()//horizenal
{
for(int i=;i<=n;i++)
for(int j=;j<=m;j+=)
{
if(a[i][j]=='L')continue;
int x=i,y=j;
while(!chk(x,y))
{
if(a[x+][y+]=='L')
{rot(x,y+);break;}
x++;y++;
}
while()
{
rot(x,y); if(x==i)break;
rot(x-,y);x--;y--;
if(tot>lm){fg=;return;}
}
}
}
void cz2()
{
for(int i=;i<=n;i+=)
for(int j=;j<=m;j++)
{
if(a[i][j]=='U')continue;
int x=i,y=j;
while(!chk(x,y))
{
if(a[x+][y+]=='U')
{rot(x+,y);break;}
x++;y++;
}
while()
{
rot(x,y); if(x==i)break;
rot(x,y-); x--;y--;
if(tot>lm){fg=;return;}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",a[i]+);
for(int i=;i<=n;i++)
scanf("%s",b[i]+);
if((m&)==)cz1(); else cz2();
if(fg){puts("-1");return ;}
int ans=tot; tot=;
for(int i=;i<=ans;i++)prn[i]=s[i];
memcpy(a,b,sizeof b);
if((m&)==)cz1(); else cz2();
if(fg||ans+tot>lm){puts("-1");return ;}
for(int i=tot;i;i--)prn[++ans]=s[i];
printf("%d\n",ans);
for(int i=;i<=ans;i++)
printf("%d %d\n",prn[i].x,prn[i].y);
return ;
}
CF 778D Parquet Re-laying——构造的更多相关文章
- CodeForces 778D Parquet Re-laying 构造
题意: 有两个\(n \times m\)的矩阵\(A,B\),都是由\(1 \times 2\)的砖块铺成,代表初始状态和结束状态 有一种操作可以把两个砖块拼成的\(2 \times 2\)的矩形旋 ...
- cf 605B B. Lazy Student 构造 好题
题意: 一个n个节点的图,有m条边,已知这个图的一个mst 现在如果我们知道这个图的m条边,和知道mst的n-1条边是哪些,问能不能构造出一个满足条件的图 思路:排序+构造 数组deg[i]表示节点i ...
- CF C. Vladik and fractions——构造题
题目 构造一组 $x, y, z$,使得对于给定的 $n$,满足 $\frac{1}{x} + \frac{1}{y} + \frac{1}{z} = \frac{2}{n}$. 分析: 样例二已 ...
- CodeFirst 初恋
CodeFirst 初恋 原著:Prorgamming Entity Framework Entitywork Code First 大家好! 我是AaronYang,这本书我也挺喜欢的,看了一半了, ...
- 【AaronYang风格】第一篇 CodeFirst 初恋
原著:Prorgamming Entity Framework Entitywork Code First 大家好! 我是AaronYang,这本书我也挺喜欢的,看了一半了,今晚也没 ...
- BZOJ1086: [SCOI2005]王室联邦(贪心,分块?)
Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2610 Solved: 1584[Submit][Status] ...
- CF #296 (Div. 1) B. Clique Problem 贪心(构造)
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- cf 323A A. Black-and-White Cube 立体构造
A. Black-and-White Cube time limit per test 1 second memory limit per test 256 megabytes input stand ...
- cf 323A A. Black-and-White Cube 立体构造 不知道为什么当k为奇数时构造不出来 挺有趣的题目吧
A. Black-and-White Cube time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- curl 和 wget 命令
1. curl curl 支持 HTTP.HTTPS.FTP 等协议,还支持 POST.cookies.认证.从指定偏移处下载部分文件.User-Agent.限速.文件大小.进度条等特征. 1.1 选 ...
- Ubuntu安装byzanz截取动态效果图
byzanz-record主要参数选项 用法: byzanz-record [选项...] 录制您的当前桌面会话 帮助选项: -?, --help 显示帮助选项 --help-all 显示全部帮助选项 ...
- 关于Object.create()与原型链的面试题?
原文地址 https://segmentfault.com/q/1010000004670616?utm_source=weekly&utm_medium=email&utm_camp ...
- [Linux] 018 关机重启命令
1. shutdown 命令 $ shutdown [选项] 时间 选项 -c 取消前一个关机wgwy -h 关机 -r 重启 2. 其他关机命令 $ halt $ poweroff $ init 0 ...
- Ngnix VS Apache
Ngnix和Apache各有优缺点, Ngnix在并发性能上比Apache好太多了 原因是,Ngnix是采用的epoll网络I/O模型, 而Apache采用的是select网络I/O模型 具体参见: ...
- mysql-时间格式
SELECT DATE_FORMAT('2019-1-1 15:1:1.099','%Y-%m-%d %H:%i:%s.%f') -- 2019-01-01 15:01:01.099000 %a 缩 ...
- Manacher(最长镜面回文串)
I - O'My! Gym - 101350I Note: this is a harder version of Mirrored string I. The gorillas have recen ...
- Day5---Python的random库
random库 1.random库是随机数的Python标准库 2.原理 : random生成的伪随机数,而采用梅森旋转算法生成的(伪)随机序列中的元素叫做伪随机数 https://liam.pa ...
- [原]Threads vs Processes in Linux 分析
Linux中thread (light-weighted process) 跟process在實作上幾乎一樣. 最大的差異來自於,thread 會分享 virtual memory address s ...
- CodeChef Count Substrings
Count Substrings Problem code: CSUB Submit All Submissions All submissions for this problem ar ...