Gym 100917 A. Abstract Picture

本博客探讨了著名抽象画家VaSya在创作时所面临的挑战,即如何使用连续技术为方形网格画作(边长为n×n单位格)染色,其中部分单位格的颜色已预先定义。通过倒序处理,每次选择颜色种类少于或等于1的行或列进行着色,并删除对应单元格,最终得到合适的着色序列。实例分析展示了求解过程,涉及细节处理和逻辑思考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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
3
ac?
ab?
?cz
Output
h 1 p
h 3 q
v 2 c
h 2 b
v 1 a
v 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;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值