题目链接: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;
}