101 RTE !
Posted: Tue Oct 22, 2002 8:27 pm
by Moni
Hello! Everybody!
I am in great trouble with this code. It is giving "Run-Time Error" in the judge's machine. I have compiled it in VC++, TC++ and also in BC++, but there was neither error nor warning. I think not only the sample input output was correct but also this is the correct solution for the problem 101.
Can any C++ expert help me???
[cpp]
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define ROW 100
#define COL 100
int i=0,j=0,t=0,f=0;
int grid[ROW][COL];
int num=0;
inline void init(void)
{
memset(grid,-1,sizeof(grid)); // Initialize all array to -1 as it can differ from data;
}
void setgrid(int &data);
void print(void);
void locate(int &data,int &x,int &y);
inline int check(int &ar,int &ac,int &br,int &bc);
void back(int &row,int &col);
void move_onto(int Chk,int &a,int &b,int &ar,int &ac,int &br,int &bc);
void move_over(int Chk,int &a,int &b,int &br,int &bc);
void pile_onto(int Chk,int &a,int &b,int &ar,int &ac,int &br,int &bc);
void pile_over(int Chk,int &a,int &b,int &br,int &bc);
void back(int &row,int &col)
{
for(i=(ROW-1);i!=row;i--)
{
if(grid[col]!=-1)
{
grid[0][grid[col]]=grid[col];
grid[col]=-1;
}
}
}
void setgrid(int &data)
{
for(i=0;i<data;i++)
grid[0]=i;
}
void print(void)
{
for(j=0,t=0;j<COL;j++)
{
if(t<num)
{
printf("%2d:",t);
for(i=0;i<ROW;i++)
{
if(grid[j]!=-1)
cout << ' ' << grid[j];
}
cout << endl;
t++;
}
}
}
void locate(int &data,int &x,int &y)
{
for(i=0;i<ROW;i++)
{
f=0;
for(j=0;j<COL;j++)
if(grid[j]==data)
{
x=i;
y=j;
f=1; // If you find the data and its location BREAK the loop;
break;
};
if(f==1)
break;
}
}
int check(int &ar,int &ac,int &br,int &bc)
{
if(ac==bc)
return -1; // If in same stack return -1;
if(grid[ar+1][ac]>0 && grid[br+1][bc]>0) // if both have above something return 3
return 3;
if(grid[ar+1][ac]>0) // if somehting above a return 1
return 1;
if(grid[br+1][bc]>0)
return 2; // if something above b return 2
return 0;
}
void move_onto(int Chk,int &a,int &b,int &ar,int &ac,int &br,int &bc)
{
if(Chk==0)
{
grid[ar][ac]=-1;
grid[br+1][bc]=a;
}
else if(Chk==3)
{
back(ar,ac);
back(br,bc);
move_onto(check(ar,ac,br,bc),a,b,ar,ac,br,bc);
}
else if(Chk==1)
{
back(ar,ac);
move_onto(check(ar,ac,br,bc),a,b,ar,ac,br,bc);
}
else if(Chk==2)
{
back(br,bc);
move_onto(check(ar,ac,br,bc),a,b,ar,ac,br,bc);
}
}
void move_over(int Chk,int &a,int &b,int &br,int &bc)
{
int ar,ac;
locate(a,ar,ac);
int row=br,col=bc;
if(Chk==2 || Chk==0)
{
if(grid[row+1][col]==-1)
move_onto(Chk,a,b,ar,ac,row,col);
else
{
for(int t=row;t<ROW;t++)
if(grid[t][col]==-1)
{
row=t-1;
break;
}
move_onto(check(ar,ac,row,col),a,grid[row][col],ar,ac,row,col);
}
}
else if(Chk==3 || Chk==1)
{
back(ar,ac);
move_over(check(ar,ac,br,bc),a,b,br,bc);
}
}
void pile_onto(int Chk,int &a,int &b,int &ar,int &ac,int &br,int &bc)
{
if(Chk==0 || Chk==2)
move_onto(Chk,a,b,ar,ac,br,bc);
else if(grid[br+1][bc]==-1)
{
while(grid[ar][ac]!=-1)
{
grid[++br][bc]=grid[ar][ac];
grid[ar][ac]=-1;
ar++;
}
}
else if(Chk==3 || Chk==1)
{
back(br,bc);
pile_onto(check(ar,ac,br,bc),a,b,ar,ac,br,bc);
}
}
void pile_over(int Chk,int &a,int &b,int &br,int &bc)
{
int row=br;
int col=bc;
int ar,ac;
locate(a,ar,ac);
if(grid[row+1][col]==-1)
pile_onto(Chk,a,b,ar,ac,br,bc);
else
{
for(int t=row;t<ROW;t++)
if(grid[t][col]==-1)
{
row=t-1;
break;
}
pile_onto(check(ar,ac,row,col),a,grid[row][col],ar,ac,row,col);
}
}
int main()
{
char command[5]={0},type[5]={0};
int from=0,to=0;
cin >> num;
init();
setgrid(num);
while(strcmp(command,"quit")!=0)
{
cin >> command;
if(strcmp(command,"move")==0)
{
cin >> from;
cin >> type;
cin >> to;
int fr,fc,tr,tc;
locate(from,fr,fc);
locate(to,tr,tc);
int Chk=check(fr,fc,tr,tc);
if(Chk==-1)
continue;
else if(strcmp(type,"onto")==0)
{
move_onto(Chk,from,to,fr,fc,tr,tc);
}
else if(strcmp(type,"over")==0)
{
move_over(Chk,from,to,tr,tc);
}
}
else if(strcmp(command,"pile")==0)
{
cin >> from;
cin >> type;
cin >> to;
int fr,fc,tr,tc;
locate(from,fr,fc);
locate(to,tr,tc);
int Chk=check(fr,fc,tr,tc);
if(Chk==-1)
continue;
else if(strcmp(type,"onto")==0)
{
pile_onto(Chk,from,to,fr,fc,tr,tc);
}
else if(strcmp(type,"over")==0)
{
pile_over(Chk,from,to,tr,tc);
}
}
}
print();
return 0;
}
[/cpp]
101 - WA though it works fine on example data
Posted: Sat Oct 26, 2002 3:49 am
by Archer
Hi,
I don't quite understand why I get a WA error with this code: (works perfectly with the example input)
[c]
/* @JUDGE_ID: 21775WY 101 C */
/* Problem Number 101 */
/* Robert Spielmann (Archer)*/
/*
rsp@byteforge.org */
void m_onto(int ii, int jj);
void m_over(int ii, int jj);
void p_onto(int ii, int jj);
void p_over(int ii, int jj);
void clear_top(int ii);
void print_world();
struct box {
int id; /* number of box */
int position; /* position */
int height; /* height in stack */
} boxes[25];
char world[25][26];
char command[2][5], c;
char line[16];
int a, b, i, j, m, n, on, worldsize;
int main(void) {
/* inits */
memset(world, 0x20, 650);
memset(command, 0, 10);
/* read world size */
scanf("%i", &worldsize);
c = getchar();
/* put boxes on initial positions */
for(i=0; i<worldsize; i++) {
boxes
.id = boxes.position = i;
boxes.height = 0;
world[0] = i;
}
i=0;
/* parse commands until "quit" is found */
while(1) {
/* read a line up to \n */
while((c=getchar())!='\n')
line[i++] = c;
line='\0'; /* terminate line */
i=0;
/* if only one argument: quit */
if(sscanf(line, "%s %i %s %i", &command[0], &a, &command[1], &b) < 4)
break;
/* set vars for function call decision */
if(strcmp(command[0], "move")==0) m=1; else m=0;
if(strcmp(command[1], "onto")==0) on=1; else on=0;
/* switch to decide which function to call */
switch(m) {
case 1:
if(on==1) m_onto(a, b);
else m_over(a, b);
break;
case 0:
if(on==1) p_onto(a, b);
else p_over(a, b);
break;
}
}
print_world();
}
void m_onto(int ii, int jj) {
if(ii==jj) return;
if(boxes[ii].position==boxes[jj].position) return;
clear_top(ii); clear_top(jj);
world[boxes[jj].position][boxes[jj].height+1] = ii;
world[boxes[ii].position][boxes[ii].height] = 0x20;
boxes[ii].position = boxes[jj].position;
boxes[ii].height = boxes[jj].height+1;
}
void m_over(int ii, int jj) {
int nw = boxes[jj].position;
int nh = boxes[jj].height;
if(ii==jj) return;
if(boxes[ii].position==nw) return;
while(world[nw][nh]!=0x20) nh++;
world[nw][nh] = ii;
world[boxes[ii].position][boxes[ii].height]=0x20;
boxes[ii].position=nw;
boxes[ii].height = nh;
}
void p_onto(int ii, int jj) {
int nh = boxes[jj].height+1;
int nw = boxes[jj].position;
int ow = boxes[ii].position;
int oh = boxes[ii].height;
if(ii==jj) return;
if(nw==ow) return;
clear_top(jj);
for( ; world[ow][oh] != 0x20 ; nh++, oh++) {
world[nw][nh] = world[ow][oh];
world[ow][oh] = 0x20;
boxes[ii].position = nw;
boxes[ii].height = nh;
}
}
void p_over(int ii, int jj) {
int nh = boxes[jj].height+1;
int nw = boxes[jj].position;
int ow = boxes[ii].position;
int oh = boxes[ii].height;
if(ii==jj) return;
if(ow == nw) return;
for( ; world[ow][oh] != 0x20 ; nh++, oh++) {
world[nw][nh] = world[ow][oh];
world[ow][oh] = 0x20;
boxes[ii].position = nw;
boxes[ii].height = nh;
}
}
/* fn to unstack boxes above box ii */
void clear_top(int ii) {
int temp;
int where = boxes[ii].position;
int height = boxes[ii].height+1;
while(1) {
temp = world[where][height];
if(temp==0x20) break;
world[temp][0]=temp;
world[where][height]=0x20;
boxes[temp].position = boxes[temp].id;
boxes[temp].height = 0;
++height;
}
}
void print_world() {
for(i=0; i<worldsize; i++) {
printf("%2i:", i);
j=0;
while(world[j]!=0x20)
printf(" %i", world[j++]);
printf("\n");
}
}
[/c]