Codeforces 329A Purification

题目链接: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值