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;
}

优质内容筛选与推荐>>
1、全排列输出 递归算法
2、JS 对象封装的常用方式
3、排序算法复杂度与稳定性
4、第3章 MVC模式
5、超好玩的推盒子


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号