CF 329A(Purification-贪心-非DLX)
1 second
256 megabytes
standard input
standard output
You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n × n grid. The rows are numbered 1 through n from top to bottom, and the columns are numbered 1through n from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door:
The cleaning of all evil will awaken the door!
Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells.
The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile — then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once.
You would like to purify all n × n cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell.
Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way.
The first line will contain a single integer n (1 ≤ n ≤ 100). Then, n lines follows, each contains n characters. The j-th character in the i-th row represents the cell located at row i and column j. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise.
If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts x "Purification" spells (where x is the minimum possible number of spells), output x lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell.
3
.E.
E.E
.E.
1 1
2 2
3 3
3
EEE
E..
E.E
-1
5
EE.EE
E.EE.
E...E
.EE.E
EE.EE
3 3
1 3
2 2
4 4
5 3
The first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which "Purification" is cast. Yellow tiles are the tiles being purified as a result of the current "Purification" spell. Green tiles are tiles that have been purified previously.
In the second example, it is impossible to purify the cell located at row 1 and column 1.
For the third example:
这题是NOI 2013 被虐残以后 做的第一次Div 1,马上就掉回蓝了轻囧
这题一开始以为DLX各种弱。。。。。
印度尼西亚的人怎么都出这种游戏题目、、、、
进入正解模式:
我们发现出现‘十‘E 是无解的
否则必然存在n次Purification(净化)的解.
反证:
假设最优解为n+1
那么必然有一行/一列净化了2次,删掉那次不影响结果
假设存在答案为n的净化方案:
因为只做了n遍,必然每行取一个(%号表示净化点)
%E
%E
或每列:
%%
EE
所以考虑这2种情况,随便取即可,反之无解
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,a[MAXN]={0},d[MAXN]={0};
bool b[MAXN][MAXN]={0};
int main()
{
// freopen("Purification.in","r",stdin);
cin>>n;
For(i,n) For(j,n)
{
char c;
while (cin>>c)
{
if (c=='.') b[i][j]=1,a[i]=j,d[j]=i;
else if (c=='E') b[i][j]=0;
else continue;
break;
}
}
/*
For(i,n)
{
bool bo=0;
For(j,n) if (b[i][j]) bo=1;
if (!bo) {puts("-1");return 0;}
bo=0;
For(j,n) if (b[j][i]) bo=1;
if (!bo) {puts("-1");return 0;}
}*/
//For(i,n) cout<<a[i]<<' ';cout<<endl;
//For(i,n) cout<<d[i]<<' ';cout<<endl; bool bo=0;
For(i,n) if (!a[i]) bo=1;
if (!bo)
{
For(i,n) cout<<i<<' '<<a[i]<<endl;
return 0;
} bo=0;
For(i,n) if (!d[i]) bo=1;
if (!bo)
{
For(i,n) cout<<d[i]<<' '<<i<<endl;
return 0;
} puts("-1"); return 0;
}
CF 329A(Purification-贪心-非DLX)的更多相关文章
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- CF 949D Curfew——贪心(思路!!!)
题目:http://codeforces.com/contest/949/problem/D 有二分答案的思路. 如果二分了一个答案,首先可知越靠中间的应该大约越容易满足,因为方便把别的房间的人聚集过 ...
- CF Covered Path (贪心)
Covered Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CF 389 E 贪心(第一次遇到这么水的E)
http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...
- CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树
http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...
- cf 之lis+贪心+思维+并查集
https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...
- cf C. Purification
http://codeforces.com/contest/330/problem/C 这道题分三种情况.有一行全是E,有一列全是E,还有一种为无解的情况. #include <cstdio&g ...
- CF# 260 A. Laptops
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the m ...
- Codeforces Round #401 (Div. 2) 离翻身就差2分钟
Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...
随机推荐
- 迈出第一步,Hexo博客搭建
很早之前看到别人的博客就总想着自己之后也要搭一个,最近突然来了干劲,就开始搭起了博客.不过搭博客还真是一个累活,失败了不下十次,用了好几天的时间,感觉自己在浪费时间,但是看到现在博客终于能用了,非常开 ...
- 心跳包(HeartBeat)
http://itindex.net/detail/52922-%E5%BF%83%E8%B7%B3-heartbeat-coderzh 几乎所有的网游服务端都有心跳包(HeartBeat或Ping) ...
- Nutch源码阅读进程5
看nutch的源码仿佛就是一场谍战片,而构成这精彩绝伦的谍战剧情的就是nutch的每一个从inject->generate->fetch->parse->update的环节,首 ...
- 计蒜客 NOIP 提高组模拟竞赛第一试 补记
计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...
- poj 3268 最短路dijkstra *
题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最 ...
- BZOJ4268 : 小强的书架
首先将所有高度乘上10,设f[i]为将前i本书放入书架的最小高度,则 \[\begin{eqnarray*}f[i]&=&\min(f[j-1]+first(j,i)+second(j ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- 编译 php-memcache 扩展时提示Cannot find autoconf
下载memcache扩展 http://pecl.php.net/package/memcache ,到 /usr/local/src目录下并解压 [root@bogon src]# .tgz [ro ...
- 关于bootstrap的treeview不显示多选(复选框)的问题,以及联动选择的问题,外加多选后取值
最近做项目用到了treeview.因为涉及到多选的问题,很是棘手,于是乎,我决定查看原生JS,探个究竟.需要引用官方的bootstrap-treeview.js都知道吧,对于所需要引用的,我就不多说了 ...
- [翻译] 10 个实用的 Git 高级命令
1. 输出最后一次提交的改变 这个命令,我经常使用它 来发送其他没有使用 git 的人来检查或者集成所修改的.它会输出最近提交的修改内容到一个 zip 文件中. git archive -o ../u ...