UVA 690 Pipeline Scheduling (搜索+位运算+剪枝)
2014-04-08 16:01
375 查看
690 -
Pipeline Scheduling
题意:10个任务,5个通道,要求每个通道都能放下10个任务且不冲突,然后每个通道的放的方式间隔都是一样的,问最短需要时间。思路:利用位运算保存每个通道的放置方法,然后去深搜,要加剪枝。详细见代码
代码:
#include <stdio.h>
#include <string.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
const int N = 35;
int n, p[5], ans, w
, wn;
char str
;
bool judge(int s0, int s1, int s2, int s3, int s4) {
return ((s0&p[0]) == 0
&& (s1&p[1]) == 0
&& (s2&p[2]) == 0
&& (s3&p[3]) == 0
&& (s4&p[4]) == 0);
}
void init() {
ans = 10 * n; wn = 0;
for (int i = 0; i < 5; i++) {
p[i] = 0;
scanf("%s", str);
for (int j = n - 1; j >= 0; j--) {
p[i] = p[i] * 2 + (str[j] == 'X');
}
}
int s0 = p[0], s1 = p[1], s2 = p[2], s3 = p[3], s4 = p[4];
for (int k = 0; k <= n; k++) {
if (judge((s0>>k), (s1>>k), (s2>>k), (s3>>k), (s4>>k))) {
w[wn++] = k;//剪枝,开w数组把不能放的位置剔除掉
}
}
}
void dfs(int s0, int s1, int s2, int s3, int s4, int d, int len) {
if (len + w[0] * (10 - d) > ans) return;//关键剪枝
if (d == 10) {
ans = min(ans, len);
return;
}
for (int i = 0; i < wn; i++) {
int ss0 = (s0>>w[i]), ss1 = (s1>>w[i]), ss2 = (s2>>w[i]), ss3 = (s3>>w[i]), ss4 = (s4>>w[i]);
if (judge(ss0, ss1, ss2, ss3, ss4)) {
dfs(ss0^p[0], ss1^p[1], ss2^p[2], ss3^p[3], ss4^p[4], d + 1, len + w[i]);
}
}
}
int main() {
while (~scanf("%d", &n) && n) {
init();
dfs(p[0], p[1], p[2], p[3], p[4], 1, n);
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- UVA 649 - You Who?(搜索+位运算+剪枝)
- UVA 11882 Biggest Number (dfs搜索+bfs 剪枝)
- UVA 11127 - Triple-Free Binary Strings(二进制+搜索+位运算)
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
- UVa 140 Bandwidth (枚举全排列&剪枝搜索)
- uva208 -Firetruck (双向搜索进行剪枝)
- UVALive-4793 Robots on Ice 搜索加剪枝
- UVa 690 - Pipeline Scheduling(回溯剪枝)
- uva 1354 位运算与搜索
- UVA 1601 Morning after holloween(搜索前的剪枝)
- UVALive 4527 Vaccination Centers 搜索+剪枝
- Regionals 2009 :: Asia - Hsinchu UVALIVE, 4527 Vaccination Centers - 搜索剪枝
- uva 140 剪枝搜索
- 【搜索+强剪枝】UVA-11882 - Biggest Number
- UVa 208 - Firetruck,双向搜索进行剪枝
- UVA 690 PipelineScheduling 位运算+dfs+剪枝
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4529 A Constrained Queen Game - 搜索剪枝
- [二进制位运算状态 隐式图搜索]UVa658 - It's not a Bug, it's a Feature!
- UVALive 6432 Influence 搜索 剪枝
- 紫书搜索 习题7-3 UVA - 211 The Domino Effect 搜索dfs