您的位置:首页 > 其它

HDU 4536 XCOM Enemy Unknown

2013-07-18 08:12 218 查看
题目连接:XCOM Enemy Unknown

解题思路:腾讯马拉松复赛第二场,数据量不是很大,可以直接搜索,注意回溯的时候要把所有的值恢复到搜索前的值。

#include<stdio.h>
#include<string.h>

struct A{
int fair, comp;
};

typedef struct A node;
int at[110][3], max, n, m, k;
node map[20];

int judge(){
int i;
for(i = 0; i < n; i++){
if(map[i].fair > 5){
return 0;
}
}
return 1;
}

void DFS(int depth){
int i, j, tem;
if(depth > max){
max = depth;
}
if(depth >= k){
return;
}
for(i = 0; i < 3; i++){
tem = map[at[depth][i]].fair;
map[at[depth][i]].fair -= 2;
map[at[depth][(i + 1) % 3]].fair += 2;
map[at[depth][(i + 2) % 3]].fair += 2;
for(j = 0; j < n; j++){
if(map[j].comp == map[at[depth][(i + 1) % 3]].comp && j != at[depth][(i + 1) % 3]){
map[j].fair++;
}
if(map[j].comp == map[at[depth][(i + 2) % 3]].comp && j != at[depth][(i + 2) % 3]){
map[j].fair++;
}
}
if(judge()){
DFS(depth + 1);
}
map[at[depth][i]].fair = tem;
map[at[depth][(i + 1) % 3]].fair -= 2;
map[at[depth][(i + 2) % 3]].fair -= 2;
for(j = 0; j < n; j++){
if(map[j].comp == map[at[depth][(i + 1) % 3]].comp && j != at[depth][(i + 1) % 3]){
map[j].fair--;
}
if(map[j].comp == map[at[depth][(i + 2) % 3]].comp && j != at[depth][(i + 2) % 3]){
map[j].fair--;
}
}
}
}

int main(){
int i, j, t, tot = 1;
scanf("%d", &t);
while(t--){
max = 0;
scanf("%d%d%d", &n, &m, &k);
for(i = 0; i < n; i++){
scanf("%d", &map[i].comp);
}
for(i = 0; i < n; i++){
scanf("%d", &map[i].fair);
}
for(i = 0; i < k; i++){
scanf("%d%d%d", &at[i][0], &at[i][1], &at[i][2]);
}
DFS(0);
printf("Case #%d: %d\n", tot++, max);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  深度优先搜索