码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1247 速算24点_BFS

时间:2015-03-04 20:59:32      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #define CL(x, y) memset(x, y, sizeof(x))
  5 using namespace std;
  6 const int INF = 1 << 30;
  7 int num[4], res[4], used[4];
  8 int flag;
  9 int getNum(char ch);
 10 int calculate(int x,int y,int i);  //4步运算
 11 int checkNum();        //判断24
 12 void DFS(int k);    //全排列
 13 int main()
 14 {
 15     char ch;
 16     int i, j, k;
 17     while(cin>>ch)
 18     {
 19         for(i = 0; i < 4; i++)//输入有点讲究,十分重要
 20         {
 21             if(ch==1)
 22             {
 23                 num[i] = 10;
 24                 cin >> ch;//输入为10的时候
 25             }
 26             else
 27             {
 28                 num[i] = getNum(ch);//将当前输入的字符转化为数字
 29             }
 30             if(i<3)
 31                 cin>>ch;
 32         }
 33         flag = 0;
 34         CL(used, 0);
 35         DFS(0);
 36         if(flag) printf("Yes\n");
 37         else printf("No\n");
 38     }
 39     return 0;
 40 }
 41 int getNum(char ch)
 42 {
 43     switch(ch)
 44     {
 45     case A:
 46         return 1;
 47     case J:
 48         return 11;
 49     case Q:
 50         return 12;
 51     case K:
 52         return 13;
 53     default:
 54         return ch-0;
 55     }
 56 }
 57 int calculate(int x,int y,int i)  //4步运算
 58 {
 59     switch(i)
 60     {
 61     case 0 :
 62         return x + y;
 63     case 1 :
 64         return x - y;
 65     case 2 :
 66         return x * y;
 67     case 3 :
 68         if(y != 0 && !(x % y))
 69             return x / y;
 70         else
 71             return INF;
 72     }
 73 }
 74 void DFS(int k)    //全排列24
 75 {
 76     int i;
 77     if(k == 4)
 78     {
 79         if(checkNum()) flag = 1;
 80         return;
 81     }
 82     for(i = 0; i < 4; i ++)
 83     {
 84         if(!used[i])
 85         {
 86             res[k] = num[i];
 87             used[i] = 1;
 88             DFS(k + 1);
 89             if(flag) return;
 90             used[i] = 0;
 91         }
 92     }
 93 }
 94 int checkNum()        //判断24
 95 {
 96     int i,j,k;
 97     int t1,t2,t3;
 98     for(i = 0; i < 4; i ++)       //(a @ b) @ (c @ d)
 99     {
100         t1 = calculate(res[0],res[1],i);
101         if(t1 == INF) continue;
102         for(j = 0; j < 4; j ++)
103         {
104             t2 = calculate(res[2],res[3],j);
105             if(t2 == INF) continue;
106             for(k = 0; k < 4; k ++)
107             {
108                 t3 = calculate(t1,t2,k);
109                 if(t3 == 24) return 1;
110             }
111         }
112     }
113     for(i = 0; i < 4; i ++)   //((a @ b) @ c) @ d
114     {
115         t1 = calculate(res[0],res[1],i);
116         if(t1 == INF) continue;
117         for(j = 0; j < 4; j ++)
118         {
119             t2 = calculate(t1,res[2],j);
120             if(t2 == INF) continue;
121             for(k = 0; k < 4; k ++)
122             {
123                 t3 = calculate(t2,res[3],k);
124                 if(t3 == 24) return 1;
125             }
126         }
127     }
128     for(i = 0; i < 4; i ++)   //(a @ (b @ c)) @ d
129     {
130         t1 = calculate(res[1],res[2],i);
131         if(t1 == INF) continue;
132         for(j = 0; j < 4; j ++)
133         {
134             t2 = calculate(res[0],t1,j);
135             if(t2 == INF) continue;
136             for(k = 0; k < 4; k ++)
137             {
138                 t3 = calculate(t2,res[3],k);
139                 if(t3 == 24) return 1;
140             }
141         }
142     }
143     for(i = 0; i < 4; i ++)   //a @ (b @ (c @ d))
144     {
145         t1 = calculate(res[2],res[3],i);
146         if(t1 == INF) continue;
147         for(j = 0; j < 4; j ++)
148         {
149             t2 = calculate(res[1],t1,j);
150             if(t2 == INF) continue;
151             for(k = 0; k < 4; k ++)
152             {
153                 t3 = calculate(res[0],t2,k);
154                 if(t3 == 24) return 1;
155             }
156         }
157     }
158     for(i = 0; i < 4; i ++)   //a @ ((b @ c) @ d)
159     {
160         t1 = calculate(res[1],res[2],i);
161         if(t1 == INF) continue;
162         for(j = 0; j < 4; j ++)
163         {
164             t2 = calculate(t1,res[3],j);
165             if(t2 == INF) continue;
166             for(k = 0; k < 4; k ++)
167             {
168                 t3 = calculate(res[0],t2,k);
169                 if(t3 == 24) return 1;
170             }
171         }
172     }
173     return 0;    //无解
174 }
View Code

24点问题:

先不考虑括号,4个数是全排列,则有4!;

3个运算符,每个运算符有4次选择机会,一共有4^3;

接下来考虑括号的问题,则有5种方案:(A(B(CD))),(A((BC)D)),((AB)(CD)),((A(BC))D),(((AB)C)D)

综上,一共有:4!* 4^3 * 5 = 1536种表达式;

HDU 1247 速算24点_BFS

标签:

原文地址:http://www.cnblogs.com/tyx0604/p/4314138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!