lightoj 1037 Agent-47(状态压缩dp~)

来源:互联网 发布:温州领导干部网络学堂 编辑:程序博客网 时间:2024/05/28 06:07

也是一眼能看出状态压缩,然后就水过了

有个坑:

输入那个二维数组的时候,中间没有空格,所以要用输入字符串的方式进行转换!

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#define MAX_N 16#define INF 0x3f3f3f3fusing namespace std;int n;int health[MAX_N];int map[MAX_N][MAX_N];int dp[1 << MAX_N];char ss[MAX_N];int main(void) {    int t;    int Kase = 0;    scanf("%d", &t);    while (t--) {        scanf("%d", &n);        for (int i = 0; i < n; i++) scanf("%d", &health[i]);        for (int i = 0; i < n; i++) {            scanf("%s", ss);            for (int j = 0; j < n; j++) map[i][j] = ss[j] - '0';        }        memset(dp, INF, sizeof(dp));        dp[0] = 0;        for (int i = 0; i < n; i++) {            dp[1 << i] = health[i];        }        for (int s = 0; s < (1 << n); s++) {            // i为未被射击到的            for (int i = 0; i < n; i++) {                if (s & (1 << i)) continue;                for (int j = 0; j < n; j++) {                    // j为已被射击到的                    if (!((1 << j) & s)) continue;                    int shot = 0;                    if (map[j][i] <= 1)                        shot = health[i];                    else                        shot = ceil(((double)health[i] / double(map[j][i])));                    dp[(1 << i) | s] = min(dp[(1 << i) | s], dp[s] + shot);                }            }        }        printf("Case %d: %d\n", ++Kase, dp[(1 << n) - 1]);    }    return 0;}