CodeForces 266C—— Below the Diagonal(模拟,贪心,递归)

来源:互联网 发布:燕十八php 编辑:程序博客网 时间:2024/05/21 05:57

题目:

D - Below the Diagonal
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix:

  1. Swap i-th and j-th rows of the matrix;
  2. Swap i-th and j-th columns of the matrix.

You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j.

Input

The first line contains an integer n(2 ≤ n ≤ 1000) — the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk(1 ≤ xk, yk ≤ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one.

It is guaranteed that all positions are distinct.

Output

Print the description of your actions. These actions should transform the matrix to the described special form.

In the first line you should print a non-negative integer m(m ≤ 105) — the number of actions. In each of the next m lines print three space-separated integers t, i, j(1 ≤ t ≤ 2, 1 ≤ i, j ≤ n, i ≠ j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, andi and j denote the numbers of rows or columns respectively.

Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them.

Sample Input

Input
21 2
Output
22 1 21 1 2
Input
33 11 3
Output
32 2 31 1 31 1 2
Input
32 13 2
Output
0

题意:给你一个n*n矩阵,其中n-1个格子填上了数字1,交换行和列,保证所有的1都在主对角线的下面。

方法:因为没有限制最小步骤数。而且数据量也不大。直接模拟即可。方法就是,先把最后一列换成不存在1的一列,再把最后一行换成存在1的一行。这样那个小的(n-1)*(n-1)的矩阵就能保证,最多存在(n-2)个1,方法也就一样了。


代码如下:

<pre name="code" class="cpp">#include <iostream>#include <cmath>#include <cstring>#include <cstdio>#include <vector>#include <string>#include <algorithm>#include <string>using namespace std;#define MAXN 1010#define INF 1e9+7#define MODE 1000000struct res{    int op;    int x;    int y;};vector <res> r;int a[MAXN][MAXN];int cnt=0;void solve(int n){    if(n==0)        return;    for(int j=n;j>=1;j--)    {        int ok=1;        for(int i=1;i<=n;i++)        {            if(a[i][j]==1)            {                ok=0;                break;            }        }        if(ok&&j!=n)        {            for(int i=1;i<=n;i++)            {                swap(a[i][j],a[i][n]);            }            res temp;            temp.op=2;            temp.x=j;            temp.y=n;            r.push_back(temp);            break;        }        if(ok&&j==n)            break;    }    for(int i=n;i>=1;i--)    {        int ok=0;        for(int j=1;j<=n;j++)        {            if(a[i][j]){                ok=1;                break;            }        }        if(ok&&n!=i){            for(int j=1;j<=n;j++)                swap(a[i][j],a[n][j]);            res temp;            temp.op=1;            temp.x=i;            temp.y=n;            r.push_back(temp);            break;        }        if(ok&&n==i)            break;    }    solve(n-1);}int main(){    int n;    scanf("%d",&n);    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)            a[i][j]=0;    }    for(int i=0;i<n-1;i++)    {        int x,y;        scanf("%d%d",&x,&y);        a[x][y]=1;    }    solve(n);    cout<<r.size()<<endl;    for(int i=0;i<r.size();i++)    {        printf("%d %d %d\n",r[i].op,r[i].x,r[i].y);    }}


0 0