CF1172D.Nauuo and Portals
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Nauuo is a girl who loves playing games related to portals.
One day she was playing a game as follows.
In an n×n grid, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. We denote a cell on the intersection of the r-th row and c-th column as (r,c).
A portal is a pair of doors. You can travel from one of them to another without changing your direction. More formally, if you walk into a cell with a door, you will teleport to the cell with the other door of the same portal and then walk into the next cell facing the original direction. There can not be more than one doors in a single cell.
The "next cell" is the nearest cell in the direction you are facing. For example, if you are facing bottom, the next cell of (2,5) is (3,5).
If you walk into a cell without a door, you must walk into the next cell after that without changing the direction. If the next cell does not exist, you must exit the grid.
You have to set some (possibly zero) portals in the grid, so that if you walk into (i,1) facing right, you will eventually exit the grid from (ri,n), if you walk into (1,i) facing bottom, you will exit the grid from (n,ci).
It is guaranteed that both r1..n and c1..n are permutations of n elements. A permutation of n elements is a sequence of numbers p1,p2,…,pn in which every integer from 1 to n appears exactly once.
She got confused while playing the game, can you help her to find a solution?
Nauuo 是一位喜欢玩与传送门相关游戏的女孩。
某天,她正在玩一款如下所述的游戏。
在一个 n×n 的网格中,行从上到下编号为 1 到 n,列从左到右编号为 1 到 n。我们将第 r 行与第 c 列相交的格子记作 (r,c)。
一个传送门由一对门组成。当你从其中一个门进入时,你将不改变方向地传送到另一个门所在的位置,然后继续朝原方向走入下一个格子。更准确地说:若你朝某个方向行走并进入一个含有门的格子,则你会被传送到该传送门另一扇门所在的格子,并继续朝原来的方向走入其“下一个格子”。每个格子中至多只能有一扇门。
所谓“下一个格子”,是指你在当前朝向上的最近邻格子。例如,若你朝下走,则 (2,5) 的下一个格子是 (3,5)。
若你进入一个不含门的格子,则你必须保持原方向,继续走入其下一个格子;若该下一个格子不存在(即已到达边界外),则你必须离开网格。
你需要在网格中设置若干(可以为零)个传送门,使得:
- 若你从 (i,1) 进入网格且面朝右,则最终必须从 (ri,n) 离开网格;
- 若你从 (1,i) 进入网格且面朝下,则最终必须从 (n,ci) 离开网格。
题目保证 r1..n 和 c1..n 均为 n 个元素的排列。一个 n 元排列是指一个长度为 n 的序列 p1,p2,…,pn,其中 1 到 n 的每个整数恰好出现一次。
她在玩游戏时感到困惑,你能帮她找出一种可行方案吗?
输入格式
The first line contains a single integer n (1≤n≤1000) — the side length of the grid.
The second line contains n integers r1,r2,…,rn (1≤ri≤n) — if you walk into (i,1) facing right, you should exit the grid from (ri,n). It is guaranteed that r1..n is a permutation of n elements.
The third line contains n integers c1,c2,…,cn (1≤ci≤n) — if you walk into (1,i) facing bottom, you should exit the grid from (n,ci). It is guaranteed that c1..n is a permutation of n elements.
第一行包含一个整数 n(1≤n≤1000)—— 网格的边长。
第二行包含 n 个整数 r1,r2,…,rn(1≤ri≤n)—— 若从位置 (i,1) 向右进入网格,则应从 (ri,n) 离开网格。保证 r1..n 是 n 个元素的一个排列。
第三行包含 n 个整数 c1,c2,…,cn(1≤ci≤n)—— 若从位置 (1,i) 向下进入网格,则应从 (n,ci) 离开网格。保证 c1..n 是 n 个元素的一个排列。
输出格式
If it is impossible to satisfy the rule, print the only number −1.
Otherwise the first line should contain a single integer m (0≤m≤2n2) — the number of portals you set.
In the following m lines, each line should contain four integers x1,y1,x2,y2, represents that you set a portal consisting of two doors in (x1,y1) and (x2,y2).
If there are multiple answers, print any. You do not have to minimize m.
如果无法满足规则,则输出唯一的数字 −1。
否则,第一行应包含一个整数 m(0≤m≤2n2),表示你设置的传送门数量。
接下来的 m 行中,每行应包含四个整数 x1,y1,x2,y2,表示你在 (x1,y1) 和 (x2,y2) 处设置了一个由两扇门组成的传送门。
若存在多个答案,输出任意一个即可。你无需最小化 m。
输入输出样例
输入#1
3 1 3 2 3 1 2
输出#1
2 1 1 1 3 2 2 3 1
输入#2
5 3 1 5 4 2 4 2 1 3 5
输出#2
3 1 1 3 4 2 2 3 2 2 3 5 1
说明/提示
Example 1
The cells with the same letter are a portal. You can set portals in this way:

It satisfies the rule, because:

Example 2
You can set portals in this way:

示例 1
具有相同字母的格子构成一个传送门。你可以按如下方式设置传送门:

该设置满足规则,因为:

示例 2
你可以按如下方式设置传送门:

输入解题思路,AI测评打分。不知道怎么写?