HDOJ 1172 猜数字

Problem Description
猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么。每猜一个数,计算机都会告诉玩家猜对几个数字,其中有几个数字在正确的位置上。
比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。
现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。
 

 

Input
输入数据有多组。每组的第一行为一个正整数N(1<=N<=100),表示在这段对话中共有N次问答。在接下来的N行中,每行三个整数A,B,C。gameboy猜这个四位数为A,然后计算机回答猜对了B个数字,其中C个在正确的位置上。当N=0时,输入数据结束。
 

 

Output
每组输入数据对应一行输出。如果根据这段对话能确定这个四位数,则输出这个四位数,若不能,则输出"Not sure"。
 

 

Sample Input
6
4815 2 1
5716 1 0
7842 1 0
4901 0 0
8585 3 3
8555 3 2
2
4815 0 0
2999 3 3
0
 

 

Sample Output
3585
Not sure
 

 

  1 #include <iostream>
  2 #include <algorithm>
  3 
  4 using namespace std;
  5 
  6 int cc[10000][4];
  7 int nth;
  8 
  9 void dfs(int cur,int* a,int *c,int num)
 10 {
 11     if(cur==4)
 12     {
 13        for(int i=0;i<4;i++)
 14            cc[nth][i]=c[i];
 15         nth++;
 16         return ;
 17     }
 18     else for(int i=0;i<num;i++)
 19     {
 20             c[cur]=a[i];
 21             dfs(cur+1,a,c,num);
 22     }
 23 }
 24 
 25 
 26 void func(char*a,int* b,int &x,int &y)
 27 {
 28     for(int i=0;i<4;i++)
 29     {
 30         if(a[i]-48==b[i])
 31             y++;
 32     }
 33     int vis[4]={0};
 34     for(int i=0;i<4;i++)
 35     {
 36         for(int j=0;j<4;j++)
 37         {
 38             if(b[i]==a[j]-48&&vis[j]==0)
 39             {
 40                 vis[j]=1;
 41                 break;
 42             }
 43         }
 44     }
 45 
 46     for(int i=0;i<4;i++)
 47     {
 48         if(vis[i]==1)
 49             x++;
 50     }
 51 }
 52 
 53 
 54 
 55 struct kk
 56 {
 57     char c[4];
 58     int x;
 59     int y;
 60 }lcy[101];
 61 
 62 int n;
 63 
 64 int main()
 65 {
 66     while(cin>>n&&n)
 67     {
 68         int a[10];
 69         for(int i=0;i<10;i++)
 70             a[i]=i;
 71 
 72         for(int i=0;i<n;i++)
 73         {
 74             for(int k=0;k<4;k++)
 75                 cin>>lcy[i].c[k];
 76             cin>>lcy[i].x>>lcy[i].y;
 77 
 78             if(lcy[i].x==0)
 79             {
 80                 for(int j=0;j<4;j++)
 81                 {
 82                   for(int k=0;k<10;k++)
 83                     if(lcy[i].c[j]-48==a[k])
 84                     {
 85                         a[k]=-1;
 86                     }
 87                 }
 88             }
 89 
 90         }
 91 /*
 92         cout<<endl;
 93         for(int i=0;i<10;i++)
 94             cout<<a[i]<<" ";
 95 */
 96        int num=0;
 97        int b[10]={0};
 98        for(int i=0;i<10;i++)
 99        {
100            if(a[i]!=-1)
101             b[num++]=a[i];
102        }
103 /*
104           for(int i=0;i<num;i++)
105             cout<<b[i]<<" ";
106 */
107 
108        nth=0;
109        int c[4];
110        dfs(0,b,c,num);
111 
112  //      cout<<endl<<nth<<endl;
113 /*
114        for(int i=0;i<nth;i++)
115        {
116         for(int j=0;j<4;j++)
117            cout<<cc[i][j];
118         cout<<endl;
119        }
120 */
121 
122        int tot=0;
123        int ans[4]={0};
124 
125        for(int i=0;i<nth;i++)
126        {
127            int ok=1;
128            for(int j=0;j<n;j++)
129            {
130                int tx=0,ty=0;
131                func(lcy[j].c,cc[i],tx,ty);
132 
133                if(tx==lcy[j].x&&ty==lcy[j].y)
134                     continue;
135                else
136                {
137                    ok=0;
138                    break;
139                }
140            }
141 
142            if(ok==1)
143            {
144                tot++;
145                if(tot==1)
146                for(int k=0;k<4;k++)
147                {
148                    ans[k]=cc[i][k];
149                }
150            }
151        }
152 
153        if(tot==1)
154        {
155            for(int i=0;i<4;i++)
156            {
157                cout<<ans[i];
158            }
159            cout<<endl;
160        }
161        else if(tot!=1)
162         cout<<"Not sure"<<endl;
163 
164 
165     }
166 
167     return 0;
168 }

 

posted @ 2013-04-11 11:51  码代码的猿猿  阅读(400)  评论(0编辑  收藏  举报