题目链接:Codeforces 329A Purification
开始被题目中的最少迷惑了,就是这句话where x is the minimum possible number of spells,然后就想搜索了。其实还是没读明白题意。
这道题要求的最少其实意义是不要重复,而不是选择最优。
最少肯定是n,因为每一行、每一列都需要净化。
所以可以设一个集合表示行数,若第i行存在一点可释放魔法,即将i加入集合,同样设另一个集合表示列数,只要行集合包涵1---n或者是列集合包涵1---n即可净化,任意输出一种方案即可,否则输出-1。
#include <iostream>
using namespace std;
const int MAX_N = 100 + 10;
char _map[MAX_N][MAX_N];
int row[MAX_N],column[MAX_N];
int n;
int main()
{
cin >> n;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
cin >> _map[i][j];
int cnt = 0;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(_map[i][j] == '.')
{
cnt++;
row[i] = j;
break;
}
}
}
if(cnt == n)
{
for(int i = 1;i <= n;i++)
cout << i << " " << row[i] << endl;
return 0;
}
cnt = 0;
for(int j = 1;j <= n;j++)
{
for(int i = 1;i <= n;i++)
{
if(_map[i][j] == '.')
{
cnt++;
column[j] = i;
break;
}
}
}
if(cnt == n)
{
for(int j = 1;j <= n;j++)
cout << column[j] << " " << j << endl;
return 0;
}
cout << -1 << endl;
return 0;
}