600 - A Duckpin Tournament
Posted: Mon Nov 17, 2003 5:17 pm
Couldn't find any mistakes in this code. Tested with many test cases and all ran perfectly, but I still get a WA from the judge. Please help.
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int NOSCORE=16843009;
struct {
char name[11];
int sc[3][30]; // score for each try
}
guy[4];
int nguy;
inline int max3(int a,int b,int c) {
return a>b?(a>c?a:c):(b>c?b:c);
}
int nextframe(int scores[12][3],int last) {
int i,j,sum;
for (i=last;i<12;i++) {
if (scores[0]<0 || scores[1]<0 || scores[2]<0)
continue;
sum=0;
for (j=0;j<3&&scores[j]!=NOSCORE;j++)
sum+=scores[j];
if (sum<10)
return i;
}
}
int *sequence(int scores[12][3]) {
int *rv=new int[30];
int i,j,p;
memset(rv,1,sizeof(int)*30); // default to NOSCORE
p=0;
for (i=0;i<12;i++)
for (j=0;j<3&&scores[j]!=NOSCORE;j++)
rv[p++]=scores[j];
return rv;
}
bool input() {
char line[256];
char *tok;
int scores[12][3],i,j,k,q;
gets(line);
if ((nguy=atoi(line))<1)
return false;
gets(line);
i=0;
for (tok=strtok(line," ");tok!=NULL;tok=strtok(NULL," "))
strcpy(guy[i++].name,tok);
for (j=0;j<3;j++)
for (i=0;i<nguy;i++) {
memset(scores,1,sizeof(scores)); // default to NOSCORE
for (k=0;k<3;k++) {
gets(line);
q=-1;
for (tok=strtok(line," ");tok!=NULL;tok=strtok(NULL," ")) {
q=nextframe(scores,q+1);
scores[q][k]=atoi(tok);
}
}
memcpy(guy.sc[j],sequence(scores),sizeof(guy.sc[j]));
}
gets(line);
return true;
}
void process() {
int nframe,frame,ntry,total[4][3],max,maxp;
int i,j,k,l;
// print score chart
memset(total,0,sizeof(total));
for (j=0;j<3;j++)
for (i=0;i<nguy;i++) {
printf("%-10s",guy.name);
frame=0;
ntry=0;
nframe=0;
for (k=0;k<30&&guy[i].sc[j][k]!=NOSCORE;k++) {
if (guy[i].sc[j][k]<0)
ntry=3;
else {
frame+=guy[i].sc[j][k];
ntry++;
}
if (frame>=10&&nframe<10) {
for (l=0;l<3-ntry;l++) {
if (guy[i].sc[j][k+l+1]<0)
break;
frame+=guy[i].sc[j][k+l+1];
}
ntry=3;
}
if (ntry>=3) {
printf("%4d",(total[i][j]+=frame));
frame=0;
ntry=0;
nframe++;
}
}
printf("\n");
}
// get highest series score
max=0;
for (i=0;i<nguy;i++)
if (max<total[i][0]+total[i][1]+total[i][2]) {
max=total[i][0]+total[i][1]+total[i][2];
maxp=i;
}
printf("%s has the high series score of %d.\n",guy[maxp].name,max);
// get highest game score
max=0;
for (i=0;i<nguy;i++)
if (max<max3(total[i][0],total[i][1],total[i][2])) {
max=max3(total[i][0],total[i][1],total[i][2]);
maxp=i;
}
printf("%s has the high game score of %d.\n",guy[maxp].name,max);
printf("\n");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
while (input())
process();
return 0;
}
[/cpp]
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int NOSCORE=16843009;
struct {
char name[11];
int sc[3][30]; // score for each try
}
guy[4];
int nguy;
inline int max3(int a,int b,int c) {
return a>b?(a>c?a:c):(b>c?b:c);
}
int nextframe(int scores[12][3],int last) {
int i,j,sum;
for (i=last;i<12;i++) {
if (scores[0]<0 || scores[1]<0 || scores[2]<0)
continue;
sum=0;
for (j=0;j<3&&scores[j]!=NOSCORE;j++)
sum+=scores[j];
if (sum<10)
return i;
}
}
int *sequence(int scores[12][3]) {
int *rv=new int[30];
int i,j,p;
memset(rv,1,sizeof(int)*30); // default to NOSCORE
p=0;
for (i=0;i<12;i++)
for (j=0;j<3&&scores[j]!=NOSCORE;j++)
rv[p++]=scores[j];
return rv;
}
bool input() {
char line[256];
char *tok;
int scores[12][3],i,j,k,q;
gets(line);
if ((nguy=atoi(line))<1)
return false;
gets(line);
i=0;
for (tok=strtok(line," ");tok!=NULL;tok=strtok(NULL," "))
strcpy(guy[i++].name,tok);
for (j=0;j<3;j++)
for (i=0;i<nguy;i++) {
memset(scores,1,sizeof(scores)); // default to NOSCORE
for (k=0;k<3;k++) {
gets(line);
q=-1;
for (tok=strtok(line," ");tok!=NULL;tok=strtok(NULL," ")) {
q=nextframe(scores,q+1);
scores[q][k]=atoi(tok);
}
}
memcpy(guy.sc[j],sequence(scores),sizeof(guy.sc[j]));
}
gets(line);
return true;
}
void process() {
int nframe,frame,ntry,total[4][3],max,maxp;
int i,j,k,l;
// print score chart
memset(total,0,sizeof(total));
for (j=0;j<3;j++)
for (i=0;i<nguy;i++) {
printf("%-10s",guy.name);
frame=0;
ntry=0;
nframe=0;
for (k=0;k<30&&guy[i].sc[j][k]!=NOSCORE;k++) {
if (guy[i].sc[j][k]<0)
ntry=3;
else {
frame+=guy[i].sc[j][k];
ntry++;
}
if (frame>=10&&nframe<10) {
for (l=0;l<3-ntry;l++) {
if (guy[i].sc[j][k+l+1]<0)
break;
frame+=guy[i].sc[j][k+l+1];
}
ntry=3;
}
if (ntry>=3) {
printf("%4d",(total[i][j]+=frame));
frame=0;
ntry=0;
nframe++;
}
}
printf("\n");
}
// get highest series score
max=0;
for (i=0;i<nguy;i++)
if (max<total[i][0]+total[i][1]+total[i][2]) {
max=total[i][0]+total[i][1]+total[i][2];
maxp=i;
}
printf("%s has the high series score of %d.\n",guy[maxp].name,max);
// get highest game score
max=0;
for (i=0;i<nguy;i++)
if (max<max3(total[i][0],total[i][1],total[i][2])) {
max=max3(total[i][0],total[i][1],total[i][2]);
maxp=i;
}
printf("%s has the high game score of %d.\n",guy[maxp].name,max);
printf("\n");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
while (input())
process();
return 0;
}
[/cpp]