Description
Statements
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.
Sample Input
3 ac? ab? ?cz
h 1 p h 3 q v 2 c h 2 b v 1 av 3 z
题意:给一个n*n的矩阵,每个点都有一个颜色,“?”表示任意颜色,每次只能刷一行或者一列,要刷2*n 次,如果一个点被刷多次,颜色为最后一次刷的颜色,应该按照什么样的规则顺序来刷,按粉刷顺序,输出;例如 h 1 p 表示第一行刷颜色p ,“h” 表示行,中间数字表示第几行,最后一个字母表示颜色
解:如果一行(列)只有一种颜色或者全是“?”,那么这行(列)是最后刷的,因为只有最后那一次可以的哪一行(列)
是颜色统一的,那么,我们可以利用这个性质,一层一层揭开这个棋盘的颜色,先从颜色只有一种的行(列)“揭开”,当揭开这一层后,其实是一行(列)一行(列)的揭开,揭开一行(列),他会影响到每一列(行),揭开的这一行(列)颜色全部更改为“?”,那么每一列(行)的颜色个数都会减1,(原来是“?”的不算,即不用考虑),然后继续以这样的规则,揭开颜色只有一种或者0种的行(列),直到把每一行每一列“揭开”
#include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> const int maxn=1e4+10; using namespace std; struct node { char din,color; int num; node() { ; } node(char a,int c) { din=a; num=c; } node(char a,int c,char b) { din=a; color=b; num=c; } } ans[maxn]; int tot; char str[maxn][maxn]; int col[maxn],row[maxn],cl[maxn],rw[maxn]; int col_num[maxn][30],row_num[maxn][30]; void init() { memset(col_num,0,sizeof(col_num)); memset(row_num,0,sizeof(row_num)); memset(col,0,sizeof(col)); memset(row,0,sizeof(row)); tot=0; memset(cl,0,sizeof(cl)); memset(rw,0,sizeof(rw)); } node get(node temp) { int k; k=temp.num; if(temp.din=='h') { if(row[k]==0) return node('h',k,'a'); for(int i=0;i<26;i++) { if(row_num[k][i]) return node('h',k,(char)(i+'a')); } } if(col[k]==0) return node('v',k,'a'); for(int i=0;i<26;i++) { if(col_num[k][i]) return node('v',k,(char)(i+'a')); } } int main() { int n; while(scanf("%d",&n)!=-1) { for(int i=0; i<n; i++) scanf("%s",str[i]); init(); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { char temp=str[i][j]; if(temp!='?') { col_num[j][temp-'a']++;///i 列每个字母个数 row_num[i][temp-'a']++;///j 行每个字母个数 if(col_num[j][temp-'a']==1) col[j]++;/// i 列字母种数 if(row_num[i][temp-'a']==1) row[i]++; ///j 行字母种数 } } } queue<node>que; for(int ij=0;ij<n;ij++) { if(col[ij]<=1)///一列只有一或者零种字母 { que.push(node('v',ij)); cl[ij]=1;///标价这一列已经入队,被揭开过了, } if(row[ij]<=1)///一行只有一或者零种字母 { que.push(node('h',ij)); rw[ij]=1;///标价这一行已经入队,被揭开过了, } } while(!que.empty()) { node now=que.front(); que.pop(); ans[tot++]=get(now); if(now.din=='h')///行 { for(int i=0;i<n;i++) { char temp=str[now.num][i]; if(str[now.num][i]!='?')///now.num表示行,那么每一列不是“?”的都被影响 { col_num[i][temp-'a']--;///第i列减少了一个字母 str[now.num][i], if(col_num[i][temp-'a']==0)///如果有一个字母个数减为0了,那么这列字母种数减1 col[i]--; if(col[i]<=1&&!cl[i])///如果有一列字母种数小于等于1了,入队,待“揭开” { que.push(node('v',i)); cl[i]=1; } str[now.num][i]='?';///修改这个字母为“?” } } } else///行同理 { for(int i=0;i<n;i++) { char temp=str[i][now.num]; if(str[i][now.num]!='?') { row_num[i][temp-'a']--; if(row_num[i][temp-'a']==0) row[i]--; if(row[i]<=1&&!rw[i]) { que.push(node('h',i)); rw[i]=1; } str[i][now.num]='?'; } } } } ///倒着输出,因为最后涂的都是先入队, for(int i=tot-1;i>=0;i--) { printf("%c %d %c\n",ans[i].din,ans[i].num+1,ans[i].color); } } //cout << "Hello world!" << endl; return 0; }