题目链接:http://codeforces.com/problemset/problem/330/C点击打开链接
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 1 through 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:
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:
给你n*n的格子 每个格子都是邪恶的 你有一个操作 选定一个格子并净化这行这列的所有格子 E点不能被选择 问所有格子能否全部被净化过 如果能输出最少数量的情况
多试试就能发现 如果能被净化的话 个数一定是n
判断能否被全部净化就是看是否行有没有可能全部被净化或者列全部净化
可以用set去重
#include <bits/stdc++.h>
using namespace std;
char mmap[111][111];
int main()
{
int n;set<int > row,col;
cin >> n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf(" %c",&mmap[i][j]);
if(mmap[i][j]=='.')
{
row.insert(i);
col.insert(j);
}
}
if(row.size()!=n&&col.size()!=n)
cout << "-1" << endl;
else
{
if(row.size()==n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(mmap[i][j]=='.')
{
cout << i << " " << j << endl;
break;
}
}
}
else if(col.size()==n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(mmap[j][i]=='.')
{
cout << j << " " << i << endl;
break;
}
}
}
}
}