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

题目:

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

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
2
1 2
Output
2
2 1 2
1 1 2
Input
3
3 1
1 3
Output
3
2 2 3
1 1 3
1 1 2
Input
3
2 1
3 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 1000000
struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值