https://vjudge.net/problem/49919/origin

题意:给你n*n只出现O和X的字符阵。有两种操作,一种操作Ri将i全变成X,一种操作Ci将i全变成O,每个不同的操作最多进行一次。现给出目标状态,求空盘下的字典序最小的操作顺序是怎样的。

思路:拿到题目看起来很复杂,但仔细读题会发现X和O只由特定操作出现,且操作只进行一次,那么单独地考虑每行每列,如i行中出现了O,且存在X(如果不存在X那么可以略去Ri操作),说明在Ri操作后进行了Cj的操作,同样的方法去考虑列,就能得到所有需要进行的行列操作相对顺序。这显然是个拓扑结构。那么将行操作或列操作看作点,建图拓扑排序即可。

关键还是在于建模,vijos上有道很相像的题目(1030),但比这道复杂一些还需要DFS。但是这道省赛题由于要求的复杂度不太严格,还可以模拟做。

但我居然一下子没有想出来用拓扑排序orz

weak-dish?

/** @Date    : 2017-03-27-21.44
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const int ost = 510; char mp[510][510];
vectorvt[1100];
int fr[510], fc[510];
int cnt[1100];
int n;
void init()
{
MMF(fr);
MMF(fc);
MMF(cnt);
for(int i = 0; i < n + ost; i++)
vt[i].clear();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(mp[i][j] == 'O')
fc[j] = 1;
else fr[i] = 1;
}
}
for(int i = 0; i < n; i++)
{
if(!fr[i]) continue;
for(int j = 0; j < n; j++)
{
if(mp[i][j] == 'O')
vt[i].PB(j + ost), cnt[j + ost]++;
}
}
for(int j = 0; j < n; j++)
{
if(!fc[j]) continue;
for(int i = 0; i < n; i++)
{
if(mp[i][j] == 'X')
vt[j + ost].PB(i), cnt[i]++;
}
}
} void top()
{
int ct = 0;
priority_queue<int, vector, greater >q;
for(int i = 0; i < n + ost; i++)
{
if(cnt[i] == 0 && vt[i].size() != 0)
q.push(i);
}
while(!q.empty())
{
int nw = q.top();
q.pop();
for(int i = 0; i < vt[nw].size(); i++)
{
int nt = vt[nw][i];
cnt[nt]--;
if(!cnt[nt])
q.push(nt);
}
if(nw < ost)
printf("%sR%d", ct==0?"":" ", nw + 1), ct = 1;
else printf("%sC%d", ct==0?"":" ", nw - ost + 1), ct = 1;
}
if(ct == 0)
printf("No solution");
printf("\n");
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = 0; i < n; i++)
{
scanf("%s", mp[i]);
}
init();
top();
}
return 0;
}

ZOJ 3780 E - Paint the Grid Again 拓扑排序的更多相关文章

  1. 【ZOJ - 3780】 Paint the Grid Again (拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  2. ZOJ - 3780-Paint the Grid Again-(拓扑排序)

    Description Leo has a grid with N × N cells. He wants to paint each cell with a specific color (eith ...

  3. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

  4. Paint the Grid Again ZOJ - 3780 拓扑

    Paint the Grid Again Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu [ ...

  5. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  6. ZOJ 3780 Paint the Grid Again

    拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...

  7. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  8. Paint the Grid Again (隐藏建图+优先队列+拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  9. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

随机推荐

  1. 又要开始新的征程了hhh(这次内容比较感兴趣)

    因为做英雄部分,既是我比较感兴趣,又很符合这次c++学习的目的,所以我很开心. 其实从小玩的RPG,即时战略和回合制游戏不算少,对于属性方法其实都算不上陌生.但是还是在网上找了一些学习资源. http ...

  2. 1029对c语言文法的理解

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  3. Scrum Meeting Beta - 2

    Scrum Meeting Beta - 2 NewTeam // 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 了解缓存的相关内容Issue #109 设计本地存储的方案Is ...

  4. 【转】Linux C 网络编程——TCP套接口编程

    地址:http://blog.csdn.net/matrix_laboratory/article/details/13669211 2. socket() <span style=" ...

  5. 使用vue-cli3新建一个项目,并写好基本配置

    1. 使用vue-cli3新建项目: https://cli.vuejs.org/zh/guide/creating-a-project.html 注意,我这里用gitbash不好选择选项,我就用了基 ...

  6. Spring注解原理

    一.注解的基本概念和原理及其简单实用 注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析注解 ...

  7. 【C++】深度探索C++对象模型读书笔记--执行期语意学(Runtime Semantics)

    对象的构造和析构: 全局对象 C++程序中所有的global objects都被放置在程序的data segment中.如果显式指定给它一个值,此object将以此值为初值.否则object所配置到的 ...

  8. iOS 监听键盘高度,输入框上升

    //设置输入框 ---<因为输入框用了get方法,所以第一次调用输入框要用self 调用>: self.textlab.frame=CGRectMake(, , , ); _textlab ...

  9. Oracle 10g DG 环境搭建详解

    环境描述:线上招聘库在物理机上,需要上云主机,于是申请两台云主机:由于云主机变态性,分配的云主机具有很多局限性:1.没有/tmp,2.没有 swap 3. /home 目录非常小:于是申请两块云硬盘, ...

  10. 【Java并发编程】之二:线程中断

    [Java并发编程]之二:线程中断 使用interrupt()中断线程 ​ 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一 ...