Codeforces Purification

time limit per test
1 second
memory limit per test
256 megabytes


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.


Sample test(s)
Input

3
.E.
E.E
.E.

Output

1 1
2 2
3 1(原为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

 

思路:

这题想通了就不难。首先先考虑什么样的情况下才会输出-1呢?只有在出现有一行并且一列全为E的时候。然后再考虑有一行全为E且每一列都至少有一个不为E的情况:

eg:EEEEE

     .  .  .  . . 

     .  .  .  .  .

     .  .  .  .  .

     .  .  .  .  .

这种情况下因为每一列至少有有一个不为E,那么就从第一列开始找到一个 ‘. ’ 输出他的坐标,然后找到第二列的‘ . ’,输出他的坐标,如此往复。

最后再考虑每一行至少有一个不为E的点且每一列至少有一个不为E的点,这时候从第一行开始找‘.’然后输出,如此往复。

注意:第一组的测试数据有问题,已用红色标记。

 

源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<string.h>
 4 char map[105][105];
 5 int row[105]={0},col[105]={0};
 6 int main()
 7 {
 8     int n,i,j;
 9     int r=0,c=0;
10     memset(row,0,sizeof(row));
11     memset(col,0,sizeof(col));
12     scanf("%d",&n);
13     for(i=0;i<n;i++)
14         scanf("%s",map[i]);
15         for(i=0;i<n;i++)
16         {
17             for(j=0;j<n;j++)
18           {
19             if(map[i][j]=='E')
20             {row[i]++;
21             col[j]++;}
22             if(row[i]==n)r=1;//出现一行全为E
23             if(col[j]==n)c=1;//出现一行全为E
24             if(r&c)
25             {printf("-1\n");
26             return 0;}
27           }
28         }
29     if(r)
30     {
31         for(j=0;j<n;j++)
32         {
33             for(i=0;i<n;i++)
34             {
35                 if(map[i][j]=='.')
36                 {printf("%d %d\n",i+1,j+1);
37                 break;}
38             }
39         }
40     }
41     else
42     for(i=0;i<n;i++)
43     {
44         for(j=0;j<n;j++)
45         {
46             if(map[i][j]=='.')
47             {
48                 printf("%d %d\n",i+1,j+1);
49                 break;
50             }
51         }
52     }
53     return 0;
54 }

 

posted @ 2013-08-06 00:39  小の泽  阅读(237)  评论(0)    收藏  举报