Codeforces 266C

本文解析了CodeForces上的一道题目C.Below the Diagonal,该题要求通过行和列的交换将矩阵中的1元素移动到对角线下方。文章提供了详细的解题思路及递归解决方案,并附上了C语言实现的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

暑期集训1 D题

传送门:http://codeforces.com/problemset/problem/266/C

C. Below the Diagonal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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, 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
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(其他位置是0)

每次可以交换其中任意两行或者两列

求在10^5个交换内把所以的1都交换到对角线以下(横坐标大于纵坐标)的方法

答案可以有很多,写出任意一个即可


题解:用递归的方法

因为有n-1个1,所以肯定存在一列里面全为0,此时把这一列移到最右边,然后再找一行有1的,把他和最后一行交换

此时,我们就可以把问题变成在前(n-1)*(n-1)的矩阵中把最多n-2个点移到左下角的问题

依次递归即可


下面代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;

int dmap[1005][1005];
int x[1005];
int y[1005];

struct node{
	int	a,b,c;
}ans[100005];

int m=0;

int eg(int n)
{
	int i,j;
	
	if(n==1) return 0;
	for(i=1;i<n;i++)
	{
		if(y[i]==0)
		{
			ans[m].a=2;
			ans[m].b=i;
			ans[m].c=n;
			m++;
			for(j=1;j<=n;j++)
			{
				swap(dmap[j][i],dmap[j][n]);
			}
			swap(y[i],y[n]);
			break;
		}
	}
	for(i=1;i<n;i++)
	{
		if(x[i]>0)
		{
			ans[m].a=1;
			ans[m].b=i;
			ans[m].c=n;
			m++;
			for(j=1;j<=n;j++)
			{
				swap(dmap[i][j],dmap[n][j]);
			}
			swap(x[i],x[n]);
			break;
		}
	}
	for(i=1;i<=n;i++)
	{
		if(dmap[n][i]==1)
		{
			y[i]--;
		}
	}
	eg(n-1);
}

int main()
{
	int n,i,a,b;
	
	scanf("%d",&n);
	memset(dmap,0,sizeof(dmap));
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&a,&b);
		dmap[a][b]=1;
		x[a]++;
		y[b]++;
	}
	
	eg(n);
	printf("%d\n",m); 
	for(i=0;i<m;i++)
	{
		printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值