Purification(CF-330C)
Problem Description
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 " 代表無法去凈化的點,即無法到達,只能通過 " . " 的凈化來凈化,如果能凈化整張圖,就輸出每個格子的位置,否則輸出 -1?
思路:
看題意比較難,但觀察下面的 Note 仔細想一想并沒有那么難
首先,如同樣例 2,對于某一行列均是 E 的情況,那么注定無解,因此這種情況直接輸出 -1 即可
其次,若是某幾行全是 E,那么為不與第一種情況沖突,可以保證剩下的行有足夠的 " . " 使得可覆蓋全圖,對于這種情況,進行構造即可,每一列都輸出第一個 " . " 的位置,可保證一定能覆蓋全圖
然后,若是某幾列全是 E,那么為不與第一種情況沖突,可以保證剩下的列有足夠的 " . " 使得可覆蓋全圖,對于這種情況,進行構造即可,每一行都輸出第一個 " . " 的位置,可保證一定能覆蓋全圖
對于其他的情況,一定是行、列沒有全是 E 的情況,那么同樣可以找每一行每一列第一個 " . " 的位置即可
最后,根據以上 4 種情況,進行暴力搜索即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<deque> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-6 #define INF 0x3f3f3f3f #define N 10001 #define LL long long const int MOD=998244353; const int dx[]={-1,1,0,0}; const int dy[]={0,0,-1,1}; using namespace std; char G[N][N]; int row[N],col[N];//記錄每一行每一列的E的個數 int main() {int n;cin>>n;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>G[i][j];bool flagRow=false;bool flagCol=false;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(G[i][j]=='E'){row[i]++;//第i行E的個數+1col[j]++;//第j列E的個數+1if(row[i]==n)flagRow=true;if(col[j]==n)flagCol=true;if(flagRow&&flagCol){//某行某列E的個數全是E的情況cout<<-1<<endl;return 0;}}}}if(flagRow){//行全是E的情況for(int j=1;j<=n;j++){for(int i=1;i<=n;i++){if(G[i][j]=='.'){cout<<i<<" "<<j<<endl;break;}}}}else if(flagCol){//列全是E的情況for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(G[i][j]=='.'){cout<<i<<" "<<j<<endl;break;}}}}else{//行或列不存在全是E的情況for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(G[i][j]=='.'){cout<<i<<" "<<j<<endl;break;}}}}return 0; }?
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Purification(CF-330C)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 组合计数与反演 —— 反演
- 下一篇: C++语言基础 —— STL —— 容器