2018刑侦科推理试题
先来看看2018刑侦科推理试题。
直接做的话,因为有很多情况,所以会很麻烦。但如果我们用程序来做的话,那就好很多了。
根据题意,我们可以用dfs来求答案,枚举每一种情况,若有 n n n道题,则时间复杂度为 O ( n × 4 n ) O(n\times 4^n) O(n×4n)。
code
#include<bits/stdc++.h>
using namespace std;
int n=10,f,a[15],c[5],p[5][5]={{0,0,0,0,0},{0,1,0,1,1},{0,0,1,0,1},{0,1,0,1,0},{0,1,1,0,1}};
bool pd1(){
return 1;
}
bool pd2(){
return (a[2]+2)%4==a[5]%4;
}
bool pd3(){
if(a[3]==1) return a[3]!=a[6]&&a[3]!=a[2];
else if(a[3]==2) return a[6]!=a[2]&&a[6]!=a[4];
else if(a[3]==3) return a[2]!=a[4]&&a[2]!=a[3];
else return a[4]!=a[3]&&a[4]!=a[6];
}
bool pd4(){
if(a[4]==1) return a[1]==a[5];
else if(a[4]==2) return a[2]==a[7];
else if(a[4]==3) return a[1]==a[9];
else return a[6]==a[10];
}
bool pd5(){
if(a[5]==1) return a[5]==a[8];
else if(a[5]==2) return a[5]==a[4];
else if(a[5]==3) return a[5]==a[9];
else return a[5]==a[7];
}
bool pd6(){
if(a[6]==1) return a[8]==a[2]&&a[8]==a[4];
else if(a[6]==2) return a[8]==a[1]&&a[8]==a[6];
else if(a[6]==3) return a[8]==a[3]&&a[8]==a[10];
else return a[8]==a[5]&&a[8]==a[9];
}
bool pd7(){
int t=1;
if(c[2]<c[t]) t=2;
if(c[3]<c[t]) t=3;
if(c[4]<c[t]) t=4;
if(a[7]==1) return t==3;
else if(a[7]==2) return t==2;
else if(a[7]==3) return t==1;
else return t==4;
}
bool pd8(){
if(a[8]==1) return p[a[1]][a[7]];
else if(a[8]==2) return p[a[1]][a[5]];
else if(a[8]==3) return p[a[1]][a[2]];
else return p[a[1]][a[10]];
}
bool pd9(){
if(a[9]==1) return (a[1]==a[6])^(a[6]==a[5]);
else if(a[9]==2) return (a[1]==a[6])^(a[10]==a[5]);
else if(a[9]==3) return (a[1]==a[6])^(a[2]==a[5]);
else return (a[1]==a[6])^(a[9]==a[5]);
}
bool pd10(){
int t1=1,t2=1,t3;
if(c[2]>c[t1]) t1=2;
if(c[3]>c[t1]) t1=3;
if(c[4]>c[t1]) t1=4;
if(c[2]<c[t2]) t2=2;
if(c[3]<c[t2]) t2=3;
if(c[4]<c[t2]) t2=4;
t3=c[t1]-c[t2];
if(a[10]==1) return t3==3;
else if(a[10]==2) return t3==2;
else if(a[10]==3) return t3==4;
else return t3==1;
}
bool pd(){
return (pd1()&&pd2()&&pd3()&&pd4()&&pd5()&&
pd6()&&pd7()&&pd8()&&pd9()&&pd10());
}
void dfs(int t){
for(int i=1;i<=4;i++){
a[t]=i;++c[i];
if(t<n){
dfs(t+1);
if(f) return;
}
else{
if(pd()){
for(int j=1;j<=n;j++)
printf("%c ",'A'+a[j]-1);
f=1;
return;
}
}
--c[i];
}
}
int main()
{
dfs(1);
return 0;
}
输出文件
正确答案
BCACA CDABA