【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 ...
随机推荐
- ballerina 学习十四 values && types
ballerina 包含的数据类型有string int map array record boolean ojbect function table tuple any 简单说明 数据类型和其他语言 ...
- php基础语法(数据类型、运算符)
数据类型 标量类型: int, float, string, bool 复合类型: array, object 特殊类型: null, resouce 整数类型int, integer 字符串类型st ...
- Ant入门使用说明
一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.在实际软件开发中,有很多地方可以用到ant. 开发环境: 复制代码 代码如下: S ...
- 笔记:NPM 无限需要依赖问题解决
笔记:NPM 无限需要依赖问题解决 起因 因为想学一下 VUE,开始跟着教程一步一步输出命令,开始也没有什么问题,一切都很顺利. 突然不知道是哪一步出了问题,一直让我安装依赖,没完没了,开始并不觉得有 ...
- 云原生应用基金会CNCF
2006 年 8 月 9 日,埃里克·施密特(EricSchmidt)在搜索引擎大会上首次提出了“云计算”(Cloud Computing)的概念.一转眼十年过去了,它的发展势如破竹,不断渗透当代的 ...
- 什么是YARN
YARN的核心组件: 1)ResourceManager,扮演Master角色(和HDFS的nameNode很像)主要用于资源分配:RM有两个子组件,分别是Scheduler(Capacity Sch ...
- bzoj 4530 [Bjoi2014]大融合——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 LCT维护子树 siz .设 sm[ ] 表示轻儿子的 siz 和+1(1是自己的si ...
- 原生 Javascript 编写五子棋
原文地址:原生 Javascript 编写五子棋 博客地址:http://www.extlight.com 一.背景 近一个月没写 Javascript 代码,有点生疏.正好浏览网页时弹出五子棋的游戏 ...
- 11.Python使用Scrapy爬虫小Demo(新手入门)
1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...
- new与malloc的区别,以及内存分配浅析
从函数声明上可以看出.malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小.比如: 1 2 3 int *p; p = new int; //返回类型 ...