UVa 690 流水线调度(Pipeline Scheduling)

题目:

代码:

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 200 + 10;
const int maxr = 5;

int maze[maxr][maxn];
int x[maxn], y[maxn];
int n;
char str[maxn];
int cnt;
int res;
vector<int> store;

void init() {  
    res = n * 9;
    store.clear();
    cnt = 0;
    memset(maze, 0, sizeof(maze));
    for(int i = 0; i < 5; i++) {
        cin >> str;
        for(int j = 0; j < n; j++) {
            if(str[j] == 'X') {
                x[cnt] = i;
                y[cnt++] = j;
                maze[i][j] = 1;
            }
        }
    }
    for(int d = 1; d < n; d++) {
        int ok = 1;
        for(int i = 0; i < cnt; i++) {
            if(maze[x[i]][y[i] + d]) {
                ok = 0;
                break;
           }
        }
        if(ok) {
            //cout << d << endl;
            store.push_back(d);
        }
    }
    store.push_back(n);
}


void dfs(int D, int step) {
    if( (9 - step) * store[0] + D >= res) return;
    if(step == 9) {
        res = D;
        return;
    }
    for(int i = 0; i < store.size(); i++) {
        int ok = 1;
        for(int j = 0; j < cnt; j++) {
            if(maze[x[j]][y[j] + store[i] + D]) {
                ok = 0 ;
                break;
            }
        }
        if(ok) {
            for(int j = 0; j < cnt; j++) {
                maze[x[j]][y[j] + store[i] + D] = 1;
            }
            dfs(D + store[i], step + 1);
            for(int j = 0; j < cnt; j++) {
                maze[x[j]][y[j] + store[i] + D] = 0;
            }
        }
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int kase = 0;
    while(cin >> n && n) {
        init();
        dfs(0, 0);
        cout << res + n << endl;
    }
    return 0;
}

测试样例生成器

#include<bits/stdc++.h>
#define LL long long
using namespace std;

int main() {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    const int maxn = 100000;
    srand(time(NULL));
    for(int i = 0; i < maxn; i++) {
        int w;
        w = rand() % 19 + 1;
        cout << w << endl;
        for(int j = 1; j <= 5; j++) {
            for(int k = 0; k < w; k++) {
                cout << (rand() % 4 == 0 ? '.' : 'X');
            }
            cout << endl;
        }
    }
    cout << 0 << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值