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. 《C》数组

    数组 数组方法: var arr = [1, 2, 3]; arr.push(4)://arr='[1, 2, 3, 4]' 向末尾添加一个或者多个元素 arr.pop()://删除末位元素 var ...

  2. 做更好的自己 ——读《我是IT小小鸟》有感

    转眼间大一已经过了一大半了,到了大学,才发现初高中时父母所说的“到了大学你就轻松了···”都是骗人的.但我脑海里却一直被这个观点所支配,以至于我在大一上学期里无所事事,不知道干些什么.学习也没重视,分 ...

  3. ASP.NET MVC 4 内容映射

    文章:ASP.NET MVC 4 内容映射 地址:https://msdn.microsoft.com/zh-cn/library/gg416514(v=vs.108).aspx 模型-视图-控制器 ...

  4. Spring学习(四)—— java动态代理(JDK和cglib)

    JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他 的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托 ...

  5. iOS 通过网络请求获取图片的下载歌曲

    1.导入代理<NSURLConnectionDataDelegate> @interface ViewController ()<NSURLConnectionDataDelegat ...

  6. Cornfields POJ - 2019(二维RMQ板题)

    就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...

  7. [CF1103B]Game with modulo

    题目大意:交互题,有一个数$a(a\leqslant10^9)$,需要猜出它的值,一次询问为你两个数字$x,y(x,y\in[0,2\times10^9])$: 若$x\bmod a\geqslant ...

  8. 【JavaScript】事件

    一.前言         继续上一章的内容,继续今天的Js学习. 二.内容         事件处理程序 事件就是用户或浏览器自身执行的某种动作.而响应某个事件的函数就叫做事件处理程序 //HTML事 ...

  9. Last Position of Target

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

  10. ueditor上传图片配置

    1 去ueditor文件夹下 找 ueidtors/dialogs/image/image.html -- 配置位置大概如下: 107   utils.domReady(function(){ 108 ...