UVALive 6030 Infiltration (迭代加深搜索)

题目

Good morning, agent W-12. Your mission, should you choose to accept it, is as follows. We are infiltrating the ever so insidious Association of Chaos and Mischief (ACM) in order to take down their command structure. Unfortunately, they appear to be prepared for such an eventuality, and have given their command structure an annoyingly complex design which makes our infiltration quite difficult. The ACM command structure is divided into several cells. For each pair of cells A and B, either A controls B or B controls A. But this “control” relation can be cyclic, so it could happen that A controls B and B controls C and C controls A. We can send in agents to infiltrate any particular cell, which gives us control over that cell and the cells that it controls, but not any other cells. So in the example above, infiltrating A would give us control over A and B, but not C. For a successful infiltration of the ACM, we must obtain control over all of its cells, otherwise the cells that are out of our control will discover us and start causing some of their trademark chaos and mischief. As you know, we’re on a tight spending leash from higher authority these days, so we need to execute this mission as efficiently as possible. Your mission is to figure out the minimum number of cells we need to infiltrate in order to succeed. This mission briefing will self-destruct in five hours. Good luck!

Input

The first line of a test case contains the number n of cells the ACM has (1 ≤ n ≤ 75). Each of the next n lines contains a binary string of length n where the i-th character of the j-th line is ‘1’ if cell j controls cell i, and ‘0’ otherwise (1 ≤ i, j ≤ n). The i-th character of the i-th line is ‘0’ and for i ̸= j, either the i-th character of the j-th line is ‘1’ or the j-th character of the i-th line is ‘1’, but not both.

Output

For each test case, display its case number followed by the minimum number m of cells that must be infiltrated to obtain complete control of the ACM. Then display m numbers c1, . . . , cm in any order, indicating the list of cells to infiltrate (cells are numbered from 1 to n). If more than one set of m cells gives complete control, any one will be accepted.

Sample Input

 

Sample Output

 

 

题解

 使用迭代加深搜索

 

AC代码

#include <iostream>
#include <bitset>
#include <vector>
using namespace std;

bitset<80> bit[80];
int b[80];
int n;
int ans;

bool dfs(int dep, int last, bitset<80> bt){
    if(dep > ans) return bt.count() == n;
    for(int i = last + 1; i <= n; ++i)
        if(dfs(dep + 1, b[dep] = i, bt | bit[i]))
            return true;
    return false;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    string s[80];
    int cases = 1;
    while(cin >> n){
        ans = 1;
        for(int i = 1; i <= n; ++i)
            cin >> s[i];
        for(int i = 1; i <= n; ++i){
            for(int j = 0; j < n; ++j)
                bit[i][j + 1] = s[i][j] - '0'; //
            bit[i][i] = 1;
        }
        while(!dfs(1, 0, 0))
            ans++;
        cout << "Case " << cases++ << ": " << ans;
        for(int i = 1; i <= ans; ++i)
            cout << " " << b[i];
        cout << endl;
        for(int i = 1; i <= 80; ++i)
            bit[i].reset();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Cl0ud_z/p/11281638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值