170 - Clock Patience
Moderator: Board moderators
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Problem 170 TLE
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.
Please Help me: Problem 170 Clock Patience (RE)
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
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] );
}
}
170-wa
Hello , I'm taiwanese .
I tried many test data , but still wa
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 () ; }
}
read the following post:
http://online-judge.uva.es/board/viewto ... hlight=170
http://online-judge.uva.es/board/viewto ... hlight=170
First card is always the first exposed.Which card is the first!?
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!?....mf wrote:First card is always the first exposed.Which card is the first!?
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 4C after 3D!?........
Please tell me....
Thanks in advanced!
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!
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;
}
Check it out.
Greetings!
I still don't know what's wrong with your code, but under Windows XP it crashes around line 100:
Hope it helps!
If you already solved it, you might post what you've done, so others can do it too.
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;
If you already solved it, you might post what you've done, so others can do it too.
_.
170 Clock Patience - incorrect spec?
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
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 = "";
}
}
}