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. 《Linux内核与分析》第四周

    20135130王川东 一.用户态.内核态和中断处理过程 CPU的几种不同的执行级别: 高执行级别下,代码可以执行特权指令,访问任意的物理地址,这种执行级别对应内核态: 低级别执行状态下,代码的掌握范 ...

  2. 结对作业_Two

    Part 1.前言 (附:本次编码涵盖的所有功能均为java语言实现) 结对项目作业 结对同学高裕翔的博客 个人github传送门 博文pdf链接 Part 2.具体分工 本次的结对作业我们简单的拆分 ...

  3. HDU 5172 GTY's gay friends 线段树+前缀和+全排列

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...

  4. 【Leetcode】 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  5. 图文详解 IntelliJ IDEA 15 创建 Maven 构建的 Java Web 项目(使用 Jetty 容器)

    图文详解 IntelliJ IDEA 15 创建 maven 的 Web 项目 搭建 maven 项目结构 1.使用 IntelliJ IDEA 15 新建一个项目.  2.设置 GAV 坐标  3. ...

  6. Spring Cloud 之 Eureka

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

  7. 【UNIX环境编程、操作系统】孤儿进程和僵尸进程

    基本概念: 在类UNIX系统中,僵尸进程是指完成执行(通过exit系统调用,或运行时发生致命错误或收到终止信号所致)但在操作系统的进程表中仍然有一个进程表表项(进程控制块PCB),处于"终止 ...

  8. 第88天:HTML5中使用classList操作css类

    在HTML5 API里,页面DOM里的每个节点上都有一个classList对象,程序员可以使用里面的方法新增.删除.修改节点上的CSS类.使用classList,程序员还可以用它来判断某个节点是否被赋 ...

  9. hbase windows安装

    下载目前最新版本 http://mirrors.hust.edu.cn/apache/hbase/stable/ 最新版本 hbase-1.2.6 1. 解压到D:\software\hbase-1. ...

  10. 小Z的袜子 题解报告【莫队】

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只 ...