CodeForces 266C—— Below the Diagonal(模拟,贪心,递归)
来源:互联网 发布:燕十八php 编辑:程序博客网 时间:2024/05/21 05:57
题目:
题意:给你一个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
- CodeForces 266C—— Below the Diagonal(模拟,贪心,递归)
- CodeForces 266CBelow the Diagonal
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits——贪心+模拟
- CodeForces 723C-Polycarp at the Radio(模拟 贪心 vector乱搞)
- codeforces--C - Anya and Ghosts(贪心+模拟)
- Codeforces 733C (模拟,贪心)
- Codeforces 749C Voting 贪心+模拟
- Codeforces 797C Minimal String 贪心+模拟
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- Codeforces 48C The Race 模拟题
- Codeforces 586C Gennady the Dentist 模拟
- Codeforces 586C Gennady the Dentist【模拟】
- Codeforces 508C - Anya and Ghosts (模拟 + 贪心)
- Codeforces,578C,贪心出错,手工模拟“或”操作
- Codeforces 509C. Sums of Digits 贪心 + 模拟
- Codeforces Round #340 (Div. 2)(C)贪心,模拟
- 【CodeForces】508C - Anya and Ghosts(贪心 & 模拟)
- [codeforces] C - Efim and Strange Grade 模拟+贪心
- HDU 1575 矩阵快速幂裸题
- looper,handler,msg
- 深入理解指针,数组的基本概念
- ruby学习之-------问题整理
- hdoj1865 1string(大数,打表,大数斐波那契数列)
- CodeForces 266C—— Below the Diagonal(模拟,贪心,递归)
- malloc,realloc,calloc的使用场合及特点,以及malloc的实现原理
- skyeye-1.3.5运行提示python: undefined symbol: PyFloat_Type 和 undefined symbol: PyUnicodeUCS2_FromUnicode
- WScript.Shell创建失败
- iOS-自定义TabBar
- ubuntu16.04安装jdk并配置
- 使用Cookie进行会话管理
- 学习笔记:ListView应用,分块右侧导航的简单分析
- HDU 5721Palace