1. 程式人生 > >【打CF,學演算法——二星級】CodeForces 237B Young Table (構造)

【打CF,學演算法——二星級】CodeForces 237B Young Table (構造)

題面:

B. Young Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≤ n) holds ci ≤ ci - 1.

Let's denote s

as the total number of cells of table a, that is, . We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct.

Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j

-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:

  1. for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
  2. for all i, j (1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1
    .

In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.

Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.

Input

The first line contains a single integer n (1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows.

Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j.

It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.

Output

In the first line print a single integer m (0 ≤ m ≤ s), representing the number of performed swaps.

In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.

Examples Input
3
3 2 1
4 3 5
6 1
2
Output
2
1 1 2 2
2 1 3 1
Input
1
4
4 3 2 1
Output
2
1 1 1 4
1 2 1 3

----------------------------------------------------------------------------------------------我是分割線-----------------------------------------------------------------------------------------------------------------

題意:

    給定一個不規則的n行矩陣,每行分別有Ci個元素,其中Ci大於等於Ci+1,元素總個數為s,每個矩陣位置都有一個不同的值(從1到s),問能否給出一種方案,使得每行的元素從左到右遞增,同一列的元素從上到下遞增。

解題:

     因為題目沒限制最少需要幾步,構造出一個合法解,只要求不超過s步,因為元素個數只有s個,我們只需要把元素按照1,2,3,...s的順序排列,移到對應位置即可,最多隻需要s-1步,故始終可以構造出一個合法解。

程式碼:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
//node記錄值為val的點的當前位置
struct node
{
	int x,y,val;
}store[3000];
//排序時按1,2,..s的方式排序
bool cmp(node a,node b)
{
	return a.val<b.val;
}
//arr儲存當前各位置儲存的是什麼數值
//c陣列記錄每行多少個元素
//x[i],y[i]陣列分表表徵值為i的點最後應該處於的位置
int arr[55][55],c[55],x[3000],y[3000];
//佇列記錄操作方式
queue <int> q1,q2,q3,q4;
int main()
{
	//資料讀入
	int n,cnt=0,sum=0,tmp;
	scanf("%d",&n);
    for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	//資料處理
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=c[i];j++)
		{
		   //分配每個值應處的位置
		   x[cnt+1]=i;
		   y[cnt+1]=j;
           scanf("%d",&arr[i][j]);
		   //記錄某值當前的位置
		   store[cnt].val=arr[i][j];
           store[cnt].x=i;
		   store[cnt].y=j;
		   cnt++;
		}
		//總元素個數,其實直接cnt就好
		sum+=c[i];
	}
	//按值從小到大排序
	sort(store,store+sum,cmp);
	//開始為每個值挪位
	for(int i=1;i<=sum;i++)
	{
		//如果該值已處於其應處的位置,則不操作
       if(store[i-1].x==x[i]&&store[i-1].y==y[i])
		   continue;
	   else
	   {
		   //否則開始交換
          q1.push(store[i-1].x);
		  q2.push(store[i-1].y);
		  //i要移過去的位置當前所佔的值為v
		  int v=arr[x[i]][y[i]];
		  //該位置分配給i
		  arr[x[i]][y[i]]=i;
		  //i原位置分配給v
		  arr[store[i-1].x][store[i-1].y]=v;
		  store[v-1].x=store[i-1].x;
		  store[v-1].y=store[i-1].y;
		  //記錄交換位置
		  q3.push(x[i]);
		  q4.push(y[i]);	  
	   }
	}
	//輸出,此處輸出過於複雜,其實一個佇列即可,一次彈出4個元素
	printf("%d\n",q1.size());
	while(!q1.empty())
	{
		tmp=q1.front();
		q1.pop();
		printf("%d",tmp);
		tmp=q2.front();
		q2.pop();
		printf(" %d",tmp);
		tmp=q3.front();
		q3.pop();
		printf(" %d",tmp);
		tmp=q4.front();
		q4.pop();
		printf(" %d\n",tmp);
	}
	return 0;
}