Gym 100917 A. Abstract Picture

来源:互联网 发布:电脑怎样下糖豆软件 编辑:程序博客网 时间:2024/05/22 18:22
Famous abstract painter Va Sya plans to start new painting. It will be composed as square with gridn × 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 inj-th line equals to '?', it means that color ofi-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 yc» — rowy is painted with colorc;

«v xc» — columnx is painted with colorc.

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   分析:正解还是很好想的,最后染色的行或列的颜色一定最多只有一种,于是我们倒着来每次把颜色种类小于等于1的行或列记入答案并删除,细节有点多导致那天调了一下下午没找到错,后来写了一个对拍发现是自己在处理'?'时太粗心了。 
#include <cstdio>#include <iostream>using namespace std;struct HL{    int tot,ch[27];} hl[2][3001];int tot,ans[6001][3],z[6001][2],n,num,f[3001][3001];bool jud[2][3001];char c;int Find(int x,int i){    for(int j=1;j <= 26;j++)     if(hl[x][i].ch[j] > 0) return j;    return 0;}int main(){    scanf("%d%*c",&n);    for(int i=1;i <= n;i++)    {      for(int j=1;j <= n;j++)      {        scanf("%c",&c);        if(c != '?')        {           hl[0][i].ch[c-'a'+1]++;           hl[1][j].ch[c-'a'+1]++;           f[i][j]=c-'a'+1;        }      }      scanf("%*c");    }    for(int i=1;i <= n;i++)    {        for(int j=1;j <= 26;j++)        {            if(hl[0][i].ch[j]) hl[0][i].tot++;            if(hl[1][i].ch[j]) hl[1][i].tot++;        }        if(hl[0][i].tot <= 1)        {            z[++tot][0]=0;            z[tot][1]=i;            jud[0][i]=true;        }        if(hl[1][i].tot <= 1)        {            z[++tot][0]=1;            z[tot][1]=i;            jud[1][i]=true;        }    }    int s=1,t=tot+1;    while(s != t)    {        ans[++num][0]=z[s][0];        ans[num][1]=z[s][1];        int col=ans[num][2]=Find(z[s][0],z[s][1]);        int now=z[s][0];        if(col)        {            for(int i=1;i <= n;i++)             if(!jud[(now+1)%2][i] && hl[(now+1)%2][i].ch[col])              {            if( now == 1 && f[i][z[s][1]]==0 ) continue;            if( now == 0 && f[z[s][1]][i]==0 ) continue;                hl[(now+1)%2][i].ch[col]--;                 if(hl[(now+1)%2][i].ch[col] == 0) hl[(now+1)%2][i].tot--;                if(hl[(now+1)%2][i].tot <= 1)                {                    z[t][0]=(now+1)%2;                    z[t][1]=i;                    jud[(now+1)%2][i]=true;                    t++;                }             }        }        s++;    }   for(int i=2*n; i >= 1;i--)    if(ans[i][0] == 0) cout<<"h "<<ans[i][1]<<" "<<char(max(1,ans[i][2])+'a'-1)<<endl;    else cout<<"v "<<ans[i][1]<<" "<<char(max(1,ans[i][2])+'a'-1)<<endl;}

 
0 0
原创粉丝点击