162 - Beggar My Neighbour

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

Moderator: Board moderators

angga888
Experienced poster
Posts: 143
Joined: Sat Dec 21, 2002 11:41 am
Location: Indonesia

Post by angga888 »

Output for shuniu's input:

Code: Select all

2 51
2 49
2 51
2 48
1 47
2 41
2 44
1 42
1 45
2 34
1 51
2 48
2 38
1 49
2 44
2 51
1 49
1 43
2 50
2 51
2 51
1 48
1 49
2 49
2 40
2 44
1 47
1 47
1 51
1 44
Good luck!
shuniu
New poster
Posts: 34
Joined: Thu Oct 16, 2003 6:15 pm

Post by shuniu »

thanx angga! i get AC now. There was an problem in my input parsing routine.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Test your code with the input output from this topic---

http://online-judge.uva.es/board/viewto ... hlight=162
Ami ekhono shopno dekhi...
HomePage
logan
New poster
Posts: 6
Joined: Wed May 25, 2005 10:04 am

162 Beggar My Neighbour (13.08.2005)

Post by logan »

Hello :-)

Can anybody write me game trace for sample input or check my trace?
I'm getting:
1 43

For other inputs it's even worst :-(

Game trace:
> (player, #number after previous round, table + new card)
> p=2 #c1=26 #c2=26 H2
> p=1 #c1=26 #c2=25 H2 D2
> p=2 #c1=25 #c2=25 H2 D2 S7
> p=1 #c1=25 #c2=24 H2 D2 S7 ST
> p=2 #c1=24 #c2=24 H2 D2 S7 ST SQ
> p=1 #c1=24 #c2=23 H2 D2 S7 ST SQ H8
> p=1 #c1=23 #c2=23 H2 D2 S7 ST SQ H8 HT
> player 2 gets 7 cards from table
> p=2 #c1=22 #c2=30 C3
> p=1 #c1=22 #c2=29 C3 CQ
> p=2 #c1=21 #c2=29 C3 CQ CK
> p=1 #c1=21 #c2=28 C3 CQ CK SA
> p=2 #c1=20 #c2=28 C3 CQ CK SA DA
> p=1 #c1=20 #c2=27 C3 CQ CK SA DA H9
> p=1 #c1=19 #c2=27 C3 CQ CK SA DA H9 D9
> p=1 #c1=18 #c2=27 C3 CQ CK SA DA H9 D9 DQ
> p=2 #c1=17 #c2=27 C3 CQ CK SA DA H9 D9 DQ D3
> p=2 #c1=17 #c2=26 C3 CQ CK SA DA H9 D9 DQ D3 DK
> p=1 #c1=17 #c2=25 C3 CQ CK SA DA H9 D9 DQ D3 DK DJ
> p=2 #c1=16 #c2=25 C3 CQ CK SA DA H9 D9 DQ D3 DK DJ S4
> player 1 gets 12 cards from table
> p=1 #c1=28 #c2=24 C7
> p=2 #c1=27 #c2=24 C7 H5
> p=1 #c1=27 #c2=23 C7 H5 S2
> p=2 #c1=26 #c2=23 C7 H5 S2 S3
> p=1 #c1=26 #c2=22 C7 H5 S2 S3 D8
> p=2 #c1=25 #c2=22 C7 H5 S2 S3 D8 C2
> p=1 #c1=25 #c2=21 C7 H5 S2 S3 D8 C2 C6
> p=2 #c1=24 #c2=21 C7 H5 S2 S3 D8 C2 C6 S8
> p=1 #c1=24 #c2=20 C7 H5 S2 S3 D8 C2 C6 S8 D6
> p=2 #c1=23 #c2=20 C7 H5 S2 S3 D8 C2 C6 S8 D6 CJ
> p=1 #c1=23 #c2=19 C7 H5 S2 S3 D8 C2 C6 S8 D6 CJ C8
> player 2 gets 11 cards from table
> p=2 #c1=22 #c2=30 C9
> p=1 #c1=22 #c2=29 C9 CT
> p=2 #c1=21 #c2=29 C9 CT HK
> p=1 #c1=21 #c2=28 C9 CT HK H6
> p=1 #c1=20 #c2=28 C9 CT HK H6 SJ
> p=2 #c1=19 #c2=28 C9 CT HK H6 SJ S9
> player 1 gets 6 cards from table
> p=1 #c1=25 #c2=27 D4
> p=2 #c1=24 #c2=27 D4 DT
> p=1 #c1=24 #c2=26 D4 DT HJ
> p=2 #c1=23 #c2=26 D4 DT HJ D7
> player 1 gets 4 cards from table
> p=1 #c1=27 #c2=25 D5
> p=2 #c1=26 #c2=25 D5 HQ
> p=1 #c1=26 #c2=24 D5 HQ S6
> p=1 #c1=25 #c2=24 D5 HQ S6 S5
> player 2 gets 4 cards from table
> p=2 #c1=24 #c2=28 H7
> p=1 #c1=24 #c2=27 H7 CA
> p=2 #c1=23 #c2=27 H7 CA C4
> p=2 #c1=23 #c2=26 H7 CA C4 C5
> p=2 #c1=23 #c2=25 H7 CA C4 C5 SK
> p=1 #c1=23 #c2=24 H7 CA C4 C5 SK H3
> p=1 #c1=22 #c2=24 H7 CA C4 C5 SK H3 C3
> p=1 #c1=21 #c2=24 H7 CA C4 C5 SK H3 C3 CQ
> p=2 #c1=20 #c2=24 H7 CA C4 C5 SK H3 C3 CQ H4
> p=2 #c1=20 #c2=23 H7 CA C4 C5 SK H3 C3 CQ H4 HA
> p=1 #c1=20 #c2=22 H7 CA C4 C5 SK H3 C3 CQ H4 HA CK
> p=2 #c1=19 #c2=22 H7 CA C4 C5 SK H3 C3 CQ H4 HA CK H2
> p=2 #c1=19 #c2=21 H7 CA C4 C5 SK H3 C3 CQ H4 HA CK H2 D2
> p=2 #c1=19 #c2=20 H7 CA C4 C5 SK H3 C3 CQ H4 HA CK H2 D2 S7
> player 1 gets 14 cards from table
> p=1 #c1=33 #c2=19 SA
> p=2 #c1=32 #c2=19 SA ST
> p=1 #c1=32 #c2=18 SA ST DA
> p=2 #c1=31 #c2=18 SA ST DA SQ
> p=1 #c1=31 #c2=17 SA ST DA SQ H9
> p=1 #c1=30 #c2=17 SA ST DA SQ H9 D9
> player 2 gets 6 cards from table
> p=2 #c1=29 #c2=23 H8
> p=1 #c1=29 #c2=22 H8 DQ
> p=2 #c1=28 #c2=22 H8 DQ HT
> p=2 #c1=28 #c2=21 H8 DQ HT C7
> player 1 gets 4 cards from table
> p=1 #c1=32 #c2=20 D3
> p=2 #c1=31 #c2=20 D3 H5
> p=1 #c1=31 #c2=19 D3 H5 DK
> p=2 #c1=30 #c2=19 D3 H5 DK S2
> p=2 #c1=30 #c2=18 D3 H5 DK S2 S3
> p=2 #c1=30 #c2=17 D3 H5 DK S2 S3 D8
> player 1 gets 6 cards from table
> p=1 #c1=36 #c2=16 DJ
> p=2 #c1=35 #c2=16 DJ C2
> p=1 #c1=35 #c2=15 DJ C2 S4
> p=2 #c1=34 #c2=15 DJ C2 S4 C6
> p=1 #c1=34 #c2=14 DJ C2 S4 C6 C9
> p=2 #c1=33 #c2=14 DJ C2 S4 C6 C9 S8
> p=1 #c1=33 #c2=13 DJ C2 S4 C6 C9 S8 CT
> p=2 #c1=32 #c2=13 DJ C2 S4 C6 C9 S8 CT D6
> p=1 #c1=32 #c2=12 DJ C2 S4 C6 C9 S8 CT D6 HK
> p=2 #c1=31 #c2=12 DJ C2 S4 C6 C9 S8 CT D6 HK CJ
> p=1 #c1=31 #c2=11 DJ C2 S4 C6 C9 S8 CT D6 HK CJ H6
> player 2 gets 11 cards from table
> p=2 #c1=30 #c2=22 C8
> p=1 #c1=30 #c2=21 C8 SJ
> p=2 #c1=29 #c2=21 C8 SJ D5
> player 1 gets 3 cards from table
> p=1 #c1=32 #c2=20 S9
> p=2 #c1=31 #c2=20 S9 HQ
> p=1 #c1=31 #c2=19 S9 HQ D4
> p=1 #c1=30 #c2=19 S9 HQ D4 DT
> player 2 gets 4 cards from table
> p=2 #c1=29 #c2=23 S6
> p=1 #c1=29 #c2=22 S6 HJ
> p=2 #c1=28 #c2=22 S6 HJ S5
> player 1 gets 3 cards from table
> p=1 #c1=31 #c2=21 D7
> p=2 #c1=30 #c2=21 D7 SA
> p=1 #c1=30 #c2=20 D7 SA H7
> p=1 #c1=29 #c2=20 D7 SA H7 CA
> p=2 #c1=28 #c2=20 D7 SA H7 CA ST
> p=2 #c1=28 #c2=19 D7 SA H7 CA ST DA
> p=1 #c1=28 #c2=18 D7 SA H7 CA ST DA C4
> p=1 #c1=27 #c2=18 D7 SA H7 CA ST DA C4 C5
> p=1 #c1=26 #c2=18 D7 SA H7 CA ST DA C4 C5 SK
> p=2 #c1=25 #c2=18 D7 SA H7 CA ST DA C4 C5 SK SQ
> p=1 #c1=25 #c2=17 D7 SA H7 CA ST DA C4 C5 SK SQ H3
> p=1 #c1=24 #c2=17 D7 SA H7 CA ST DA C4 C5 SK SQ H3 C3
> player 2 gets 12 cards from table
> p=2 #c1=23 #c2=29 H9
> p=1 #c1=23 #c2=28 H9 CQ
> p=2 #c1=22 #c2=28 H9 CQ D9
> p=2 #c1=22 #c2=27 H9 CQ D9 DJ
> p=1 #c1=22 #c2=26 H9 CQ D9 DJ H4
> player 2 gets 5 cards from table
> p=2 #c1=21 #c2=31 C2
> p=1 #c1=21 #c2=30 C2 HA
> p=2 #c1=20 #c2=30 C2 HA S4
> p=2 #c1=20 #c2=29 C2 HA S4 C6
> p=2 #c1=20 #c2=28 C2 HA S4 C6 C9
> p=2 #c1=20 #c2=27 C2 HA S4 C6 C9 S8
> player 1 gets 6 cards from table
> p=1 #c1=26 #c2=26 CK
> p=2 #c1=25 #c2=26 CK CT
> p=1 #c1=25 #c2=25 CK CT H2
> p=2 #c1=24 #c2=25 CK CT H2 D6
> p=1 #c1=24 #c2=24 CK CT H2 D6 D2
> p=2 #c1=23 #c2=24 CK CT H2 D6 D2 HK
> p=1 #c1=23 #c2=23 CK CT H2 D6 D2 HK S7
> p=1 #c1=22 #c2=23 CK CT H2 D6 D2 HK S7 H8
> p=1 #c1=21 #c2=23 CK CT H2 D6 D2 HK S7 H8 DQ
> p=2 #c1=20 #c2=23 CK CT H2 D6 D2 HK S7 H8 DQ CJ
> p=1 #c1=20 #c2=22 CK CT H2 D6 D2 HK S7 H8 DQ CJ HT
> player 2 gets 11 cards from table
> p=2 #c1=19 #c2=33 H6
> p=1 #c1=19 #c2=32 H6 C7
> p=2 #c1=18 #c2=32 H6 C7 S9
> p=1 #c1=18 #c2=31 H6 C7 S9 D3
> p=2 #c1=17 #c2=31 H6 C7 S9 D3 HQ
> p=1 #c1=17 #c2=30 H6 C7 S9 D3 HQ H5
> p=1 #c1=16 #c2=30 H6 C7 S9 D3 HQ H5 DK
> p=2 #c1=15 #c2=30 H6 C7 S9 D3 HQ H5 DK D4
> p=2 #c1=15 #c2=29 H6 C7 S9 D3 HQ H5 DK D4 DT
> p=2 #c1=15 #c2=28 H6 C7 S9 D3 HQ H5 DK D4 DT D7
> player 1 gets 10 cards from table
> p=1 #c1=25 #c2=27 S2
> p=2 #c1=24 #c2=27 S2 SA
> p=1 #c1=24 #c2=26 S2 SA S3
> p=1 #c1=23 #c2=26 S2 SA S3 D8
> p=1 #c1=22 #c2=26 S2 SA S3 D8 C8
> p=1 #c1=21 #c2=26 S2 SA S3 D8 C8 SJ
> p=2 #c1=20 #c2=26 S2 SA S3 D8 C8 SJ H7
> player 1 gets 7 cards from table
> p=1 #c1=27 #c2=25 D5
> p=2 #c1=26 #c2=25 D5 CA
> p=1 #c1=26 #c2=24 D5 CA S6
> p=1 #c1=25 #c2=24 D5 CA S6 HJ
> p=2 #c1=24 #c2=24 D5 CA S6 HJ ST
> player 1 gets 5 cards from table
> p=1 #c1=29 #c2=23 S5
> p=2 #c1=28 #c2=23 S5 DA
> p=1 #c1=28 #c2=22 S5 DA C2
> p=1 #c1=27 #c2=22 S5 DA C2 HA
> p=2 #c1=26 #c2=22 S5 DA C2 HA C4
> p=2 #c1=26 #c2=21 S5 DA C2 HA C4 C5
> p=2 #c1=26 #c2=20 S5 DA C2 HA C4 C5 SK
> p=1 #c1=26 #c2=19 S5 DA C2 HA C4 C5 SK S4
> p=1 #c1=25 #c2=19 S5 DA C2 HA C4 C5 SK S4 C6
> p=1 #c1=24 #c2=19 S5 DA C2 HA C4 C5 SK S4 C6 C9
> player 2 gets 10 cards from table
> p=2 #c1=23 #c2=29 SQ
> p=1 #c1=23 #c2=28 SQ S8
> p=2 #c1=22 #c2=28 SQ S8 H3
> p=1 #c1=22 #c2=27 SQ S8 H3 H6
> p=2 #c1=21 #c2=27 SQ S8 H3 H6 C3
> p=1 #c1=21 #c2=26 SQ S8 H3 H6 C3 C7
> p=2 #c1=20 #c2=26 SQ S8 H3 H6 C3 C7 H9
> p=1 #c1=20 #c2=25 SQ S8 H3 H6 C3 C7 H9 S9
> p=2 #c1=19 #c2=25 SQ S8 H3 H6 C3 C7 H9 S9 CQ
> p=1 #c1=19 #c2=24 SQ S8 H3 H6 C3 C7 H9 S9 CQ D3
> p=1 #c1=18 #c2=24 SQ S8 H3 H6 C3 C7 H9 S9 CQ D3 HQ
> p=2 #c1=17 #c2=24 SQ S8 H3 H6 C3 C7 H9 S9 CQ D3 HQ D9
> p=2 #c1=17 #c2=23 SQ S8 H3 H6 C3 C7 H9 S9 CQ D3 HQ D9 DJ
> p=1 #c1=17 #c2=22 SQ S8 H3 H6 C3 C7 H9 S9 CQ D3 HQ D9 DJ H5
> player 2 gets 14 cards from table
(...)
mars kaseijin
New poster
Posts: 22
Joined: Mon Sep 19, 2005 4:58 am
Contact:

Post by mars kaseijin »

greetings logan,
are you still interested in this problem?
dragon135
New poster
Posts: 2
Joined: Sat May 23, 2009 8:31 am

Re: 162 Beggar My Neighbour (13.08.2005)

Post by dragon135 »

I have the same problem like logan..
My trace is the same like him but my answer for problem's sample input is 2 43..
how come the output of the sample input became 1 44.. ?
Please, someone be kind explaining it or post the trace for the problem's sample input??
thanks..
araconda040494
New poster
Posts: 1
Joined: Tue Jun 14, 2011 4:57 pm

Problem 162 - Getting WA

Post by araconda040494 »

Ok I have check with other sample tests in the forum and there's no problem with my code.

http://acm.uva.es/board/viewtopic.php?f ... 510#p18791

About the input system i have done some tricks so that dont mind about that.
One more thing : I use Pascal :(
Please help me :(

Code: Select all

const
        finp = '';
        fout = '';
var
        fi, fo : text;
        s : array[1 .. 4] of string;
        deck : array[1 .. 2] of string;
{----------------}
procedure OpenFile;
begin
        assign(fi, finp);
        reset(fi);

        assign(fo, fout);
        rewrite(fo);
end;
{----------------}
procedure Input;
begin
        readln(fi, s[2]);
        readln(fi, s[3]);
        readln(fi, s[4]);
end;
{----------------}
procedure Convert;
var
        i, j : integer;
        player : integer;
        c : char;
begin
        deck[1] := '';
        deck[2] := '';
        player := 1;

        for i := 1 to 4 do
        begin
                for j := 1 to 13 do
                begin
                        player := 3 - player;
                        c := s[i][3 * (j - 1) + 2];
                        if c = 'J' then deck[player] := '1' + deck[player]
                        else
                        if c = 'Q' then deck[player] := '2' + deck[player]
                        else
                        if c = 'K' then deck[player] := '3' + deck[player]
                        else
                        if c = 'A' then deck[player] := '4' + deck[player]
                        else deck[player] := '0' + deck[player];
                end;
        end;
end;
{----------------}
procedure Generate;
var
        table : string;
        player : integer;
        c : char;
        card : integer;
begin
        player := 1;
        repeat
                table := '';
                repeat
                        player := 3 - player;
                        if deck[player] = '' then exit;
                        c := deck[player][1];
                        table := table + c;
                        delete(deck[player], 1, 1);
                until c <> '0';

                card := ord(c) - 48;
                repeat
                        player := 3 - player;

                        while card > 0 do
                        begin
                                if deck[player] = '' then exit;
                                c := deck[player][1];
                                table := table + c;
                                delete(deck[player], 1, 1);
                                card := card - 1;

                                if c <> '0' then
                                begin
                                        card := ord(c) - 48;
                                        break;
                                end;
                        end;
                until card = 0;

                deck[3 - player] := deck[3 - player] + table;
        until false;
end;
{----------------}
procedure Output;
begin
        if deck[1] = '' then
        begin
                write(fo, '2');
                writeln(fo, length(deck[2]) : 3);
        end
        else
        begin
                write(fo, '1');
                writeln(fo, length(deck[1]) : 3);
        end;
end;
{----------------}
procedure Process;
begin
        readln(fi, s[1]);
        while s[1] <> '#' do
        begin
                Input;
                Convert;
                Generate;
                Output;
                readln(fi, s[1]);
                while s[1] = '' do readln(fi, s[1]);
        end;
end;
{----------------}
procedure CloseFile;
begin
        close(fi);
        close(fo);
end;
{----------------}
begin
        OpenFile;
        Process;
        CloseFile;
end.
zobayer
Experienced poster
Posts: 110
Joined: Tue May 06, 2008 2:18 pm
Location: CSE-DU, Bangladesh
Contact:

Re: Problem 162 - Getting WA

Post by zobayer »

Please use existing thread, do not open new one when it is already there.
You should not always say what you know, but you should always know what you say.
hiddenstones
New poster
Posts: 3
Joined: Fri Sep 14, 2012 1:13 pm

Re: 162 Beggar My Neighbour

Post by hiddenstones »

I have tested all cases from Jan's link and Observer's case and still got WA. What else am I missing?
hiddenstones
New poster
Posts: 3
Joined: Fri Sep 14, 2012 1:13 pm

Re: 162 Beggar My Neighbour

Post by hiddenstones »

I have the same output as yours. What else am I missing? Can somebody give me a hint? Thank you.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 162 Beggar My Neighbour

Post by brianfry713 »

post your code.
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: 162 Beggar My Neighbour

Post by brianfry713 »

post your code.
Check input and AC output for thousands of problems on uDebug!
mennakatkout
New poster
Posts: 1
Joined: Sun Apr 14, 2013 6:18 pm

162 - Beggar My Neighbour

Post by mennakatkout »

I was getting presentation error then after I removed the last endline in the output now I'm getting WA, if someone can supply tricky test cases other than the ones already posted here about this problem, I will totally appreciate it.

Here's my code

Code: Select all


#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<map>
#include<cstdio>
using namespace std;

vector<string> nonD;
vector<string> deal;
map<char,int> mapp;

//(A), 2-9, Ten (T), Jack (J), Queen (Q) and King (K).
void simulate()
{
	vector<string> floor;
	int strt = 2; // start of game for non dealer
	int num = 1;
	bool face=false;
	while(true)
	{

		string currCard;
		if(strt == 2)
		{
			while(num)
			{
				if(nonD.size() == 0) return;
				currCard = nonD[nonD.size()-1];
				nonD.pop_back();
				floor.push_back(currCard);
				if( currCard[1] == 'A' || currCard[1] == 'J' || currCard[1] == 'Q'|| currCard[1] == 'K')
				{
					face = true;
					break;
				}
				num --;
			}
			if(face && num !=0) num = mapp[currCard[1]];
			else if(face && num == 0)
			{
				for(int i = 0 ; i < (int)floor.size() ; i++)
				{
					string f = floor[i];
					deal.insert(deal.begin(), f);
				}
				floor.clear();
				face = false;
				num = 1;
			}
			else if(num == 0) num = 1;
			strt = 1;
		}
		else if( strt ==1)
		{
			while(num)
			{
					if(deal.size() == 0) return;
					currCard = deal[deal.size()-1];
					deal.pop_back();
					floor.push_back(currCard);
					if( currCard[1] == 'A' || currCard[1] == 'J' || currCard[1] == 'Q'|| currCard[1] == 'K')
					{
						face = true;
						break;
					}
				num --;
			}
			if(face && num !=0) num = mapp[currCard[1]];
			else if(face && num == 0)
			{
					for(int i = 0 ; i < (int)floor.size() ; i++)
					{
						string f = floor[i];
						nonD.insert(nonD.begin(), f);
					}
					floor.clear();
					face = false;
					num = 1;
			}
			else if(num == 0) num = 1;
			strt = 2;
		}


	}
}
int main()
{

	mapp['J'] = 1;
	mapp['Q'] = 2;
	mapp['K'] = 3;
	mapp['A'] = 4;
	string input;
	bool last=false;
	bool flag = false; // card to non dealer
	while (true)
	{

		cin>>input;
		if( input =="#")
			break;
		if(last)
		{
			cout<<endl;
			last = false;
		}


		if( !flag) nonD.push_back(input), flag = true;
		else deal.push_back(input), flag = false;

		if( nonD.size() == 26 && deal.size() == 26)
		{
			simulate();
			if(nonD.size() == 0) cout<<"1 "<<deal.size();
			else if(deal.size() == 0) cout<<"2 "<<nonD.size();

			deal.clear();
			nonD.clear();
			last = true;
		}

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

Re: 162 - Beggar My Neighbour

Post by brianfry713 »

There should be a newline at the end of the last line. Each line will consist of the number of the winning player (1 is the dealer, 2 is the first to play) and the number of cards in the winner's hand (ignoring any on the stack), right justified in a field of width 3. So for input:

Code: Select all

H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3
H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3
H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3
H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3 H3
#
AC output is:

Code: Select all

1  0
Your output:

Code: Select all

1 0
See this thread: http://acm.uva.es/board/viewtopic.php?t=192
Check input and AC output for thousands of problems on uDebug!
AmirAz
New poster
Posts: 8
Joined: Tue May 20, 2014 12:34 pm

162 Beggar my Neighbour What's Wrong?

Post by AmirAz »

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <string>
#include <list>
using namespace std;

int card2int(const string &card) {
switch (card[1]) {
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return int(card[1] - '0');
break;
case 'T':
return 10;
break;
case 'J':
return 11;
break;
case 'Q':
return 12;
break;
case 'K':
return 13;
break;
case 'A':
return 14;
break;
}
}

int main(void) {
list<int> p[2], mid;
string f;
int cover=0, turn= 0;
std::cin >> f;
while (f != "#")
{
p[0].clear(); p[1].clear(); mid.clear();
p[0].insert(p[0].begin(), card2int(f));
for (int i = 1; i < 52; i++)
{
std::cin>> f;
p[i%2].insert(p[i%2].begin(), card2int(f));
}
turn = cover = 0;
while (!p[turn].empty())
{
if (!cover)
{

mid.insert(mid.begin(), *p[turn].begin());
p[turn].erase(p[turn].begin());
if (*mid.begin() > 10)
cover = *mid.begin() - 10;


turn = 1- turn;
}
else
if (cover)
{

cover--;
mid.insert(mid.begin(), *p[turn].begin());
p[turn].erase(p[turn].begin());
if (*mid.begin() > 10) {
cover = *mid.begin() - 10;
turn = 1- turn;
}
else if (!cover)
{
mid.reverse();
turn = 1 - turn;
p[turn].insert(p[turn].end(), mid.begin(), mid.end());
mid.clear();
}



}
}

if (!p[1].empty()) cout << "1 " << p[1].size() << "\n";
else cout << "2 "<< p[0].size() << "\n";
std::cin >> f;
}
}

I get WA for this code, could anyone tell me what's wrong with it or give me an input that it works wrong?
Post Reply

Return to “Volume 1 (100-199)”