Codeforces 330C Purification【思维】

175 篇文章 2 订阅

C. Purification
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 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:

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.

Input

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.

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

Examples
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 4
5 3
Note

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的位子不能释放魔法,在空白处是可以释放魔法的,在格子(x,y)处如果释放了魔法的话,作用会影响其一行一列。

净化所有在x行和y列上的格子。

问能否使得所有格子都净化,如果可以,那么输出最小释放魔法次数的任意一种答案。否则输出-1.


思路:


这也是一道煞笔题啊。

明显对于问题来讲,我们如果能够选1~n-1行中某n-1列的点作为释放魔法的点,而最后一行都是E的话,明显只能通过选n列每一列一个点来使得整个图都净化。


所以我们只要判断:

①是否能够在每一行找到一个点施法 。

②是否能够在每一列找到一个点施法。


如果满足上述条件的一条,就是可以做到。那么最小施法次数也就是n.

否则就是-1.


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
char a[450][450];
int row[450];
int col[450];
int n;
int Slove_Row()
{
    memset(row,-1,sizeof(row));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]=='.')
            row[i]=j;
        }
    }
    for(int i=0;i<n;i++)if(row[i]==-1)return 0;
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",i+1,row[i]+1);
    }
    return 1;
}
void Slove_Col()
{
    memset(col,-1,sizeof(col));
    for(int j=0;j<n;j++)
    {
        for(int i=0;i<n;i++)
        {
            if(a[i][j]=='.')
            {
                col[j]=i;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        if(col[i]==-1)
        {
            printf("-1\n");
            return ;
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d %d\n",col[i]+1,i+1);
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)scanf("%s",a[i]);
        if(Slove_Row()==1)
        {
            continue;
        }
        else Slove_Col();
    }
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值