zoukankan      html  css  js  c++  java
  • hdu1427之速算24点

    速算24点

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2430    Accepted Submission(s): 582

    Problem Description
    速算24点相信绝大多数人都玩过。就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13)。要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次)。游戏很简单,但遇到无解的情况往往让人很郁闷。你的任务就是针对每一组随机产生的四张牌,判断是否有解。我们另外规定,整个计算过程中都不能出现小数。
     
    Input
    每组输入数据占一行,给定四张牌。
     
    Output
    每一组输入数据对应一行输出。如果有解则输出"Yes",无解则输出"No"。
     
    Sample Input
    A 2 3 6 3 3 8 8
     
    Sample Output
    Yes No
     
    /*分析:对于a,b,c,d四个数进行+,-,*,/。
    有这几种情况:1.(a@b)@(c@d),(c@d)@(a@b),(b@a)@(c@d),... 综合为(x1@y1)@(x1@y1)这种情况,不过得到的值可以为-24 
    2.((a@b)@c)@d,a@((b@c)@d),(d@(a@b)@c),...综合为((x1@y1)@x2)@y2这种情况,不过得到的值可以为-24 
    */ 
    所以只要求a,b,c,d的全排列在用(a@b)@(c@b),((a@b)@c))@d去计算即可
    1.用next_permutation求全排列:注意next_permutation求的是按字典序的全排列,所以要先排序
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<iomanip>
    #define INF 99999999
    using namespace std;
    
    const int MAX=5;
    char s[3];
    int number[MAX];
    
    void check(char ch,int &num){
    	if(ch == 'A')num=1;
    	else if(ch == 'J')num=11;
    	else if(ch == 'Q')num=12;
    	else if(ch == 'K')num=13;
    	else for(int i=2;i<10;++i){
    		if(ch == i+'0'){num=i;break;}
    	}
    	if(ch == '1')num=10;
    }
    
    int f(int a,int op,int c){
    	if(op == 0)return a+c;
    	if(op == 1)return a-c;
    	if(op == 2)return a*c;
    	if(c && a%c == 0)return a/c;
    	return INF;
    }
    
    bool calculate(int i,int j,int k){
    	int temp1,temp2;
    	temp1=f(number[0],i,number[1]);
    	if(temp1 != INF)temp2=f(number[2],k,number[3]);
    	if(temp2 == INF)temp1=INF;
    	if(temp1 != INF)temp1=f(temp1,j,temp2);
    	if(temp1 == 24 || temp1 == -24)return true;
    	temp1=f(number[0],i,number[1]);
    	if(temp1 != INF)temp1=f(temp1,j,number[2]);
    	if(temp1 != INF)temp1=f(temp1,k,number[3]);
    	if(temp1 == 24 || temp1 == -24)return true;
    	return false;
    }
    
    int main(){
    	while(~scanf("%s",s)){
    		check(s[0],number[0]);
    		for(int i=1;i<=3;++i){
    			scanf("%s",s);
    			check(s[0],number[i]);
    		}
    		sort(number,number+4);
    		bool flag=false;
    		do{
    			for(int i=0;i<4 && !flag;++i)for(int j=0;j<4 && !flag;++j)for(int k=0;k<4 && !flag;++k){
    				flag=calculate(i,j,k);
    				//if(flag)cout<<number[0]<<' '<<number[1]<<' '<<number[2]<<' '<<number[3]<<endl;
    				//if(flag)cout<<i<<' '<<j<<' '<<k<<endl;
    			}
    		}while(!flag && next_permutation(number,number+4));
    		if(flag)printf("Yes
    ");
    		else printf("No
    ");
    	}
    	return 0;
    }
    自己写的全排列:
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<iomanip>
    #define INF 99999999
    using namespace std;
    
    const int MAX=5;
    char s[3];
    int number[MAX];
    bool flag;
    
    void check(char ch,int &num){
    	if(ch == 'A')num=1;
    	else if(ch == 'J')num=11;
    	else if(ch == 'Q')num=12;
    	else if(ch == 'K')num=13;
    	else for(int i=2;i<10;++i){
    		if(ch == i+'0'){num=i;break;}
    	}
    	if(ch == '1')num=10;
    }
    
    int f(int a,int op,int c){
    	if(op == 0)return a+c;
    	if(op == 1)return a-c;
    	if(op == 2)return a*c;
    	if(c && a%c == 0)return a/c;
    	return INF;
    }
    
    bool calculate(int i,int j,int k){
    	int temp1,temp2;
    	temp1=f(number[0],i,number[1]);
    	if(temp1 != INF)temp2=f(number[2],k,number[3]);
    	if(temp2 == INF)temp1=INF;
    	if(temp1 != INF)temp1=f(temp1,j,temp2);
    	if(temp1 == 24 || temp1 == -24)return true;
    	temp1=f(number[0],i,number[1]);
    	if(temp1 != INF)temp1=f(temp1,j,number[2]);
    	if(temp1 != INF)temp1=f(temp1,k,number[3]);
    	if(temp1 == 24 || temp1 == -24)return true;
    	return false;
    }
    
    void Perm(int k){
    	if(k == 3){
    		for(int i=0;i<4 && !flag;++i)for(int j=0;j<4 && !flag;++j)for(int k=0;k<4 && !flag;++k){
    			flag=calculate(i,j,k);
    		}
    		return;
    	}
    	Perm(k+1);
    	for(int i=k+1;i<4;++i){
    		if(flag)return;
    		if(number[i] == number[k])continue;
    		swap(number[k],number[i]);
    		Perm(k+1);
    		swap(number[k],number[i]);
    	}
    }
    
    int main(){
    	while(~scanf("%s",s)){
    		check(s[0],number[0]);
    		for(int i=1;i<=3;++i){
    			scanf("%s",s);
    			check(s[0],number[i]);
    		}
    		flag=false;
    		Perm(0);
    		if(flag)printf("Yes
    ");
    		else printf("No
    ");
    	}
    	return 0;
    }

  • 相关阅读:
    ZYB建围墙
    换根DP
    原码,反码,补码的计算
    字符串游戏-博弈论-trie
    【十连赛day8】神炎皇
    生成序列
    【USACO 2006 February Silver】产奶安排Stall Reservations-贪心
    tar 压缩解压命令
    java 注解
    回溯算法解决全排列问题
  • 原文地址:https://www.cnblogs.com/james1207/p/3304119.html
Copyright © 2011-2022 走看看