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

SDiZ
New poster
Posts: 13
Joined: Fri Aug 05, 2005 12:20 pm
Location: Hong Kong
Contact:

Re: 462 Problem

Post by SDiZ » Tue Jan 10, 2012 10:37 am

Just FYI,

When you have two suit with same card number, pick it according to the order S-H-D-C.
I have tried picking D over S and got a WA.

hefesto
New poster
Posts: 1
Joined: Thu Jul 05, 2012 6:37 pm

Re: 462 Problem

Post by hefesto » Thu Jul 05, 2012 8:14 pm

Hi! I've been having the same type of trouble!!! All WA with such a simple problem. Can anybody help me?

Code: Select all

#include <cstdio>
#include <string.h>

using namespace std;


int main()
{
    freopen("in.txt", "r", stdin);

    char hand[14][5];

    char hola[10];
    scanf("%s", hola);

    /*
    for(int i = 0; i < 14; i++)
    {
    	for(int j = 0; j < 5; j++)
    	{
    		hand[i][j]=' ';
    	}
    }*/
    memset(hand,' ', sizeof hand);
    while(scanf("%s %s %s %s %s %s %s %s %s %s %s %s %s\n",
                hand[0], hand[1], hand[2], hand[3], hand[4], hand[5], hand[6],
                hand[7], hand[8], hand[9], hand[10], hand[11], hand[12]) > 12)

    {
        int contSpades = 0;
        int contHearts = 0;
        int contDiamonds = 0;
        int contClubs = 0;
        int points = 0;
        //these bools state wherever the suit is stoped or not. ss == spadesStoped, hs == heartsStoped, ...
        bool ss = false;
        bool hs = false;
        bool ds = false;
        bool cs = false;
        for(int i = 0; i < 13; i++)
        {
            if(hand[i][0] == 'A')
            {
                points += 4;
                if(hand[i][1] == 'S')
                {
                    contSpades++;
                    ss = true;
                }
                else if(hand[i][1] == 'H')
                {
                    contHearts++;
                    hs = true;
                }
                else if(hand[i][1] == 'D')
                {
                    contDiamonds++;
                    ds = true;
                }
                else if(hand[i][1] == 'C')
                {
                    contClubs++;
                    cs = true;
                }
            }
            else if(hand[i][0] == 'K')
            {
                points += 3;
                if(hand[i][1] == 'S')
                    contSpades++;
                else if(hand[i][1] == 'H')
                    contHearts++;
                else if(hand[i][1] == 'D')
                    contDiamonds++;
                else if(hand[i][1] == 'C')
                    contClubs++;

            }
            else if(hand[i][0] == 'Q')
            {
                points += 2;
                if(hand[i][1] == 'S')
                    contSpades++;
                else if(hand[i][1] == 'H')
                    contHearts++;
                else if(hand[i][1] == 'D')
                    contDiamonds++;
                else if(hand[i][1] == 'C')
                    contClubs++;
            }
            else if(hand[i][0] == 'J')
            {
                points += 1;
                if(hand[i][1] == 'S')
                    contSpades++;
                else if(hand[i][1] == 'H')
                    contHearts++;
                else if(hand[i][1] == 'D')
                    contDiamonds++;
                else if(hand[i][1] == 'C')
                    contClubs++;

            }
            else
            {
                if(hand[i][1] == 'S')
                    contSpades++;
                else if(hand[i][1] == 'H')
                    contHearts++;
                else if(hand[i][1] == 'D')
                    contDiamonds++;
                else if(hand[i][1] == 'C')
                    contClubs++;

            }
        }
        for(int i = 0; i < 13; i++)
        {
            if(hand[i][0] == 'K')
            {
                if(hand[i][1] == 'S')
                {
                    if(contSpades == 1)
                        points--;
                    if(contSpades > 1)
                        ss = true;
                }
                else if(hand[i][1] == 'H')
                {
                    if(contHearts == 1)
                        points--;
                    else
                        hs = true;
                }
                else if(hand[i][1] == 'D')
                {
                    if(contDiamonds == 1)
                        points--;
                    else
                        ds = true;
                }
                else if(hand[i][1] == 'C')
                {
                    if(contClubs == 1)
                        points--;
                    else
                        cs = true;
                }

            }
            else if(hand[i][0] == 'Q')
            {
                if(hand[i][1] == 'S')
                {
                    if(contSpades < 3)
                        points--;
                    if(contSpades > 2)
                        ss = true;
                }
                else if(hand[i][1] == 'H')
                {
                    if(contHearts < 3)
                        points--;
                    if(contHearts > 2)
                        hs =  true;
                }
                else if(hand[i][1] == 'D')
                {
                    if(contDiamonds < 3)
                        points--;
                    if(contDiamonds > 2)
                        ds = true;
                }
                else if(hand[i][1] == 'C')
                {
                    if(contClubs < 3)
                        points--;
                    if(contClubs > 2)
                        cs = true;
                }
            }
            else if(hand[i][0] == 'J')
            {
                if(hand[i][1] == 'S')
                    if(contSpades < 4)
                        points--;
                    else if(hand[i][1] == 'H')
                        if(contHearts < 4)
                            points--;
                        else if(hand[i][1] == 'D')
                            if(contDiamonds < 4)
                                points--;
                            else if(hand[i][1] == 'C')
                                if(contClubs < 4)
                                    points--;

            }
        }
        if(points >= 16 && ss && hs && ds && cs)
        {
            printf("BID NO-TRUMP\n");
            continue;
        }
        if(contSpades == 2)
        {
            points++;
        }
        else if(contSpades == 1)
        {
            points += 2;
        }
        else if(contSpades == 0)
        {
            points += 2;
        }
        if(contHearts == 2)
        {
            points++;
        }
        else if(contHearts == 1)
        {
            points += 2;
        }
        else if(contHearts == 0)
        {
            points += 2;
        }
        if(contDiamonds == 2)
        {
            points++;
        }
        else if(contDiamonds == 1)
        {
            points += 2;
        }
        else if(contDiamonds == 0)
        {
            points += 2;
        }
        if(contClubs == 2)
        {
            points++;
        }
        else if(contClubs == 1)
        {
            points += 2;
        }
        else if(contClubs == 0)
        {
            points += 2;
        }
        int max = contSpades;
        int maxS = 1;
        if(contHearts > max)
        {
            max = contHearts;
            maxS = 2;
        }
        if(contDiamonds > max)
        {
            max = contDiamonds;
            maxS = 3;
        }
        if(contClubs > max)
        {
            maxS = 4;
        }
        if(points >= 14)
        {
            if(maxS == 1)
                printf("BID S\n");
            else if(maxS == 2)
                printf("BID H\n");
            else if(maxS == 3)
                printf("BID D\n");
            else if(maxS == 4)
                printf("BID C\n");
        }
        else
            printf("PASS\n");
    }
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 462 Problem

Post by brianfry713 » Tue Jul 17, 2012 1:05 am

Don't read from a file.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 462 Problem

Post by brianfry713 » Fri Nov 09, 2012 8:22 pm

Input:

Code: Select all

2C JH KD 6C 3S 3D AD 5H 9D 4S 5S 2D AC
3H JC QS QD KH 9S TS 5S 5D 5C 7H 4S QH
6C JS AD 3H JH 7S QC 7C JD KC 3S KD 5D
3D 6S 7S 7H 2S 6C 4S 3S KC 2C 4D 5D 8S
8D QC 4C QD KS 3C 6H 6D KD 2H AS 4H 2D
4C 5C 7D JH QD 9S 3H 4H AS 4S 3C 5H 2C
JH 2C QD 9H 6S JC 9S 7C 5C AD 8S 3D 9C
5C 4S KS JS TD TH 7C JC AS 5D QD 8C 3S
QC TS QH AS TH 8H 6H 3D KD JD 4C 2C 7C
2H 7S JH AH 5C 7D JD AS 2S 5D 6C 7C 3C
7D 7C 3S 4D 2C 9C TH 5C 9S 7S 3D 2S 5H
QC JH 4C 5H 4S 2S 4D 6D 9S AC TC 7S 6C
QD AS KS 2H 3C AC JD QS 4C 5C 4H QC 6H
JD QC QS TS 8H AC KH 2S 4D 3D 9S 9D TD
4D 5H 4S 3C 6S 2H 5D 8C AS 7D 8S 5S JH
5S JC 7S 4D 8S 4C QH TD 5C TH QS 9C KH
8C 5S KS 4D 8D 6D JC 9S 8H 2D TS 3H 6H
5D 8C 5C QS TH JC 4D 7C 8S 7H 4C 5H 3C
5C AD 7C 3S 3H 2S 8D 9H JC 2C KC 5H QH
2H AC KD 8S 5D JC 4H 3D 8C 6C 7C 2D 5S
6D 9C 8D 9H AS 3C 5C 4D 5S 6H 3D 8H QD
KH QH 9D 3C 2D 4S 9S JH 5D TH AS QC 6C
5S 6H KH QH KS AS 2C 5D 5H 8H 9C QD JH
2S 8D AD KD 6S JS 9C 9H QD JC 4D TD 3S
QC 9C 3D 9H AD 6D 2H KH 2D 8C 5D 6C AS
KH 5S TD 2S JD QC KD 3C 8S 9S TS 5C 7D
AS AH 9D KD TC TS JS 3H 9C 7C KC KH 4C
2D KH 9H KD 6S TD AD 3C 8C 9S AH 6H TS
TD 4S 7D 7H QD 4C 8D 8S 3S 4H AH AC 6H
AH 9H 4S QC 2S AS KS 5D 7S KH QD 8S 6H
9H 3C JC AC 8S TS 3H 3S 4H KS 6D 5H 4C
9S 2D 4H JD 2C 5S 5H 4D 2H TH JC 5D 8C
QH JD 3S AS KC 4H 4C 9S 2S 9H 2C 7C 9C
7H 8D 5D 9D TD TS QC 5C AS 3S 7D JS 4S
TH 9H 5S JS 7H 9C KC TS 3S 6S AD JC 5C
KC 3H KH 8D QC AD AC 6D TC 4H AH TH JD
6S 8S 6D 3D 3H 4D 7D 9C 7S 8C 7H QH 5D
TS QC 8H 8S 7H TD 6S 9D QS 4D AH 5D 7D
KD TD 6C JS 5C 7S 2H QC AC 8H 6H 7H TC
3S 7C 4H QS KC 9D 6D 2H 4C 5C 8H 5S 5D
3D 9C 3H 8C 5C 4S JH TS TC 6H 7D QC JS
9C JD 5S 2H 8C 8D 6D TD 5C TH 5D 2S TC
JC 4D KC 2S 9C 3S JH 6D 4H JS TH KH 8D
6S 9S KS JD QC 4H 3D TD JC 9H 5C AH 5S
AH 9H 3D 5D 9C AC TC 2D 7S 7C 4C 5C JH
9C AH TD 7S JH 3H TH KC 5D 5S TC 3S 6C
4D AC AD 8D KC JS 8S 7D 5D QH TH 8H 2H
9H 2H 2C 6D 7H KD 7C 4H KS JH 9C TD 7S
QC KD AH 3S QD 2C 9D QS JC 4H 4C 2D KS
8C 4D 7D TD 6C JS 5D 7C 3C AC 3H KC TC
6D 8D 9H 6C 7S JH 4D 7C 7D AD 3C QH 6S
5C AD 6C JD 3H JH 6H 8S 9S KS QD 3D AS
6S 2D 9S QS 8S 4D 4S 2C 6D 9D 7C TC KH
AD 5C 4H QC 9D KD 6C 2C 9H 2H KC 7S 5S
AH 6H 9S 3D 2S 7H JS 6S 9H KH 8S 2D 7D
3C 9C 3D 9H 8H 4C 2C TD AD JC 7S JD 5H
7H JS QC TH QS 5C 5D 3H 6C QH TC JC 4S
8C 5S 7H 2C 6D 3H 8S TC 3S 4C QS QC KD
4H 8D 5D TD 6H 3D 7D 4D TS 5C 4C KC 2H
QS 6C 6H 6S 7D QH 7H 2H TC 9H 4S 7S 8C
4H JC 4S 8C 8S 8H AD QH 2H 9C 4C KC 7D
8S 4D 9H 5H 2H 3D KD AD 3C TS TD 8H QS
KD 6D 7D 7C AH 3D QC 6H 2S AD 4H 8C TH
9S 5C 2H 5S JS 6D 5H TC 9C KH 8S 4C QS
2S AC 5H 7D 6C 4C JD TH 3H QC 5D 4H 4D
JH AS JD 3C 3S AD 7C QS QD 7S 3H JS 2C
6S JH QC 8D 2H 2C 3C 7D KD 8C JD TH 3S
JH JS 4S QD 5D AS 6S 3H 9C AC 9D KC TH
9S 8S TC JC 9C JD 3D 5H QC 4H TS 8H 4D
TC 3C 8S 6H 2D TS 5D 5S TH 9D 6C 7C KH
4S 5C 9S QD KD 6S 6D JC QH 8C AD 3S 2S
5C JC 5H 4C 9D 7H 9C 6D 3D 2C TH 7D 8D
TH JS JH 5D JD 2S 7D 5H AC AH QC KD 4S
QC 5S AC 2H 4D 5H 8D JS KS 2D 4H 3H 4C
QD TH QC 7S 8S TC TD 4D 6D 8H AD 3D KS
JS 3C 4C 8S 8H 5H TC 9D 3D 8D 7C 5D TD
TC 9S 6S 7C 2H QS 5H QH AD JH 6C 5C TS
5S 6H TC AS 9D KD 9C 3S KH QS 8C 6D KC
6S 2H 8C AS 6H 3H 3S 5S 6D KH 5C 4D 6C
8D KD 9C 2D TS TD 9S TH 6S QH JH JS 6C
5C 3C 4C 9C 3S JD 8S QC 9S 5S TS 4H 7D
AC JC 7C JH KC 3S 8D 7S KD 5C TH JD 9S
2D 6H TC 6C KS KH 9C AC 5D 4S 2H JD QS
4C 9S 4S KS 7S 7D 6C QS 6D 6S 4D 3D 4H
5S TD 4C 9H 7S 9D TS 5D JD 4S QH 9S 6C
2H QC 4S QH AD JS 3H 9D 5H 2S 2C 5D 6C
AS 8D AH 6D 3S 9S 2C KD JH 4H 9D 5S TC
8H 9D 7H 7S 5C 9H 6D 6S QH 8D 6H JS TS
8D 7H QS AD TC 6H AH 7S JC 8C 2S 4C 5D
8H 9C 4H 3S AC 8C KH 8D 2C 7C 6H QH 2S
JD JS 2C 8S 4S 3C AD TS QD AH 7H 5S JH
7H 7C AC 2H 3H TC 5D 6C 9C KC 4H 4S JD
9H KD QD 7D 9C AC AD QC 2C 2S 7S 6D 7H
4C 4D QD JS 2D AC QH 5S 6C 8C 5D 8S KH
6C 5C 7C 4D 4S AC KC 3S 2H TC 5H JD TD
8C KS TC 4S 2C TH 3S AD 2S 7C TS 5D AC
8D JC 6H 2S 8H AC 4C JH AS 7D 3D 7S JD
QS 2C 4D TH 6D KD 9S AC JD 4C 5S 9H AD
4H 2H KS 8D JD AC 2C QS 6S JH JC 9H KC
7H 8S 6S 7D 3H 2C 6D QC 4H AD 3S 2S 4C
AC output:

Code: Select all

PASS
PASS
BID D
PASS
BID D
PASS
PASS
PASS
PASS
PASS
PASS
PASS
BID C
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
BID H
PASS
BID D
PASS
BID NO-TRUMP
BID H
PASS
BID S
PASS
PASS
PASS
PASS
PASS
BID H
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
BID D
PASS
BID NO-TRUMP
PASS
PASS
BID S
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
BID D
PASS
PASS
BID S
PASS
BID S
PASS
PASS
PASS
PASS
BID H
PASS
PASS
PASS
PASS
BID S
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
BID D
PASS
PASS
BID S
PASS
BID D
BID H
PASS
Check input and AC output for thousands of problems on uDebug!

mszs
New poster
Posts: 4
Joined: Fri Jun 21, 2013 4:08 pm

Re: 462 Problem

Post by mszs » Fri Jun 21, 2013 7:07 pm

I get WA even though my solution passes all the test cases posted here.

Puzzled...

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 462 Problem

Post by brianfry713 » Sat Jun 22, 2013 6:42 am

Post your code
Check input and AC output for thousands of problems on uDebug!

mszs
New poster
Posts: 4
Joined: Fri Jun 21, 2013 4:08 pm

Re: 462 Problem

Post by mszs » Tue Jun 25, 2013 1:36 pm

My code:

Code: Select all

#include <iostream>
#include <cstdlib>
using namespace std;

typedef enum {Spades = 0, Hearts, Diamonds, Clubs} suite;
typedef enum {Ace = 0, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King} rank;

const int numberSuites = 4;
const int numberRanks = 13;
const int numberCardsOnHand = 13;

int main() {
  bool hand[numberRanks][numberSuites], done, stoppedSuites[numberSuites], allStopped;
  int numberCardsOfSuite[numberSuites], i, j, indexRank, indexSuite, strength, strengthNoTrump, maxCardsOfSuite, suiteWithMostCards;
  char cardRank, cardSuite;

  while (!done) {
    for (i = 0; i < numberRanks; i++)
      for (j = 0; j < numberSuites; j++)
	hand[i][j] = false;
    for (j = 0; j < numberSuites; j++) {
      numberCardsOfSuite[j] = 0;
      stoppedSuites[j] = false;
    }
    strength = strengthNoTrump = 0;
    allStopped = true;
    maxCardsOfSuite = 0;
    for (i = 0; i < numberCardsOnHand; i++) {
      if (cin >> cardRank) {
	switch (cardRank) {
	case 'A':
	  indexRank = Ace;
	  break;
	case '2':
	  indexRank = Two;
	  break;
	case '3':
	  indexRank = Three;
	  break;
	case '4':
	  indexRank = Four;
	  break;
	case '5':
	  indexRank = Five;
	  break;
	case '6':
	  indexRank = Six;
	  break;
	case '7':
	  indexRank = Seven;
	  break;
	case '8':
	  indexRank = Eight;
	  break;
	case '9':
	  indexRank = Nine;
	  break;
	case 'T':
	  indexRank = Ten;
	  break;
	case 'J':
	  indexRank = Jack;
	  break;
	case 'Q':
	  indexRank = Queen;
	  break;
	case 'K':
	  indexRank = King;
	  break;
	}
	cin >> cardSuite;
	switch (cardSuite) {
	case 'S':
	  indexSuite = Spades;
	  break;
	case 'H':
	  indexSuite = Hearts;
	  break;
	case 'D':
	  indexSuite = Diamonds;
	  break;
	case 'C':
	  indexSuite = Clubs;
	  break;
	}
	hand[indexRank][indexSuite] = true;
      } else {
	done = true;
	break;
      }
    }
    if (done) 
      break;
    for (j = 0; j < numberSuites; j++) {
      for (i = 0; i < numberRanks; i++)
	numberCardsOfSuite[j] += hand[i][j];
      strengthNoTrump += hand[Ace][j] * 4;
      strengthNoTrump += hand[King][j] * 3;
      strengthNoTrump += hand[Queen][j] * 2;
      strengthNoTrump += hand[Jack][j] * 1;
      strengthNoTrump -= hand[King][j] * (numberCardsOfSuite[j] == 1);
      strengthNoTrump -= hand[Queen][j] * (numberCardsOfSuite[j] <= 2);
      strengthNoTrump -= hand[Jack][j] * (numberCardsOfSuite[j] <= 3);
    }
    strength = strengthNoTrump;
    maxCardsOfSuite = 0;
    for (j = 0; j < numberSuites; j++) {
      strength += (numberCardsOfSuite[j] == 2);
      strength += 2*(numberCardsOfSuite[j] <= 1);
      stoppedSuites[j] = hand[Ace][j] || (hand[King][j] && numberCardsOfSuite[j] >= 2) || (hand[Queen][j] && numberCardsOfSuite[j] >= 3);
      allStopped = allStopped && stoppedSuites[j];
      if (maxCardsOfSuite < numberCardsOfSuite[j]) {
	maxCardsOfSuite = numberCardsOfSuite[j];
	suiteWithMostCards = j;
      }
    }
    if (strength < 14)
      cout << "PASS\n";
    else if (strengthNoTrump >= 16 && allStopped)
      cout << "BID NO-TRUMP\n";
    else switch (suiteWithMostCards) {
      case Spades:
	cout << "BID S\n";
	break;
      case Hearts:
	cout << "BID H\n";
	break;
      case Diamonds:
	cout << "BID D\n";
	break;
      case Clubs:
	cout << "BID C\n";
	break;
      }
  }
  return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 462 Problem

Post by brianfry713 » Tue Jun 25, 2013 11:57 pm

Initialize done to false.
Check input and AC output for thousands of problems on uDebug!

mszs
New poster
Posts: 4
Joined: Fri Jun 21, 2013 4:08 pm

Re: 462 Problem

Post by mszs » Wed Jun 26, 2013 9:37 pm

of course!

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

Re: 462 Problem

Post by dibery » Tue Jul 09, 2013 9:51 pm

That's not fair.

In bridge, we only subtract HCP (high card points) 1 time when multiple conditions fit in a suit.

However, we have to subtract 1 HCP every time a condition fits! lol

eg. a suit with only QJ needs to be subtracted 2 HCP, not 1.
Life shouldn't be null.

LanceHAOH
New poster
Posts: 9
Joined: Tue Aug 27, 2013 9:04 am

462 - Bridge Hand Evaluator

Post by LanceHAOH » Thu Apr 10, 2014 9:13 am

Hi all. I have carefully crafted and refined my algorithm many times. But I can''t seem to see where the error lies. I just keep getting WA. See my code below:

Code: Select all


import java.util.*;

class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		LinkedList<Card> arr = null;

		// read in the input
		while(in.hasNext()) {
			arr = new LinkedList<Card>();
			for(int i = 0; i < 13; i++) {
				String s = in.next();
				arr.add(new Card(s.substring(0,1),s.substring(1,2)));
			}
			Solution sl = new Solution(arr);
			System.out.println(sl.bid());
		}
	}
}

class Solution {
	// Define the object attributes
	private LinkedList<Card> _cards = new LinkedList<Card>();
	private HashMap<String,Integer> _suitCount = new HashMap<String,Integer>();
	private HashMap<String,HashMap<String,Integer>> _special = new HashMap<String,HashMap<String,Integer>>();
	private int _points;

	// Constructor
	Solution(LinkedList<Card> arr) {
		// Intialize all the attributes
		_cards = arr;
		_suitCount = new HashMap<>();
		_special   = new HashMap<>();

		// Award the initial points
		for(Card c : _cards) {

			// Track the number of cards for each suit
			String rank = c.getRank();
			String suit = c.getSuit();

			_suitCount.put(suit,(_suitCount.containsKey(suit)) ? _suitCount.get(suit)+1 : 1);

			// Record the special cards for each suit
			if(!_special.containsKey(suit)) {
				_special.put(suit,new HashMap<String,Integer>());
			}

			// Award initial points
			switch(rank) {
				case "A":
					_special.get(suit).put("A",1);
					_points += 4;
					break;
				case "K":
					_special.get(suit).put("K",1);
					_points += 3;
					break;
				case "Q":
					_special.get(suit).put("Q",1);
					_points += 2;
					break;
				case "J":
					_special.get(suit).put("J",1);
					_points += 1;
			}
		}
	}

	void deductPoints() {
		for(String s : _special.keySet()) {
			// foreach suit, check for conditions to
			// deduct points
			HashMap<String,HashMap<String,Integer>> suitData = new HashMap<>();
			if(_special.get(s).containsKey("K") &&
					(_suitCount.get(s) == 1)) {
				_points -= 1;
					}
			if(_special.get(s).containsKey("Q") &&
					(_suitCount.get(s) <= 2)) {
				_points -= 1;
					}
			if(_special.get(s).containsKey("J") &&
					(_suitCount.get(s) <= 3)) {
				_points -= 1;
					}
		}
	}

	int getPoints() {
		return _points;
	}

	void addPoints() {

		// Add 2 points for each suit not owned
		_points += 2 * (4 - _special.size());

		for(String s : _suitCount.keySet()) {
			// Check for conditions to add points

			if(_suitCount.get(s) == 2) {
				_points += 1;
			} else if(_suitCount.get(s) == 1) {
				_points += 2;
			}
		}
	}

	boolean isStoppedSuit(String suit) {
		return 
			_special.get(suit).containsKey("A") ||
			(_special.get(suit).containsKey("K") && _suitCount.get(suit) >= 2) ||
			(_special.get(suit).containsKey("Q") && _suitCount.get(suit) >= 3);
	}

	String bid() {
		deductPoints();
		int trumpPnt = _points;
		addPoints();

		if(_points < 14) {
			return "PASS";
		} else if(trumpPnt >= 16 && canTrump() && trumpPnt >= _points) {
			return "BID NO-TRUMP";
		} else {
			return "BID "+maxSuit();
		}
	}

	boolean canTrump() {
		for(String suit : _special.keySet()) {
			if(!isStoppedSuit(suit)) {
				return false;
			}
		}
		return true;
	}

	String maxSuit() {

		int max =  0;
		String maxSuit = "";

		for(String suit : _special.keySet()) {
			if(max == _suitCount.get(suit)) {
				maxSuit = suit;
				continue;
			}
			if(max == Math.max(max,_suitCount.get(suit))) {
				if(max == _suitCount.get(suit) && precedence(maxSuit) < precedence(suit)) {
					maxSuit = suit;
				}
			} else {
				maxSuit = suit;
			}
			max = Math.max(max,_suitCount.get(suit));
		}
		return maxSuit;
	}

	int precedence(String suit) {
		switch(suit) {
			case "S":
				return 4;
			case "H":
				return 3;
			case "D":
				return 2;
			case "C":
				return 1;
		}
		return -1;
	}

}

class Card {
	private String _rank;
	private String _suit;

	Card(String r, String s) {
		_rank = r;
		_suit = s;
	}

	String getSuit() {
		return _suit;
	}

	String getRank() {
		return _rank;
	}

	public String toString() {
		return "Card:" + " Rank => "+ _rank + " Suit => " + _suit;
	}
}


I have spent hours at this problem. Could someone please help? Thanks.

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 462 - Bridge Hand Evaluator

Post by lbv » Thu Apr 10, 2014 5:25 pm

LanceHAOH wrote:Hi all. I have carefully crafted and refined my algorithm many times. But I can''t seem to see where the error lies. I just keep getting WA.
You may try these cases:

Input

Code: Select all

AH QS 3S 7C 3C KC 8H AD AS 4D 5S 6S 3H
3S 2D JS AH 7C JH KH 4C TH TS AS TD 4D
JD KC 7C QD 3D 8C 5D 2S AS QC JH 2C KD
Output

Code: Select all

BID NO-TRUMP
BID S
BID D
P.S. When posting in these forums, please try looking for an existing thread for the relevant problem first before creating a new one. There is already another thread for this problem, and you can find many other test cases there in case you need them.

LanceHAOH
New poster
Posts: 9
Joined: Tue Aug 27, 2013 9:04 am

Re: 462 - Bridge Hand Evaluator

Post by LanceHAOH » Thu Apr 10, 2014 7:21 pm

Hi. Thanks for your help. I managed to get AC at last! Yeah, I know that the there is another thread regarding the same problem already. But I just could not spot any logic errors. So I though that maybe someone could help me with debugging. I collected all the test cases and I checked the input that did not match. Managed to spot the errors in my code. Guess I need more practice!

seeva92
New poster
Posts: 3
Joined: Fri Oct 09, 2015 7:57 am

Re: 462 - Bridge Hand Evaluator

Post by seeva92 » Fri Oct 09, 2015 8:01 am

Please review my code. I got passed several input test cases from Udebug. But still I am getting wrong answer. Please help me on this problem.

Code: Select all

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <sstream>
#include <set>
#include <climits>
#include <cstdio>
#include <string>
#include <unordered_map>
#include <sstream>
using namespace std;
int main() {
	std::ios_base::sync_with_stdio(false);
	// freopen("1.txt", "r", stdin);
	// freopen("2.txt", "w", stdout);
	string stream;
	char str[10];
	char suit[4] = {'S', 'H', 'D', 'C'};
	stringstream ss;
	while (std::getline(cin, stream)) {
		// cout << stream;
		ss << stream;
		unordered_map<char, vector<char>> suitmp;
		unordered_map<char, vector<char>> mp;
		unordered_map<char, int> count;
		unordered_map<char, bool> stopped;
		int points = 0;


		for (int i = 0; i < 13; i++) {
			ss >> str;
			// cout << str << " ";
			suitmp[str[1]].push_back(str[0]);
			if (str[0] == 'Q' || str[0] == 'K' || str[0] == 'J' || str[0] == 'A') {
				count[str[0]]++;
				mp[str[0]].push_back(str[1]);
				if (str[0] == 'A')
					stopped[str[1]] = true;
			}
		}
		points += count['Q'] * 2; points += count['K'] * 3; points += count['J'];
		points += count['A'] * 4;

		char mostCardSuit = ' ';
		int suitSize = INT_MIN;
		int extrapoints = 0;
		for (int i = 0; i < 4; ++i)
		{
			if (suitmp[suit[i]].size() == 2)
				extrapoints += 1;
			else if (suitmp[suit[i]].size() == 1 || suitmp[suit[i]].size() == 0)
				extrapoints += 2;

			// cout << suitSize << " " << (suitmp[suit[i]]).size() << '\n';
			if (suitSize < (int)(suitmp[suit[i]].size())) {
				suitSize = (int)suitmp[suit[i]].size();
				mostCardSuit = suit[i];
				// cout << "Wow" << mostCardSuit << '\n';
			}

		}
		for (std::unordered_map<char, vector<char>>::iterator i = mp.begin(); i != mp.end(); ++i)
		{
			if (i->first == 'Q' || i->first == 'K' || i->first == 'J') {
				int size = i->second.size();
				int reduce = 1;
				vector<char> v = i->second;
				for (int j = 0; j < size; ++j)
				{
					if (i->first == 'K') {
						if (suitmp[v[j]].size() == 1)
							points -= reduce;
						else if (suitmp[v[j]].size() > 1)
							stopped[v[j]] = true;
					}
					else if (i->first == 'Q') {
						if (suitmp[v[j]].size() <= 2)
							points -= reduce;
						else if (suitmp[v[j]].size() > 2)
							stopped[v[j]] = true;
					}
					else if (i->first == 'J' && suitmp[v[j]].size() <= 3)
						points -= reduce;
				}
			}

		}
		
		if (points >= 16 && stopped['S'] && stopped['D'] && stopped['H'] && stopped['C']) {
			cout << "BID NO-TRUMP\n";
		} else if (points + extrapoints >= 14)
			cout << "BID " << mostCardSuit << '\n';
		else
			cout << "PASS\n";

	}
}

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

Re: 462 - Bridge Hand Evaluator

Post by dibery » Tue Oct 13, 2015 1:09 pm

Your code fails on these 2 cases. (Your code returns clubs on both cases.)

QC JC AH KH QH JH 3D 4D 5D 6D 7S 8S QS
---bid heart

QC JC AH KH QH JH 3D 4D 5D 6D 7S 8S JS
---pass
Life shouldn't be null.

Post Reply

Return to “Volume 4 (400-499)”