462 - Bridge Hand Evaluator

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

qshahrukh40
New poster
Posts: 2
Joined: Sun Nov 15, 2015 6:24 pm

Re: 462 - Bridge Hand Evaluator

Post by qshahrukh40 »

I have got right answers for all the test cases in Udebug and for the test cases in the forum! still getting a WA ! anybody could help please?

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
    int len,point,i,S_king,S_queen,S_jack,S_ace,H_king,H_queen,H_jack,H_ace,C_king,C_queen,C_jack,C_ace,D_king,D_queen,D_jack,D_ace,heart,club,diamond,spade,stop,flag,max,f;
    char str[100];
    while(gets(str))
    {
    point=0,i,heart=0,club=0,diamond=0,spade=0,S_king=0,S_queen=0,S_jack=0,S_ace=0,H_king=0,H_queen=0,H_jack=0,H_ace=0,C_king=0,C_queen=0,C_jack=0,C_ace=0,D_king=0,D_queen=0,D_jack=0,D_ace=0,stop=0,flag=0,max=0,f;
    len=strlen(str);

    for(i=0;i<len;i++)
    {
        if(str[i]=='H') heart++;
        if(str[i]=='S') spade++;
        if(str[i]=='C') club++;
        if(str[i]=='D') diamond++;
    }
       if(club>=max) max=club,f=3;
       if(diamond>=max) max=diamond,f=4;
       if(heart>=max) max=heart,f=1;
       if(spade>=max) max=spade,f=2;

    for(i=0;i<len;i=i+3)
    {
        if(str[i]=='K'){
            point=point+3;
            if(str[i+1]=='S') S_king++;
            if(str[i+1]=='H') H_king++;
            if(str[i+1]=='C') C_king++;
            if(str[i+1]=='D') D_king++;
        }
        if(str[i]=='Q'){
            point=point+2;
            if(str[i+1]=='S') S_queen++;
            if(str[i+1]=='H') H_queen++;
            if(str[i+1]=='C') C_queen++;
            if(str[i+1]=='D') D_queen++;
        }
        if(str[i]=='J'){
            point=point+1;
            if(str[i+1]=='S') S_jack++;
            if(str[i+1]=='H') H_jack++;
            if(str[i+1]=='C') C_jack++;
            if(str[i+1]=='D') D_jack++;
        }
        if(str[i]=='A'){
            point=point+4;
            if(str[i+1]=='S') S_ace++;
            if(str[i+1]=='H') H_ace++;
            if(str[i+1]=='C') C_ace++;
            if(str[i+1]=='D') D_ace++;
        }
    }
    //printf("S_queen=%d spade=%d\n",S_queen,spade);
        //printf("1: %d\n",point);
         if(S_king==1 && spade==1) point--;
         if(C_king==1 && club==1) point--;
         if(D_king==1 && diamond==1) point--;
         if(H_king==1 && heart==1) point--;
        //printf("2: %d\n",point);
         if(S_queen==1 && spade<=2) point--;
         if(C_queen==1 && club<=2) point--;
         if(D_queen==1 && diamond<=2) point--;
         if(H_queen==1 && heart<=1) point--;
        //printf("3: %d\n",point);
         if(S_jack==1 && spade<=3) point--;
         if(C_jack==1 && club<=3) point--;
         if(D_jack==1 && diamond<=3) point--;
         if(H_jack==1 && heart<=3) point--;
        //printf("4: %d\n",point);

         if(S_ace==1) stop++;
         else if(S_king==1 && spade>=2) stop++;
         else if(S_queen==1 && spade>=3) stop++;
         //printf("stop 8: %d\n",stop);
         if(H_ace==1) stop++;
         else if(H_king==1 && heart>=2) stop++;
         else if(H_queen==1 && heart>=3) stop++;
         //printf("9: %d\n",stop);
         if(C_ace==1) stop++;
         else if(C_king==1 && club>=2) stop++;
         else if(C_queen==1 && club>=3) stop++;
         //printf("10: %d\n",stop);
         if(D_ace==1) stop++;
         else if(D_king==1 && diamond>=2) stop++;
         else if(D_queen==1 && diamond>=3) stop++;

         if(stop==4 && point>=16) flag=1;

         if(spade==2) point++;
         if(heart==2) point++;
         if(club==2) point++;
         if(diamond==2) point++;
         //printf("5: %d\n",point);

         if(spade==1) point=point+2;
         if(heart==1) point=point+2;
         if(club==1) point=point+2;
         if(diamond==1) point=point+2;
         //printf("6: %d\n",point);
         if(spade==0) point=point+2;
         if(heart==0) point=point+2;
         if(club==0) point=point+2;
         if(diamond==0) point=point+2;
         //printf("%d",point);
         if(flag==1) printf("BID NO-TRUMP\n");
         else if(point<14) printf("PASS\n");
         else if(point>=14){
                printf("BID ");
                if(f==1) printf("H\n");
                else if(f==2) printf("S\n");
                else if(f==3) printf("C\n");
                else if(f==4) printf("D\n");
         }
    }
return 0;
}
alexandrek
New poster
Posts: 1
Joined: Mon Jun 20, 2016 9:29 pm

Re: 462 - Bridge Hand Evaluator

Post by alexandrek »

Hi all I got the right answers on all test cases posted here and the ones in uDebug, however my code got WA many times.

After looking at the code found the issue, and it was on the case that a suit did not had any cards.
So if you have issues after passing all test cases posted here and in uDebug look at this case.
ekangas
New poster
Posts: 2
Joined: Tue Feb 21, 2017 8:01 am

Re: 462 - Bridge Hand Evaluator

Post by ekangas »

I keep getting WA here, however I passed all the tests on uDebug. Here's my code (c++11):

Code: Select all

#include<iostream>
#include<vector>
#include<unordered_map>   
#include<sstream> //stringstreams
using namespace std;

enum class Suit {
    spades='S',hearts='H',diamonds='D',clubs='C'
};

const array<Suit,4> all_suits = {Suit::spades, Suit::hearts, Suit::diamonds, Suit::clubs};

istream& operator>>(istream& is, Suit& s) {
    char c = is.get();
    switch(c) {
        case 'S': s = Suit::spades; break;
        case 'H': s = Suit::hearts; break;
        case 'D': s = Suit::diamonds; break;
        case 'C': s = Suit::clubs; break;
        default: is.setstate(ios_base::failbit);
    } 
    return is;
}

string operator+(const char* s, const Suit& suit) {
    string str(s);
    return str + static_cast<char>(suit);
}

ostream& operator<<(ostream& os, const Suit& suit) {
    return os << static_cast<char>(suit);
}

struct Card {
    char value;
    Suit suit;

    friend ostream& operator<< (ostream& stream, const Card& card) {
        stream << card.value << card.suit;
        return stream;
    }
    friend istream& operator>> (istream& stream, Card& card) {
        stream >> card.value >> card.suit;
        return stream;
    }
};

string evaluateRules(vector<Card> hand) {
    int valueIgnoringR5_thru_7 = 0, value = 0;

    unordered_map<Suit, bool> ace_of_suit; 	//guaranteed init to false
    unordered_map<Suit, bool> king_of_suit;
    unordered_map<Suit, bool> queen_of_suit;
    unordered_map<Suit, bool> jack_of_suit;
    unordered_map<Suit, int> suitCount;		 //guaranteed init to 0
    unordered_map<Suit, bool> stoppedSuits;

    for(Card card: hand) {
        suitCount[card.suit]++;
        //r1
        switch(card.value) {
            case 'A': 
                value += 4; 
                valueIgnoringR5_thru_7 +=4;
                break;
            case 'K': 
                value += 3; 
                valueIgnoringR5_thru_7 +=3;
                break;
            case 'Q': 
                value += 2; 
                valueIgnoringR5_thru_7 +=2;
                break;
            case 'J': 
                value += 1; 
                valueIgnoringR5_thru_7 +=1;
                break; 
        }
        
        //r2-r4 + stopped suit
        if(card.value=='A') {
            ace_of_suit[card.suit] = true;        
        } 
        else if(card.value=='K') {
            king_of_suit[card.suit] = true;        
        } 
        else if(card.value=='Q') {
            queen_of_suit[card.suit] = true;        
        } else if (card.value=='J') {
            jack_of_suit[card.suit] = true;
        }
    }

    int highestCount=0, stoppedCount=0;
    Suit mostCardedSuit;
    for (Suit s: all_suits) {  	//in order from S H D C 
        //most carded suit
        if(suitCount[s] > highestCount) {
            highestCount = suitCount[s];
            mostCardedSuit=s;
        }
        //stopped_suits
        if(ace_of_suit[s] || 
                (king_of_suit[s] && suitCount[s]>=2 ) ||
                (queen_of_suit[s] && suitCount[s]>=3)) {
            stoppedCount++;
        }
        //r2-r4
        if(king_of_suit[s] && suitCount[s]==1) {
            value -=1;
            valueIgnoringR5_thru_7 -=1;
        } 
        else if (queen_of_suit[s] && suitCount[s] <= 2) {
            value -=1;
            valueIgnoringR5_thru_7 -=1;
        } else if (jack_of_suit[s] && suitCount[s] <= 3){
            value -=1;
            valueIgnoringR5_thru_7 -=1;
        }
        //r5-r7
        if(suitCount[s]==2) {
            value +=1;
        } else if (suitCount[s]==1) {
            value +=2;
        } else if (suitCount[s]==0) {
            value +=2;
        }
    }

    //evaluate hand
    if(value < 14) return "PASS";
    else if(valueIgnoringR5_thru_7 >=16 && stoppedCount==4) {
        return "BID NO-TRUMP";
    } else {
        return "BID " + mostCardedSuit;
    }
}

int main() 
{
    std::ios::sync_with_stdio(false);
    string line;
    while(getline(cin, line)) {
        if(line.size()<=30) return 0;
        stringstream ss(line);
        vector<Card> hand; 
        for(int i=0; i < 13; i++) {
            Card card;
            ss >> card;
            hand.push_back(card);
        } 
        //        for (Card card: hand) {
        //            cout << card << " ";
        cout << evaluateRules(hand) << endl;
    }
    return 0;    
}
Post Reply

Return to “Volume 4 (400-499)”