【codeforces 1025E】Colored Cubes 【构造】
题意
有一个n*n的棋盘和m个棋子,每个棋子有一个初始位置和一个目标位置,每次移动只能选择一个棋子移动到他相邻的格子,并且花费一秒钟。请你找出一个移动的方法,使得在10800步内将所有棋子移动到目标位置。
分析
不是求最少步数!!不是!!题目中保证一定有解,所以很容易想到是构造(场上并没有)
先读入每个棋子的起始位置,然后将第i个棋子移动到(i,i)位置。然后读入每个棋子的终止位置,然后也将其移动到(i,i)位置,然后正着输出第一个方案,倒着输出第二个方案。
然后我们怎么考虑将i个棋子移动到(i,i)位置时不冲突。我们先将每个点按照行坐标进行排序,然后行坐标第i大的点移动到第i行,此时每一行最多只有一个点,然后移动列坐标,将点i移动到第i列,显然不会出现冲突。现在每一列最多只有一个点,然后我们将点i移动到第i行。每一步记录一下就可以了。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn=+;
struct Node{
int x,y,id;
bool operator<(const Node& rhs)const{
return (x<rhs.x||(x==rhs.x&&y<rhs.y));
}
}node[maxn],anss[],anst[];
int n,m;
int Move(int step,int x1,int y1,int x2,int y2){
while(x1!=x2){
if(x1<x2){
anss[++step].x=x1,anss[step].y=y1;
++x1;
anst[step].x=x1,anst[step].y=y1;
}else{
anss[++step].x=x1,anss[step].y=y1;
--x1;
anst[step].x=x1,anst[step].y=y1;
}
}
while(y1!=y2){
if(y1<y2){
anss[++step].x=x1,anss[step].y=y1;
++y1;
anst[step].x=x1,anst[step].y=y1;
}else{
anss[++step].x=x1,anss[step].y=y1;
--y1;
anst[step].x=x1,anst[step].y=y1;
}
}
return step;
}
int solve(int step){
sort(node+,node++m);
for(int i=;i<=m;i++){
if(node[i].x>i){
step=Move(step,node[i].x,node[i].y,i,node[i].y);
node[i].x=i;
}
}
for(int i=m;i>=;i--){
step=Move(step,node[i].x,node[i].y,i,node[i].y);
node[i].x=i;
}
for(int i=;i<=m;i++){
step=Move(step,node[i].x,node[i].y,node[i].x,node[i].id);
node[i].y=node[i].id;
}
for(int i=;i<=m;i++){
step=Move(step,node[i].x,node[i].y,node[i].id,node[i].y);
node[i].x=node[i].id;
}
return step;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&node[i].x,&node[i].y);
node[i].id=i;
}
int step1=solve(); for(int i=;i<=m;i++){
scanf("%d%d",&node[i].x,&node[i].y);
node[i].id=i;
} int step2=solve(step1);
printf("%d\n",step2);
for(int i=;i<=step1;i++){
printf("%d %d %d %d\n",anss[i].x,anss[i].y,anst[i].x,anst[i].y);
}
for(int i=step2;i>step1;i--){
printf("%d %d %d %d\n",anst[i].x,anst[i].y,anss[i].x,anss[i].y);
} return ;
}
【codeforces 1025E】Colored Cubes 【构造】的更多相关文章
- 1352 - Colored Cubes (枚举方法)
There are several colored cubes. All of them are of the same size but they may be colored differentl ...
- UVA 10733 - The Colored Cubes(Ploya)
UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 U ...
- POJ2741 Colored Cubes
Description There are several colored cubes. All of them are of the same size but they may be colore ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- codeforces 22C System Administrator(构造水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud System Administrator Bob got a job as a s ...
- Codeforces 353D Queue(构造法)
[题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...
随机推荐
- hadoop之 解析HDFS的写文件流程
文件是如何写入HDFS的 ? 下面我们来先看看下面的“写”流程图: 假如我们有一个文件test.txt,想要把它放到Hadoop上,执行如下命令: 引用 # hadoop fs - ...
- 原生 Javascript 编写五子棋
原文地址:原生 Javascript 编写五子棋 博客地址:http://www.extlight.com 一.背景 近一个月没写 Javascript 代码,有点生疏.正好浏览网页时弹出五子棋的游戏 ...
- 数组去重(初识ES6)
较常见的一问题:数组去重. 方法一:利用hash数组的原理 var arr=[1,3,3,4,5,5,6,6,7,8,69,8,99,9,0,]; function unique(arry){ var ...
- RK3288 查看时钟树
主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...
- 使用Nancy搭建简单的Http服务的示例demo
刚刚接触Nancy没几天,暂时还不会使用Nancy来做web开发,只是使用Nancy实现了一个简单的Http服务的Demo程序,实现对Post和Get请求的处理. Demo的示例代码地址如下:http ...
- iso网络模型
tcp/ip知识 1.iOS七层模型 应用层 表示层 应用层 ssh httpssl tls ftp mime html snmp 会话层 传输层 传输层 tcp udp 网络层 网络层 ipv6 i ...
- Vue踩坑记录册
1.vue-cli+webpack项目 修改项目名称 解决办法: 1 删除 node_modules 文件夹(如果修改项目名称,需要在在package.json中修改对应的name) 2 重新安装依赖 ...
- json对象转换
String modules =...; //json格式的String对象 //String对象转换为JSON格式数组 JSONArray moduleArr=JSONObject.parseArr ...
- canvas之绘制一张图片
<canvas id="canvas" width="600" height="500" style="background ...
- Android利用百度云来识别身份证及各种证件的信息
上一篇中我已经介绍过了ocr,及google出来的tess-two的使用. 接下来我来介绍一个更方便的身份证识别系统,当然它本身也是利用ocr来识别文字的,不过它处理的更好,可以为我们提供更快,更准确 ...