题意

有一个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 【构造】的更多相关文章

  1. 1352 - Colored Cubes (枚举方法)

    There are several colored cubes. All of them are of the same size but they may be colored differentl ...

  2. UVA 10733 - The Colored Cubes(Ploya)

    UVA 10733 - The Colored Cubes 题目链接 题意:一个立方体.n种颜色,问能涂成多少不同立方体 思路:Ploya求解,正方体相应24种不同旋转一一计算出循环个数就可以.和 U ...

  3. POJ2741 Colored Cubes

    Description There are several colored cubes. All of them are of the same size but they may be colore ...

  4. Codeforces 1383D - Rearrange(构造)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...

  5. Codeforces 549B. Looksery Party[构造]

    B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. codeforces 323A. Black-and-White Cube 构造

    输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...

  7. Codeforces Gym 100531I Instruction 构造

    Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...

  8. codeforces 22C System Administrator(构造水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud System Administrator Bob got a job as a s ...

  9. Codeforces 353D Queue(构造法)

    [题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...

随机推荐

  1. python 数组反序的方法

    arr = np.array(some_sequence) reversed_arr = arr[::-1] do_something(arr) look_at(reversed_arr) do_so ...

  2. kong k8s 安装 以及可视化管理界面

    1. git  clone $ git clone git@github.com:Mashape/kong-dist-kubernetes.git $ cd kong-dist-kubernetes ...

  3. 读懂IL代码就这么简单 ---- IL系列文章

    读懂IL代码就这么简单 (一) 读懂IL代码就这么简单(二) 读懂IL代码就这么简单(三)完结篇 出处:http://www.cnblogs.com/zery/tag/IL%20%E7%B3%BB%E ...

  4. 互联网的keyvalue处理

    今天在和许伟讨论系统配置页面得时候,许伟提到了“打通页面”的概念,当时我没太明白,后来才知道是指类似于cloudera里面的配置页面那种,不是列表页,而是展示+编辑在一个页面.刚才想了一下,其实对于这 ...

  5. (转)JavaMail中的Flag(邮件状态)

    本文转载自:http://blog.csdn.net/chjttony/article/details/6005594 标记邮件就是把邮件标记为已读,删除等操作,需要使用Flags类,它mail.ja ...

  6. struts2学习(5)拦截器简介以及例子执行过程

    一.拦截器简介: 二.Struts2预定义拦截器&拦截器栈 在执行action之前和之后,拦截器进行了操作: 比如struts-default.xml中就有很多预定义的拦截器:   拦截器栈: ...

  7. 设计模式-访问者(Visitor)模式

    访问者模式是对象的行为模式.访问者模式的目的是封装施加在某种数据结构元素上的操作.一旦一些操作需要修改,接受这个操作的数据结构可以保持不变. 个人觉得访问者模式相对其他的设计模式来说稍微复杂,难理解一 ...

  8. maven环境的搭建,lemon-OA办公系统的搭建

    当时要搭建activiti工作流,但是这个工作流是基于maven启动的,于是,学习了一下,maven环境的搭建 准备的环境: Jdk  1.6 Eclipse IDE 一个或者 MyEclipse M ...

  9. VMware 虚拟机中添加新硬盘的方法(转载)

    随着在虚拟机中存储的东西的逐渐的增加,虚拟机的硬盘也逐渐告急,因此急需拓展一块新的虚拟磁盘.以下便是在VMware 中添加新的虚拟磁盘的方法:   一.VMware新增磁盘的设置步骤 (建议:在设置虚 ...

  10. wkhtmltopdf Windows下 测试demo 成功

    html2pdf 转pdf 中文不换行 然后找到了wkhtmltopdf 支持中文换行 样式也支持 在PHP中生成PDF文件,可以使用 FPDF 和 TCPDF .但是它们只能用于创建简单的表格,当涉 ...