UVA 296 - Safebreaker

来源:互联网 发布:网络歌手想你的感觉 编辑:程序博客网 时间:2024/06/07 15:32


We are observing someone playing the game of Mastermind. The object of this game is to find a secret code by intelligent guess work, assisted by some clues. In this case the secret code is a 4-digit number in the inclusive range from 0000 to 9999, say ``3321''. The player makes a first random guess, say ``1223'' and then, as for each of the future guesses, gets a clue telling him how good his guess is. A clue consists of two numbers: the number of correct digits (in this case 1: the ``2'' at the third position) and the additional number of digits guessed correctly but in the wrong place (in this case 2: the ``1'' and the ``3''). The clue would in this case be: ``1/2''.

Write a program that given a set of guesses and corresponding clues, tries to find the secret code.

Input Specification

The first line of input specifies the number of test cases (N) your program has to process. Each test case consists of a first line containing the number of guesses G ( tex2html_wrap_inline35 ), and G subsequent lines consisting of exactly 8 characters: a code of four digits, a blank, a digit indicating the number of correct digits, a `/' and a digit indicating the number of correct but misplaced digits.

Output Specification

For each test case, the output contains a single line saying either:

  • impossible if there is no code consistent with all guesses.
  • the secret code if there is exactly one code consistent with all guesses.
  • indeterminate if there is more than one code which is consistent with all guesses.

Example Input

469793 0/12384 0/26264 0/13383 1/02795 0/00218 1/011234 4/011234 2/226428 3/01357 3/0

Example Output

34111234indeterminateimpossible

枚举0-9999匹配上每次guess就是code.

#include <iostream>#include <cstdio>#include <memory.h>#include <vector>#include <string>using namespace std;string tb[10000];void init(){for (int i=0;i<=9999;++i){char o=i/1000+'0',t=(i/100)%10+'0',th=(i/10)%10+'0',f=i%10+'0';tb[i].push_back(o),tb[i].push_back(t),tb[i].push_back(th),tb[i].push_back(f);}}int n;int main(){init();int t;scanf("%d",&t);while (t--){vector<string>v,ans;int correctPos[10],wrongPos[10];scanf("%d",&n);for (int i=0;i<n;++i){char buf[5];scanf("%s %d/%d",buf,&correctPos[i],&wrongPos[i]);v.push_back(string(buf));}for (int i=0;i<=9999;++i){int f=1;for (int j=0;j<n;++j){int correctNum=0,wrongNum=0;bool vis1[4]={0},vis2[4]={0};for (int k=0;k<4;++k){if(v[j][k]==tb[i][k]){//统计正确位置的个数correctNum++;vis1[k]=1;vis2[k]=1;}}for (int k=0;k<4;++k){for (int m=0;m<4;++m){if(v[j][m]==tb[i][k]&&!vis1[k]&&!vis2[m]){//统计错误位置的个数wrongNum++;vis1[k]=1;vis2[m]=1;break;}}}if(correctNum!=correctPos[j]||wrongNum!=wrongPos[j]){f=0;break;}}if(f)ans.push_back(tb[i]);if(ans.size()>1)break;}if(ans.size()==1){printf("%s\n",ans[0].c_str());}else if(ans.size()>1){printf("indeterminate\n");}else printf("impossible\n");}return 0;}