题意

有一个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. sqlopsstudio 微软开源跨平台sql server 连接工具

    sqlopsstudio  微软开源的sql server 连接工具,跨平台,基于vscode 分之代码. 项目地址: https://github.com/Microsoft/sqlopsstudi ...

  2. Bootstrap树控件(Tree控件组件)使用经验分享

    前言:很多时候我们在项目中需要用到树,有些树仅仅是展示层级关系,有些树是为了展示和编辑层级关系,还有些树是为了选中项然后其他地方调用选中项.不管怎么样,树控件都是很多项目里面不可或缺的组件之一.今天, ...

  3. Python 函数 -getattr()

    getattr(object, name[, default]) getatt() 函数用于返回一个对象属性值.object 对象.name 字符串,对象属性.object 默认返回值,如果不提供该参 ...

  4. RK3288 修改设备默认的蓝牙名称

    path:device/rockchip/rk3288/bluetooth/bdroid_buildcfg.h /* * Copyright (C) 2012 The Android Open Sou ...

  5. 解决div嵌套时IE8和FF无法自适应高度

    解决div嵌套时IE8和FF无法自适应高度 还是做类似新浪评论回复的时候,将回复的DIV嵌套在一个DIV中,然后点击回复的时候显示子DIV,这是父DIV的高度是会变化的,于是我将父DIV的高度设置为h ...

  6. HotSpot Stop-and-Copy GC

    rednaxelafx的Cheney算法的伪代码.如果不用forwarding的话,维护一个旧地址到新地址的映射也可以. 其中重点部分: void Heap::collect() { // The f ...

  7. ALSA声卡08_从零编写之框架_学习笔记

    1.整体框架 (1)图示((DAI(全称Digital Audio Interface)接口)) 在嵌入式系统里面,声卡驱动是ASOC,是在ALSA驱动上封装的一层,包括以下三大块 (2)程序框架 m ...

  8. 【UVa】12118 Inspector's Dilemma(欧拉道路)

    题目 题目     分析 很巧秒的一道题目,对着绿书瞎yy一会. 联一下必须要走的几条边,然后会形成几个联通分量,统计里面度数为奇数的点,最后再减去2再除以2.这样不断相加的和加上e再乘以t就是答案, ...

  9. nginx+php产生大量TIME_WAIT连接解决办法

    问题:当启动nginx和php-fpm时,使用netstat -tunap查看到大量TIME_WAIT连接 由于不知道原因,害怕是受到攻击,马上killall nginx 和php-fpm 会不会是8 ...

  10. jQuery样式与动画

    修改内联CSS .css() 获取 //取得单个属性的值,传入'属性名',返回"value" .css('property') //取得多个属性的值,传入'['属性1','属性2' ...