ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
来源:互联网 发布:怎么获得淘宝优惠券 编辑:程序博客网 时间:2024/06/05 04:40
题意:
给定n*n的矩阵
有2个操作:
1、把一行变成X
2、把一列变成O
限制:每行(每列)只能变一次
给定结果图,开始时图无O,X,问最小操作步数(且字典序最小)
思路:
对于(i,j)这个格子,若现在涂的是 O,则去掉O这排,(让这排都变成X即可)可以直接认为(i,j)是X
所以当某排的X攒满n个时,就可以去掉这排X
直接模拟即可
先把所有 全为O或全为X的 行和列预处理出来,放到一个栈里
因为字典序最小,所以先处理列再处理行,第i列 用i+n表示, 第i行用i表示
然后给栈排个序,这样就得到处理当前情况的顺序, 入个队列,然后一个个去掉就可以了
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<vector>#include<queue>#include<set>using namespace std;#define N 1005vector<int>ans;char mp[N][N];int n, h[N], l[N];int yes[N];int Stack[N], Top;void init(){ans.clear();memset(yes, 0, sizeof yes);memset(h, 0, sizeof h);memset(l, 0, sizeof l);Top = 0;}bool cmp(int a,int b){return a>b;}//0-n-1 表示列 n-2n-1 表示行void work(){sort(Stack, Stack+Top, cmp);queue<int>q;int i, j;for(int i = 0; i < Top; i++){q.push(Stack[i]), ans.push_back(Stack[i]); yes[Stack[i]]=-1;}Top = 0;while(!q.empty()){int u = q.front(); q.pop();Top = 0;if(u<n)for(j = 0; j < n; j++){mp[j][u] = 'X';h[j]++;if(yes[j+n]!=-1 && h[j]==n)Stack[Top++] = j+n;}else {u-=n;for(j = 0; j < n; j++){mp[u][j] = 'O';l[j]++;if(yes[j]!=-1 && l[j]==n)Stack[Top++] = j;}}sort(Stack, Stack+Top, cmp);for(i = 0; i < Top; i++)q.push(Stack[i]), yes[Stack[i]] = -1, ans.push_back(Stack[i]);}for(int i = 0; i < 2*n; i++)if(yes[i]==0){puts("No solution");return;}for(int i = ans.size()-1; i>=0; i--){int u = ans[i];if(u>=n)printf("R"), u-=n;else printf("C");printf("%d",u+1);i ? printf(" ") : puts("");}}int main(){int T;scanf("%d",&T);int i, j;while(T--){scanf("%d",&n);init();for(i=0;i<n;i++)scanf("%s",mp[i]);for(i=0;i<n;i++){for(j = 0; j<n; j++)if(mp[i][j]=='X')h[i]++;if(h[i]==n) Stack[Top++] = i+n;else if(h[i]==0) yes[i+n] = -1;}for(i=0;i<n;i++){for(j = 0; j<n; j++)if(mp[j][i]=='O')l[i]++;if(l[i]==n) Stack[Top++] = i;else if(l[i]==0) yes[i] = -1;}if(Top==0){puts("No solution");continue;}work();}return 0;}/*991O3OOOOOOOOO2XXOX2XOOX*/
1 0
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- zoj 3780Paint the Grid Again 拓扑排序
- zoj 3780 Paint the Grid Again (拓扑排序)
- zoj 3780 Paint the Grid Again(拓扑排序)
- ZOJ 3780Paint the Grid Again-贪心模拟/拓扑排序
- ZOJ 3780 Paint the Grid Again(拓扑排序)
- 拓扑排序(ZOJ 3780,Paint the Grid Again)
- zoj 3870 Paint the Grid Again( 拓扑排序字典序)
- 【ZOJ 3780】Paint the Grid Again —— 模拟拓扑排序
- zoj 3780 Paint the Grid Again 11届浙江省赛 (拓扑排序)
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again (模拟)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again(topsort)
- zoj 3780 Paint the Grid Again
- ZOJ 3780 Paint the Grid Again
- ZOJ 3780Paint the Grid Again 模拟
- ZOJ 3780Paint the Grid Again
- linux内核升级图文攻略
- WPF 布局控件 之 Canvas
- Objective-C中的instancetype和id关键字
- Mysql Field * doesn't have a default value解决方法
- 构建iOS持续集成平台
- ZOJ 3780 Paint the Grid Again 拓扑排序 策略题
- Android Debug Bridge
- OPENGL入门汇总
- 黑马程序员 Java面向对象——(网络编程)
- OWL Web本体语言 指南(中文版)
- IAP java
- Qt下Excel报表生成的又一利器----QtXlsxWriter
- @synthesize和@property详解
- 回文时间(山东理工OJ)