【2015-2016 XVI Open CupA】【贪心 确定性思想 正难则反 本身具有拓扑序】Abstract Picture 每行每列各染色一次 恢复颜色方案
来源:互联网 发布:jquery json转字符串 编辑:程序博客网 时间:2024/06/16 10:19
Famous abstract painter Va Sya plans to start new painting. It will be composed as square with grid n × n, where each unit square is painted by some color.
Va Sya already defined the colors for some unit squares. Color of other squares does not matter for him.
For this work Va Sya is planning use the continuous technics: he paints whole row or whole column in some color. Moreover, each row and each column must be painted exactly once, so each unit square will be painted twice and its final color will be the last of two used colors.
Help Va Sya to find appropriate sequence of paints.
First line of the input contains one integer n — length of the painting side in units (1 ≤ n ≤ 3000).
Each of the next n lines contains n characters. If i-th character in j-th line equals to '?', it means that color of i-th cell in j-th row of painting does not matter. Otherwise it contains lowercase English letter from 'a' to 'z' inclusively, which represents the color of corresponding cell (it is well known that Va Sya uses only 26 colors).
Print 2n lines, i-th of those lines contains description of i-th paint in the following format:
«h y c» — row y is painted with color c;
«v x c» — column x is painted with color c.
Rows are numbered sequentially upside down, columns are numbered sequentially leftside right, so upper left corner is on intersection of row 1 and column 1. Each row and each column must be mentioned in the output exactly once.
You may assume that there exists at least one solution for the given input. If there are several correct solutions, print any of them.
3ac?ab??cz
h 1 ph 3 qv 2 ch 2 bv 1 av 3 z
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 3030, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n;char a[N][N];queue< pair<int,int> >q;int sum[N + N];int num[N + N][128];pair<int,int> ans[N + N];bool e[N + N];void inq(int p, int c){q.push(MP(p, c));e[p] = 1;}void solve(){while (!q.empty())q.pop();MS(e, 0);for (int i = 1; i <= n+n; ++i)if (sum[i]){for (char j = 'a'; j <= 'z'; ++j){if (num[i][j] == sum[i])inq(i, j);}}int o = n + n;while (!q.empty()){int p = q.front().first;int c = q.front().second;q.pop();ans[o--] = MP(p,c);if (p <= n)//行->列{for (int j = 1; j <= n; ++j)if(a[p][j]!='?'&&!e[j+n]){if (e[j + n])continue;--sum[j + n];--num[j + n][a[p][j]];if (sum[j + n])for (char k = 'a'; k <= 'z'; ++k){if (sum[j + n] == num[j + n][k])inq(j + n, k);}}}else//列->行{for (int i = 1; i <= n;++i)if(a[i][p-n]!='?')//一行行来{if (e[i])continue;--sum[i];--num[i][a[i][p - n]];if (sum[i] )for (char k = 'a'; k <= 'z';++k){if (sum[i] == num[i][k])inq(i,k);}}}}for (int i = n+n; i >= 1; --i)if (e[i] == 0)ans[o--] = MP(i, 'a');for (int i = 1; i <= n+n; ++i){if (ans[i].first <= n)printf("h %d %c\n", ans[i].first, ans[i].second);else printf("v %d %c\n", ans[i].first-n, ans[i].second);}}int main(){while (~scanf("%d", &n)){MS(num, 0); MS(sum, 0);for (int i = 1; i <= n; ++i)scanf("%s", a[i]+1);for (int i = 1; i <= n; ++i){for (int j = 1; j <= n; ++j)if (a[i][j] != '?'){++sum[i];++num[i][a[i][j]];++sum[n+j];++num[n+j][a[i][j]];}}solve();}return 0;}/*【trick&&吐槽】1,这种sb题我竟然搞混了做法。想了什么二分图匹配啦,网络流啦,拓扑排序啦一系列做法。然而正解却是——倒着来思考,直接按照确定性原则贪心选择就好了。2,写入队操作的时候没有把该行或列直接置否,导致了多次入队,崩盘>_<果然是要把操作写得函数化的好。【题意】给你一个n(3000)*n的正方形。每个格子被涂了一定的颜色。颜色一共只有'a'~'z'共计26种,有些颜色任意,用'?'表示。涂色实际上恰好图了2n次,每行每列都涂色了一次。然而顺序和涂了什么色却不知道。现在给你这个图,让你确定一种合法的涂色方案【类型】贪心遵循确定性原则分析问题【分析】我们发现,我们最后一次涂色,该行或该列的颜色一定全部相同。如果当前一行或一列的颜色完全相同,该行或列就可以是当前最后一次涂色的。我们直接暴力,枚举所有行或列,取出所有可能是最后一次涂色的。消除该次涂色对相应行或列的影响,并继续这个类似于拓扑排序的过程。倒序输出,就可以AC了。【时间复杂度&&优化】O(n^2*26)*/
- 【2015-2016 XVI Open CupA】【贪心 确定性思想 正难则反 本身具有拓扑序】Abstract Picture 每行每列各染色一次 恢复颜色方案
- Codeforces Round #192 (Div. 1) A. Purification(贪心模拟+清除每行每列)
- 【2015-2016 XVI Open CupJ】【DP bitset记录方案】Judgement n人投票前后权威值改变是否有实际区别
- a【5】【5】每行每列相加
- 【2015-2016 XVI Open CupC】【暴力】Constant Ratio 等差数列前若干项恰好为n
- 【2015-2016 XVI Open CupD】【ST-RMQ】dir -C 文件划分成最少的行
- [原创]定义每行每列显示的个数
- matlab 找矩阵中每行或每列的最大值
- matlab 找矩阵中每行或每列的最大值
- 获取table表格每行每列的数据
- 二维数组求平均数(每行/每列)
- 【2015-2016 XVI Open CupH】【STL大融合】Hierarchy 公司人来人往 公寓入住进出 求公司老大与寝室长
- 【2015-2016 XVI Open CupL】【找规律】Liesbeth and the String 字符串变换何时变为长度1
- Open DJ备份与恢复方案
- 1-9,九个数填入3*3的格子中,使每行每列总和相同
- 幻方的实现,矩阵每行每列每个对角线之和相等
- datatable取每行的每列的值,就这办法了,头疼
- 趣味魔方 每行每列对角线的和相等 C语言
- 泛型继承接口Demo
- 微信公众号编辑模式应用
- ubuntu 14.04 安装phpMyAdmin + lighttpd
- 批梯度下降法和随机梯度下降法(SGD)
- 使用mount挂载虚拟机所在的img文件
- 【2015-2016 XVI Open CupA】【贪心 确定性思想 正难则反 本身具有拓扑序】Abstract Picture 每行每列各染色一次 恢复颜色方案
- Codeforces--630K--Indivisibility(容斥)
- 二叉搜索树与双向链表Java
- Android源代码目录组成介绍-android学习之旅(97)
- php常用Output和ptions/Info函数集介绍
- 错误 finished with non-zero exit value 1
- icon图标制作
- 【FastDFS分布式文件系统之二】:FastDFS小文件上传性能测试及Python客户端上传操作
- 【零基础入门学习Python笔记001】我和Poyhon的第一次亲密接触:课后测试集答案