【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 ...
随机推荐
- Nunit中如何进行事务性单元测试
单元测试要求:单元测试方法并不真正去变更数据库,也就是说单元测试不依赖于数据库中的数据.那我们如何解决执行单元测试方法后,不变更数据库中数据呢? 一般的解决方案有两种: 1. 新建一个单元测试数据库, ...
- 修改http请求文件为本地文件的一种方法:hook InternetReadFile 和 HttpOpenRequest
今天没事的时候学了一下easyhook来hook本进程API,确实很简单就能hook.然后想到这个问题:替换webbrowser请求的文件为本地文件.有什么用就不说了,都懂.因为没有用API写过htt ...
- 关于硬件实现FFT逆运算
前面的文章我们介绍了关于FFT的硬件实现.关于FFT的逆运算IFFT,其实就是将实现FFT的过程反过来执行就可以了. 在实现过程中要注意很多问题. 同 FFT一样,效率问题.以2048点为例,根据理论 ...
- (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法
本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...
- 可复用的自定义Adapter
public abstract class MyAdapter<T> extends BaseAdapter { private ArrayList<T> mData; pri ...
- Git出现提交错误--Push to origin/master was rejected(转)
Step1:出现的问题? 在使用Git Push代码的时候,会出现Push to origin/master was rejected 的错误提示.在第一次提交到代码仓库的时候非常容易出现,因为初始化 ...
- Java堆外内存之一:堆外内存场景介绍(对象池VS堆外内存)
最近经常有人问我在Java中使用堆外(off heap)内存的好处与用途何在.我想其他面临几样选择的人应该也会对这个答案感兴趣吧. 堆外内存其实并无特别之处.线程栈,应用程序代码,NIO缓存用的都是堆 ...
- Java-API-POI:POI 笔记
ylbtech-Java-API-POI:POI 笔记 1. 笔记一返回顶部 1. 1,POI对JDK版本支持及XLSX (2017-04-01 13:51:39)对JDK6的支持,最后版本是POI- ...
- @Retention 注解的作用
注解@Retention可以用来修饰注解,是注解的注解,称为元注解.Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚 ...
- Firefox显示 您的链接不安全 的解决办法
Firefox浏览器,今天突然打开网页的时候提醒,“您的链接不安全”,于是网页怎么刷新都打不开.后来几经查询终于解决,下面告诉大家该如何解决这种情况. 百度经验:jingyan.baidu.com 工 ...