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

本文主要是介绍【2015-2016 XVI Open CupA】【贪心 确定性思想 正难则反 本身具有拓扑序】Abstract Picture 每行每列各染色一次 恢复颜色方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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
3
ac?
ab?
?cz
output
h 1 p
h 3 q
v 2 c
h 2 b
v 1 a
v 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|1
typedef 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 每行每列各染色一次 恢复颜色方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610412

相关文章

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

Python解决雅努斯问题实例方案详解

《Python解决雅努斯问题实例方案详解》:本文主要介绍Python解决雅努斯问题实例方案,雅努斯问题是指AI生成的3D对象在不同视角下出现不一致性的问题,即从不同角度看物体时,物体的形状会出现不... 目录一、雅努斯简介二、雅努斯问题三、示例代码四、解决方案五、完整解决方案一、雅努斯简介雅努斯(Janu

电脑找不到mfc90u.dll文件怎么办? 系统报错mfc90u.dll丢失修复的5种方案

《电脑找不到mfc90u.dll文件怎么办?系统报错mfc90u.dll丢失修复的5种方案》在我们日常使用电脑的过程中,可能会遇到一些软件或系统错误,其中之一就是mfc90u.dll丢失,那么,mf... 在大部分情况下出现我们运行或安装软件,游戏出现提示丢失某些DLL文件或OCX文件的原因可能是原始安装包

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行