猜数字 | ||
[ Submit Code ] [ Top 20 Runs ] | ||
Acceteped : 43 | Submit : 96 | |
Time Limit : 10000 MS | Memory Limit : 65536 KB | |
Description | ||
题目描述猜数字游戏是这样的,主持人先从1~N个数字,随意取4个数字,组成一个序列。 然后你猜了K次,每次你依次报4个数字。主持人告诉你数字和位置都对的数字有x个,数字对但位置不对的数字有y个。 请问你满足这K次询问的数字序列有几个?分别是什么? 输入有多个样例。第一行是一个整数N,(4≤N≤50),如果N为0,表示输入结束,这个样例不用处理。 第二行是一个整数K,(10≤k≤100),表示询问的次数。 以后的K行,每行6个整数,前4个表示你报的数字序列,每个数字在1到N之间,且没有重复,最后两个整数为x和y。(0≤x+y≤N) 输出每个样例按数字序列的字典序输出满足序列,每个序列占一行,每个整数之间用一个空格隔开,行末没空格。最后输出这些序列的总数。 样例输入4 样例输出1 2 3 4 | ||
Sample Input | ||
Sample Output | ||
Source | ||
[ Submit Code ] [ Top 20 Runs ] |
思路:因为n最大就50,时间给的比较充足(10s),所以就将所有解枚举一遍,不断尝试判断,符合条件就保存下来并计数,最后输出即可。
AC代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int maxm = 2005;
struct node{
int t[4];
node(){
memset(t, 0, sizeof(t));
}
} ans[maxm];
int num[maxn][4], t[4];
int n, k, x[maxn], y[maxn];
bool check(){
for (int i=0; i<k; ++i){
int t1=0, t2=0;
for (int j=0; j<4; ++j){
if (t[j] == num[i][j])
++t1;
else
for (int s=0; s<4; ++s)
if (s!=j && t[j]==num[i][s])
++t2;
}
if (t1!=x[i] || t2!=y[i])
return false;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while (~scanf("%d", &n) && n){
scanf("%d", &k);
for (int i=0; i<k; ++i){
for (int j=0; j<4; ++j)
scanf("%d", &num[i][j]);
scanf("%d%d", x+i, y+i);
}
int cnt = 0;
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j)
if (i != j){
for (int l=1; l<=n; ++l)
if (l!=i && l!=j){
for (int s=1; s<=n; ++s)
if (s!=i && s!=j && s!=l){
t[0]=i, t[1]=j;
t[2]=l, t[3]=s;
if (check()){
ans[cnt].t[0]=i, ans[cnt].t[1]=j;
ans[cnt].t[2]=l, ans[cnt].t[3]=s;
++cnt;
}
}
}
}
for (int i=0; i<cnt; ++i){
for (int j=0; j<4; ++j){
if (j > 0)
printf(" ");
printf("%d", ans[i].t[j]);
}
printf("\n");
}
printf("%d\n", cnt);
}
return 0;
}