【2015-2016 XVI Open CupA】【贪心 确定性思想 正难则反 本身具有拓扑序】Abstract Picture 每行每列各染色一次 恢复颜色方案

来源:互联网 发布:jquery json转字符串 编辑:程序博客网 时间:2024/06/16 02:26

A. Abstract Picture
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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).

Output

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.

Example
input
3ac?ab??cz
output
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)*/



0 0
原创粉丝点击