Guess chess game
[Problem Description]
A, B, C, D chess game for four players, before the A, B, C, D of four chess player ranking was predicted, each discharge line data, each line containing four numbers , respectively rank a, B, C, D, and each number separated by a space, 0 represents no prediction. A data as if it is: A 2431 represents a prediction of A 2, B No. 4, C No. 3, D 1 first. Another example is if the data is B: B 0204 represents a prediction of the B 2, D 4th, not A, C for the prediction. If a guess chess players rank of n, n called score, after the game results as found in the respective chess, this result is stored fifth line, the programming is obtained A, B, C, D according to the above data ranking.
[Input format
input multiple sets of test data.
Five each set of data. The first four lines represent guess players.
The fifth line indicates guess the results.
[] Output format
each data output line, represents a rank A, B, C, D, and separated by a space.
[Sample input]
. 3. 4 0. 1
2. 1. 4. 3
. 1 0 2. 4
. 1 2. 4. 3
. 1
[] sample output
2314
At first glance, I thought it was a profound problem, in reality. . . First search only 24 kinds of sorting 1 to 4, B, C and D is determined and then, if the sort is only a partial and k satisfy four groups directly output
#include<cstdio>
#include<cstring>
using namespace std;
int ans[5],f[5][5],t;
bool b[5];
int dfs(int k)
{
if(k==5)
{
int pd=0;
for(int i=1;i<=4;i++)
{
int s=0;
for(int j=1;j<=4;j++)if(f[i][j]==ans[j])s++;
if(s==t)pd++;
else break;
}
if(pd==4)
{
for(int i=1;i<4;i++)printf("%d ",ans[i]);
printf("%d\n",ans[4]);
return 0;
}
}
else
{
for(int i=1;i<=4;i++)
{
if(b[i]==false)
{
b[i]=true;
ans[k]=i;
dfs(k+1);
b[i]=false;
ans[k]=0;
}
}
}
}
int main()
{
while(scanf("%d",&f[1][1])!=EOF)
{
memset(b,false,sizeof(b));
for(int i=1;i<=4;i++)
{
int k;if(i==1)k=2;else k=1;
for(int j=k;j<=4;j++)scanf("%d",&f[i][j]);
}
scanf("%d",&t);
dfs(1);
}
return 0;
}