http://www.nfls.com.cn:20035/submission/781868
#include<bits/stdc++.h>
using namespace std;
int N, ct[45], b[25], ans, a[45][5];
void dfs(int t, int sum){//sum几张非对子
if(sum >= 5) return;
if(t == 4*N+1 || sum < 5){
ans = max(ans, (t-sum)/2);//最多的对数
}
for(int i = 1; i <= 4; i++){
if(ct[i] + 1 <= N){
ct[i]++;
int x = a[ct[i]][i];
if(!b[x]){
b[x] = 1;
dfs(t+1, sum+1);
b[x] = 0;
}
else if(b[x] % 2 == 1){
b[x] = 0;
dfs(t+1, sum-1);
b[x] = 1;
}
ct[i]--;//第i列--
}
}
}
int main(){
cin>>N;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= 4; j++){
scanf("%d",&a[i][j]);
}
}
dfs(1,0);
cout<<ans<<endl;
return 0;
}
right:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int N, ct[45], b[25], ans, a[45][5], f[45][45][45][45];
void dfs(int t, int sum){//每层取一个数字,sum几张非对子
if(sum >= 5) return;
if(f[ct[1]][ct[2]][ct[3]][ct[4]]) return;
f[ct[1]][ct[2]][ct[3]][ct[4]] = 1;
ans = max(ans, (t-sum)/2);//最多的对数
for(int i = 1; i <= 4; i++){
if(ct[i] + 1 <= N){
ct[i]++;
int x = a[ct[i]][i];
if(!b[x]){
b[x] = 1;
dfs(t+1, sum+1);
b[x] = 0;
}
else if(b[x] % 2 == 1){
b[x] = 0;
dfs(t+1, sum-1);
b[x] = 1;
}
ct[i]--;//第i列--
}
}
}
int main(){
cin>>N;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= 4; j++){
scanf("%d",&a[i][j]);
}
}
dfs(1,0);
cout<<ans<<endl;
return 0;
}