Codeforces - 581C Below the Diagonal(模拟 && 贪心 && 递归)

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:

Swap i-th and j-th rows of the matrix;
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, and i 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.

Examples
inputCopy

2
1 2
outputCopy
2
2 1 2
1 1 2
inputCopy
3
3 1
1 3
outputCopy
3
2 2 3
1 1 3
1 1 2
inputCopy
3
2 1
3 2
outputCopy
0

题意分析:
这道题开始要想到方阵至少有一行一列不含1,因为不限制最短路径,数据限制不大,可以考虑贪心模拟来解决。
每次操作只需要每次将不含1的列移到方阵最右边,然后紧接将含1的行移动到最后一行即可
基本思想就是 – 每次将不含1的一列移动到最后一列,将含1的一行移动到最后一行
每次操作都能确保1存在于(n - 1)*(n - 1)的方阵中,然后递归重复即可

//终于过了orz,我昨晚想的路子没错

#include<iostream>
#include<vector>
using namespace std;
#define maxn 1010

struct res
{
    int op,x,y;
};

vector<res>r;
int a[maxn][maxn];
int cnt=0;

void solve(int n)
{
    if(n==0)
        return;
        //贪心递归将不含1的列移到当前最后一列
    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;
            }
        }       //判断一整列是不是都为1
        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;
    }
    //贪心递归将含1的行移到当前最后一行
    for(int i=n;i>=1;i--)
    {
        int ok=0;
        for(int j=1;j<=n;j++)
        {
            if(a[i][j]==1)
            {
                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==1)
        {
            break;
        }
    }
    solve(n-1);
}


int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;i<=n;i++)
        {
            a[i][j]=0;
        }
    }
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        a[x][y]=1;
    }
    solve(n);
    cout<<r.size()<<endl;
    for(int i=0;i<r.size();i++)
    {
        cout<<r[i].op<<" "<< r[i].x<<" "<<r[i].y<<endl;
    }
    return 0;
}

水题 就是代码长点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值