题目描述

Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.

Allen's future parking lot can be represented as a rectangle with 4 4 4 rows and n n n ( n≤50 n \le 50 n≤50 ) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having k k k ( k≤2n k \le 2n k≤2n ) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.

Illustration to the first example.However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.

Allen knows he will be a very busy man, and will only have time to move cars at most 20000 20000 20000 times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.

输入输出格式

输入格式:

The first line of the input contains two space-separated integers n n n and k k k ( 1≤n≤50 1 \le n \le 50 1≤n≤50 , 1≤k≤2n 1 \le k \le 2n 1≤k≤2n ), representing the number of columns and the number of cars, respectively.

The next four lines will contain n n n integers each between 0 0 0 and k k k inclusive, representing the initial state of the parking lot. The rows are numbered 1 1 1 to 4 4 4 from top to bottom and the columns are numbered 1 1 1 to n n n from left to right.

In the first and last line, an integer 1≤x≤k 1 \le x \le k 1≤x≤k represents a parking spot assigned to car x x x (you can only move this car to this place), while the integer 0 0 0 represents a empty space (you can't move any car to this place).

In the second and third line, an integer 1≤x≤k 1 \le x \le k 1≤x≤k represents initial position of car x x x , while the integer 0 0 0 represents an empty space (you can move any car to this place).

Each x x x between 1 1 1 and k k k appears exactly once in the second and third line, and exactly once in the first and fourth line.

输出格式:

If there is a sequence of moves that brings all of the cars to their parking spaces, with at most 20000 20000 20000 car moves, then print m m m , the number of moves, on the first line. On the following m m m lines, print the moves (one move per line) in the format i i i r r r c c c , which corresponds to Allen moving car i i i to the neighboring space at row r r r and column c c c .

If it is not possible for Allen to move all the cars to the correct spaces with at most 20000 20000 20000 car moves, print a single line with the integer −1 -1 −1 .

输入输出样例

输入样例#1:

4 5
1 2 0 4
1 2 0 4
5 0 0 3
0 5 0 3
输出样例#1:

6
1 1 1
2 1 2
4 1 4
3 4 4
5 3 2
5 4 2
输入样例#2:

1 2
1
2
1
2
输出样例#2:

-1
输入样例#3:

1 2
1
1
2
2
输出样例#3:

2
1 1 1
2 4 1

说明

In the first sample test case, all cars are in front of their spots except car 5 5 5 , which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most 20000 20000 20000 will be accepted.

In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.

Solution:

  本题居然是A题,然后思路确实是模拟,只不过太巧了。

  我们固定一个方向移动所有的车子,比如顺时针,然后每次能进就进,否则就将其移动到空位上,然后重复这过程,不停绕圈就好了,可以证明即使某辆车反向偏离了终点,也最多只需要$2*50$步就能回来,所以最坏只要绕一圈能进的就进了。模拟此过程,设置一下步数限制,话说模拟题真的难写!

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
int n,k,a[][],tot;
struct node{
int id,x,y;
node(int a=,int b=,int c=){id=a,x=b,y=c;}
}ans[]; int main(){
scanf("%d%d",&n,&k);
For(i,,) For(j,,n) scanf("%d",&a[i][j]);
int nx=,ny=,cnt=,sum=;
while(cnt<=&&sum!=k){
int tx=nx,ty,gx,gy;
nx==?ty=ny+:ty=ny-;
if(ty>n) tx++,ty--;
if(ty<) tx--,ty++;
gy=ty;
tx==?gx=:gx=;
if(a[tx][ty]!=){
if(a[tx][ty]==a[gx][gy])ans[++tot]=node(a[tx][ty],gx,gy),sum++,a[tx][ty]=;
else if(!a[nx][ny]) ans[++tot]=node(a[tx][ty],nx,ny),swap(a[tx][ty],a[nx][ny]);
}
nx=tx,ny=ty;
cnt++;
}
if(sum==k) {
printf("%d\n",tot);
For(i,,tot) printf("%d %d %d\n",ans[i].id,ans[i].x,ans[i].y);
}
else puts("-1");
return ;
}

CF995A Tesla的更多相关文章

  1. Android开发笔记之《特斯拉-Tesla 代码分析》

    转载请注明出处 一.反编译APK文件,查看包结构 // Android库,不多解释android.support // HttpClient 4.1 repackaged for Android // ...

  2. 基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记

    基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记 飞翔的蜘蛛人 注1:本人新手,文章中不准确的地方,欢迎批评指正 注2:知识储备应达到Linux入门级水平 ...

  3. ubuntu14.04下的NVIDIA Tesla K80显卡驱动的安装教程

    搞深度学习如何能够不与浑身是“核”的显卡打交道呢? 人工智能的兴起除了数据量的大量提升,算法的不断改进,计算能力的逐步提高,还离不开软件基础设施的逐步完善.当下的主流的深度学习工具软件无论是Caffe ...

  4. Tesla为什么要公开专利

    这是今天在网上看到Tesla公司的专利墙图片,还是比较有视觉冲击力的,正好可以转来当配图. 业界先锋Tesla日前惊世骇俗地公开电动汽车专利,赢得如潮好评:不过大家都知道,对于西方科技公司,专利历来是 ...

  5. CUDA编程-(1)Tesla服务器Kepler架构和万年的HelloWorld

    结合CUDA范例精解以及CUDA并行编程.由于正在学习CUDA,CUDA用的比较多,因此翻译一些个人认为重点的章节和句子,作为学习,程序将通过NVIDIA K40服务器得出结果.如果想通过本书进行CU ...

  6. 配有Tesla K40c的服务器新装Ubuntu16.04并安装CUDA8.0、Anaconda3、Matlab2016a、OPENCV3.1、CuDNN5.1、MXNet

    注:本文原创,作者:Noah Zhang  (http://www.cnblogs.com/noahzn/) 决定加入深度学习的大军,感谢导师给配了台新设备!第一次接触服务器并配置开发环境,整个过程中 ...

  7. 企业官网Web原型制作分享-Tesla

    Tesla是汽车行业知名的奢华品牌,产品为纯电动汽车,知名度极高.此模板正是取自Tesla的官网,高端大图配上文字排版,彰显了汽车的奢华感觉. 本原型由国产Mockplus(原型工具)和iDoc(智能 ...

  8. 【转】Tesla autopilot 引起致命车祸

    Tesla autopilot 引起致命车祸 好一段时间没关心 Tesla 了,今天才发现他们的 autopilot 终于引起了致命的车祸.这场 Model S 撞上18轮大卡车的车祸,发生于5月7号 ...

  9. 【转】Tesla Model X的车门设计问题

    Tesla Model X的车门设计问题 Tesla即将推出的SUV(Model X),不但继承了以上提到的Model S的各种问题(触摸屏,门把,……),而且还制造了新的问题.Model X具有一个 ...

随机推荐

  1. 北京Uber优步司机奖励政策(12月12日)

    用户组:人民优步及电动车(适用于12月12日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...

  2. 成都Uber优步司机奖励政策(2月1日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. Myeclipse - 问题集 - specified vm install not found

    In Eclipse, click the ant file -- Run As -- External Tools Configuration and click on the JRE tab. S ...

  4. HIS系统两种收费模式比较:前计费和后计费

    一.药品 a.前计费:审核(临时医嘱)或者分解(长期医嘱)计费 退费处理方式,1)如果是还未发药,则护士站直接退费;2)如果药房已经发药,则护士站发出退费申请,由护士拿着药品去药房退药退费. b.后计 ...

  5. android分析windowManager、window、viewGroup之间关系(一)

    本文将主要介绍addview方法,在windowManager.window.viewGroup中的实现原理.首先将介绍这些类结构关系,然后分析其内在联系,介绍实现原理,最后介绍重要的一个参数wind ...

  6. 「日常训练」Jin Yong’s Wukong Ranking List(HihoCoder-1870)

    题意与分析 2018ICPC北京站A题. 题意是这样的,给定若干人的武力值大小(A B的意思是A比B厉害),问到第几行会出现矛盾. 这题不能出现思维定势,看到矛盾就是矛盾并查集--A>B.A&g ...

  7. Jquery获取DOM绑定事件

    获取到当前正在执行的事件: $('#testDive').bind('click', function(event){alert('event: ' + event.type)}); 获取所有绑定事件 ...

  8. Java小记(1)

    return 关键字 package mytest; public class Test4 { public static void main(String[] args) { // TODO Aut ...

  9. Java 递归 反射 正则表达式

    一 递归 1. 就是函数自身调用自身 (就是在栈内存中不断的加载同一个函数) 2. 什么时候用递归呢? 当一个功能被重复使用 而每一次使用该功能时的参数不确定 都由上次的功能元素结果来确定 简单说: ...

  10. 卸载CDH5.7

    CDH5.7卸载1.记录用户数据目录2.关闭所有服务2.1在CM中,选择某个集群,然后停止集群.2.2逐个关闭CDH中的服务3.删除parcels4.删除集群5.卸载Cloudera manager ...