170 - Clock Patience

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

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Apr 24, 2004 1:32 pm

The simplest way to make big tests is to

1) generate them
2) get some small tests and use them many times.

My 0.000s AC code solved a test case containing 8192 copies of your input data in 1.159s (Athlon 2200). The input size was > 5MB.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Problem 170 TLE

Post by chunyi81 » Wed Apr 28, 2004 4:25 pm

Thanks for your help. I found some problem with the parsing of input in my PC's OS and that of the online judge. I got AC already.

growup
New poster
Posts: 2
Joined: Thu Jan 20, 2005 10:31 pm

Please Help me: Problem 170 Clock Patience (RE)

Post by growup » Sat Jul 16, 2005 4:18 pm

Hi, I tested my code on Linux and my PC several times before submitted it. I got Runtime Error, however. I used an array of 13 Linked lists to solve Problem 170. Are there any problems with the usage of allocat/free memory or I didn't consider some input?
Please give me some suggestions
Thank you very much~~

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

output
04,KD

Code: Select all

#include <stdio.h>
#include <stdlib.h>

struct card {
	int face_down;
	char rank, suit;
	struct card *next; 
}; 

int main()
{
	int i, j, times, target, current;
	char input; 
	
	struct card *p[13], *temp, *temp2;
	
	while(1) {
		times = 0;
		 	
		input = getchar();
		if( input == '#' )
			return 0;
		p[12] = (struct card*)malloc( sizeof(struct card) );
		p[12]->rank = input;
		p[12]->suit = getchar();
		p[12]->next = NULL;
		p[12]->face_down = 1;
		getchar();
		
		// first line of input
		for( i = 11; i >= 0; --i ) {
			p[i] = (struct card*)malloc( sizeof(struct card) );
			p[i]->rank = getchar();
			p[i]->suit = getchar();
			p[i]->next = NULL;
			p[i]->face_down = 1;
			getchar();
		}
		// the other 3 lines of input
		for( j = 0; j < 3; ++j ) {
			for( i = 12; i >= 0; --i ) {
				temp = (struct card*)malloc( sizeof(struct card) );	
				temp2 = p[i];
				while( temp2->next != NULL )
					temp2 = temp2->next;
				temp2->next = temp;
				temp->rank = getchar();
				temp->suit = getchar();
				temp->face_down = 1;
				getchar();
			}		
		}		
		// expose the first card on 'K' pile
		if( p[12]->rank == 'A' )
			target =  0;
		else if( p[12]->rank == 'T' )
		 	target =  9;
		else if( p[12]->rank == 'J' )
			target = 10;
		else if( p[12]->rank == 'Q' )
			target = 11;
		else if( p[12]->rank == 'K' )
			target = 12;
		else	
			target = (int)p[12]->rank - 49;
		// place it at the botton of p[target]
		p[12]->face_down = 0;
		temp = p[12]->next; 
		temp2 = p[target];
		while( temp2->next != NULL )
			temp2 = temp2->next;
		temp2->next = p[12];
		p[12]->next = NULL;
		p[12] = temp;
	
		current = target;
		++times;
		// break if pile indicated by the current card has no face down cards in it
		while( p[current]->face_down == 1 ) {
		
			if( p[current]->rank == 'A' )
				target =  0;
			else if( p[current]->rank == 'T' )
			 	target =  9;
			else if( p[current]->rank == 'J' )
			 	target = 10;
			else if( p[current]->rank == 'Q' )
				target = 11;
			else if( p[current]->rank == 'K' )
				target = 12;		
			else	
				target = (int)p[current]->rank - 49;
			
			p[current]->face_down = 0;
			temp = p[current]->next;
			temp2 = p[target];
			while( temp2->next != NULL )
				temp2 = temp2->next;
			temp2->next = p[current];	
			p[current]->next = NULL;
			p[current] = temp;		
			current = target;
			++times;
		}
		temp2 = p[current];
		while( temp2->next != NULL )
			temp2 = temp2->next;
		// if times is fewer than 10, place a '0' before it
		if( times / 10 > 0 )
			printf( "%d,%c%c\n", times, temp2->rank, temp2->suit );
		else
			printf( "0%d,%c%c\n", times, temp2->rank, temp2->suit );
		for( i = 0; i < 13; ++i )
			free( p[i] );
	}
}

SRX
Learning poster
Posts: 63
Joined: Sat May 14, 2005 8:13 am
Location: Taiwan

170-wa

Post by SRX » Sat Aug 20, 2005 7:06 am

Hello , I'm taiwanese .
I tried many test data , but still wa

Code: Select all

#include <iostream>
#include <cctype>
#include <cstdio>
using namespace std ;

char card [13][4][3] ;
int top [13] , i , ans , j ;  
int Initial () { for ( i=0 ; i<13 ; top[i]=3 , i++ ) ; ans=0 ; }
int Input () {
 int i , j ; 
  for ( i=3 ; i>=0 ; i-- ) 
    for ( j=12 ; j>=0 ; j-- ){
     cin >> card[j][i] ;
     if ( card[j][i][0]=='#')
                     return 0 ; 
  }
}
int value ( char c ){
 if ( isdigit ( c )  )
              return c-'0'-1 ; 
 if ( c=='T' )
              return 9 ; 
 if ( c=='J')
              return 10 ; 
 if ( c=='Q' )
              return 11 ; 
 if ( c=='K' )
              return 12 ; 
 if ( c=='A' )
              return 0 ;
}             
int Play () {
int C = 12 , f ; 
 while ( top[C]!=-1 ){
    ans++ ;
    f=C ;  
    C=value ( card[C][top[f]][0] ) ; 
    top[f]-- ; 
  }
 if ( ans<10 ) cout << '0' ; 
 cout << ans << ',' <<card[f][top[f]+1] << endl  ; 
}
int main () {
 
 while ( Input () )  { 
  Initial () ;
  Play    () ;   } 
}    

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

170?

Post by IRA » Sun Jan 22, 2006 5:48 pm

I don't know the meaning of the problem.
Can someone show me the example about the solution of the problem.
Thanks in advanced!

sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Location: Vancouver, BC, Canada
Contact:

Post by sclo » Mon Jan 23, 2006 11:54 pm


IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Fri Jan 27, 2006 9:51 pm

I still don't know the meaning of the problem...



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

Which card is the first!?

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Fri Jan 27, 2006 11:33 pm

Which card is the first!?
First card is always the first exposed.

In your test case, the cards are exposed in this order:

Code: Select all

6D 3D 4C 3S AD 9C 7S 8H JS 9S 2S 6S 2C 4D 5H 8C QS 4S 2H KS 9D QH JH 7H TD 5S
4H TC JD 8S 2D 6C KH TS 8D JC AH 5D 9H KD AS KC   (answer: 42,KC)

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Sat Jan 28, 2006 10:40 am

mf wrote:
Which card is the first!?
First card is always the first exposed.

In your test case, the cards are exposed in this order:

Code: Select all

6D 3D 4C 3S AD 9C 7S 8H JS 9S 2S 6S 2C 4D 5H 8C QS 4S 2H KS 9D QH JH 7H TD 5S
4H TC JD 8S 2D 6C KH TS 8D JC AH 5D 9H KD AS KC   (answer: 42,KC)
Why is 3D after 6D!? Is not JS!?....
Why is 4C after 3D!?........
Please tell me....
Thanks in advanced!

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Sat Jan 28, 2006 11:08 am

K Q J T 9 8 7 6 5 4 3 2 A
========================
I already know how to solve it.
Thanks!

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Sat Jan 28, 2006 11:35 am

I got WA!
Who can give me some input data to test the result?
Who can help me to test the result,Please?
Thanks in advanced!

Code: Select all

#include <cstdio>
#include <cstring>
#include <list>
#include <string>

using namespace std;

list <string>l[14];

int main()
{
	char sen[1000];
	char *temp;
	int i,count;
	string s1="";
	while(1)
	{
		gets(sen);
		if(sen[0]=='#')
			break;
		count=0;
		s1="";
		for(i=0;i<=13;i++)
			l[i].clear();
		///////////////////////////////////////////////
		temp=strtok(sen," ");
		i=13;
		while(temp)
		{
			l[i--].push_back(s1+temp);
			temp=strtok(0," ");
		}
		///////////////////////////////////////////////
		gets(sen);
		temp=strtok(sen," ");
		i=13;
		while(temp)
		{
			l[i--].push_back(s1+temp);
			temp=strtok(0," ");
		}
		///////////////////////////////////////////////
		gets(sen);
		temp=strtok(sen," ");
		i=13;
		while(temp)
		{
			l[i--].push_back(s1+temp);
			temp=strtok(0," ");
		}
		///////////////////////////////////////////////
		gets(sen);
		temp=strtok(sen," ");
		i=13;
		while(temp)
		{
			l[i--].push_back(s1+temp);
			temp=strtok(0," ");
		}
		///////////////////////////////////////////////

		i=13;

		while(!l[i].empty())
		{
			count++;
			s1=l[i].front();
			l[i].pop_front();
			if(s1[0]=='T')
				i=10;
			else if(s1[0]=='A')
				i=1;
			else if(s1[0]=='K')
				i=13;
			else if(s1[0]=='Q')
				i=12;
			else if(s1[0]=='J')
				i=11;
			else
				i=s1[0]-'0';
		}

		printf("%d,%s\n",count,s1.c_str());


	}
	return 0;
}

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

Post by IRA » Sat Jan 28, 2006 12:18 pm

I find my mistake and got AC.
Thanks!

User avatar
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Check it out.

Post by _.B._ » Wed Aug 02, 2006 11:38 am

Greetings!
I still don't know what's wrong with your code, but under Windows XP it crashes around line 100:

Code: Select all

         while( temp2->next != NULL )
            temp2 = temp2->next;
Hope it helps!
If you already solved it, you might post what you've done, so others can do it too.
_.

jdalbey
New poster
Posts: 3
Joined: Thu Dec 10, 2009 6:05 pm

170 Clock Patience - incorrect spec?

Post by jdalbey » Sat Oct 20, 2012 5:35 am

Hello,

The spec for #170 says:
"The input will consist of decks of cards arranged in four lines of 13 cards, cards separated by a single blank. "
It gives sample input of this:
TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC

When it says "separated by a single blank" I assume that means there are no blanks before or after the 13 cards.
However, when I code my solution according to this interpretation, I get Wrong Answer.
If I change my solution to allow that there may be one or more blanks at the end of the line, I get Accepted.
If that's the case, then shouldn't the spec mention that there could be trailing blanks?
Is my assumption wrong about what "separated" means? Or is the spec in need of improvement?

Below is the relevant snippet of "Accepted" Java code that parses the input, with the "Wrong Answer" code
that meets the stated spec commented out.
Thanks,
--john

Code: Select all

    void readInput()
    {
        String lineInput = "";
        deckArray = new Vector();
        numberOfLines = 0; 
        while (scan.hasNext())
        {
            //lineInput += scan.nextLine() +" "; // Wrong Answer
            lineInput += scan.nextLine().trim() + " "; // Accepted
            numberOfLines++;
            if(numberOfLines%4 == 0)
            {
                deckArray.add(lineInput);
                lineInput = "";
            }
        }
    }

Post Reply

Return to “Volume 1 (100-199)”