CF 329A (Purification-greedy-non-DLX)

Source: Internet
Author: User

Category: greedy card and board simulation 103 people read comments (0) collect reports

A. Purification
Time limit per test1 second
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
You are an adventurer currently journeying inside an edevil temple. after defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an n blocks x blocks n grid. the rows are numbered 1 through n from top to bottom, and the columns are numbered 1 through n from left to right. at the far side of the room lies a door locked with edevil magical forces. the following inscriptions are written on the door:


The cleaning of all edevil 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 edevil. You showould 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 wowould like to purify all n rows × limit 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 certainly more edevil than the other tiles. you cannot cast the "Purification" spell on those participant ularly more edevil 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.

Input
The first line will contain in a single integer n (1 rows ≤ limit n rows ≤ limit 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 particle ly more edevil cell, and '. 'Otherwise.

Output
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 shoshould consist of two integers denoting the row and column numbers of the cell on which you shoshould cast the "Purification" spell.

Sample test (s)
Input
3
. E.
E. E
. E.
Output
1 1
2 2
3 3
Input
3
EEE
E ..
E. E
Output
-1
Input
5
EE. EE
E. EE.
E... E
. EE. E
EE. EE
Output
3 3
1 3
2 2
4
5 3 Note
The first example is already strated as follows. purple tiles are edevil 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 previusly.

 


In the second example, it is impossible to purify the cell located at row 1 and column 1.

For the third example:

 

 

 

 

This is the first Div 1 made after NOI 2013 is abused and left blue right away.

At first, I thought DLX was weak .....

How do people in Indonesia come up with this kind of game question ,,,,

 


Enter the normal solution mode:

We found that '10' E is unsolvable.

Otherwise, there must be n Purification solutions.

Counterevidence:

Assume that the optimal solution is n + 1.

Therefore, one row/column must have been purified twice. Deleting the column does not affect the result.

Suppose there is a purification solution with the answer n:

Because only n times are performed, one row is required (the "%" number indicates the purification point)

% E

% E

Or each column:

%

EE

 


So consider the two cases, just take them as needed, and vice versa.

 

#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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.