101 - The Blocks Problem

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

opportunity
New poster
Posts: 9
Joined: Thu Sep 15, 2005 11:35 pm
Location: dhaka

Post by opportunity »

hi,
i think u have made mistake while assigning value to variable which is not sufficient to hold this ....so it is making invalid memory addressing which causing runtime error....

the more possible i can say is that ur program might have tried to index an array which is beyond the size of that array....for example....
say u declared :: int ar[2]; int i=0;
at times i might incremented to say 4 that means at times i=4
so ar surely cause runtime error.....isn't it?


i hope u will not have runtime error any more...
plz keep posting....

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

Now I have a compile error 101

Post by Fadia Ismael »

I changed my code thus, and I had a compile error, even it works properly on my computer, Can you please tell me the error??

Thans in advane
Here is the code (C++):

Code: Select all

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

void returnBlocks(int origin[][25], int ai, int aj){
	int temp;
	while((temp=origin[ai][aj])!=-1 && temp>=0 && temp<24){
		origin[temp][0]=temp;
		origin[temp][1]=-1;
		origin[ai][aj]=-1;
		aj++;
	}
	
}
		
int origin[24][26]={0};
void main(){

	int n, a, b, ai, aj, bi, bj, i, j,t;
	cin>>n;
	if (n<1)
		exit(1);
	if (n>24)
		n=24;
	for (i=0; i<n; i++){
		origin[i][0]=i;
		origin[i][1]=-1;}
	char first[5];
	char second[5];
	cin>>first;
	

	while (strcmp(first, "quit")!=0){
		cin>>a;
		cin>>second;
		
		cin>>b;

		if(a==b || a<0 || a>=n || b<0 || b>=n){
			cin>>first;
				continue;
				}
		for (i=0; i<n; i++){
			for (j=0; j<n; j++){
				if (origin[i][j]==a){
					ai=i;
					aj=j;}
				if (origin[i][j]==b){
					bi=i;
					bj=j;}
		}}
			if (ai==bi){
				cin>>first;
				continue;}

		if (strcmp(first,"move")==0 && strcmp(second, "onto")==0){
			returnBlocks(origin,ai,aj+1);
			returnBlocks(origin,bi,bj+1);
			origin[bi][bj+1]=origin[ai][aj];
			origin[bi][bj+2]=-1;
			origin[ai][aj]=-1;
		}

		else
			if(strcmp(first,"move")==0 && strcmp(second,"over")==0){
				returnBlocks(origin,ai,aj+1);
				for(j=bj;origin[bi][j]!=-1;j++);
				origin[bi][j]=a;
				origin[bi][j+1]=-1;
				origin[ai][aj]=-1;
			}
			else 
				if(strcmp(first,"pile")==0 && strcmp(second,"onto")==0){
					returnBlocks(origin,bi,bj+1);
					for(j=bj+1,i=aj;origin[ai][i]!=-1;j++,i++)
						origin[bi][j]=origin[ai][i];
					origin[ai][aj]=-1;
					origin[bi][j]=-1;
				}
				else
					if(strcmp(first,"pile")==0 && strcmp(second,"over")==0){
						for(j=bj;origin[bi][j]!=-1;j++);
						for(i=aj,t=j ;origin[ai][i]!=-1;i++,t++)
							origin[bi][t]=origin[ai][i];
						origin[bi][t]=-1;
						origin[ai][aj]=-1;
					}
cin>>first;
	}
	
	for (i=0; i<n; i++){
		cout<<i<<":";
		for (j=0; origin[i][j]!=-1;j++)
			cout<<" "<<origin[i][j];
		cout<<endl;
	}
}
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

Runtime error now101

Post by Fadia Ismael »

Sorry, this is the last code I have, and it gives me a runtime error, I checked my code for a lot of states, and I think that I don't assign a number more that the array capability, so what is your opinion??

I wish you can help me in solving this problem, thanks alot.....
here is the code:

Code: Select all

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

void returnBlocks(int origin[][26], int ai, int aj){
	int temp;
	while((temp=origin[ai][aj])!=-1 && temp>=0 && temp<24){
		origin[temp][0]=temp;
		origin[temp][1]=-1;
		origin[ai][aj]=-1;
		aj++;
	}
	
}
		
int origin[24][26]={0};
void main(){

	int n, a, b, ai, aj, bi, bj, i, j,t;
	cin>>n;
	if (n<1)
		exit(1);
	if (n>24)
		n=24;
	for (i=0; i<n; i++){
		origin[i][0]=i;
		origin[i][1]=-1;}
	char first[5];
	char second[5];
	cin>>first;
	

	while (strcmp(first, "quit")!=0){
		cin>>a;
		cin>>second;
		
		cin>>b;

		if(a==b || a<0 || a>=n || b<0 || b>=n){
			cin>>first;
				continue;
				}
		for (i=0; i<n; i++){
			for (j=0; j<n; j++){
				if (origin[i][j]==a){
					ai=i;
					aj=j;}
				if (origin[i][j]==b){
					bi=i;
					bj=j;}
		}}
			if (ai==bi){
				cin>>first;
				continue;}

		if (strcmp(first,"move")==0 && strcmp(second, "onto")==0){
			returnBlocks(origin,ai,aj+1);
			returnBlocks(origin,bi,bj+1);
			origin[bi][bj+1]=origin[ai][aj];
			origin[bi][bj+2]=-1;
			origin[ai][aj]=-1;
		}

		else
			if(strcmp(first,"move")==0 && strcmp(second,"over")==0){
				returnBlocks(origin,ai,aj+1);
				for(j=bj;origin[bi][j]!=-1;j++);
				origin[bi][j]=a;
				origin[bi][j+1]=-1;
				origin[ai][aj]=-1;
			}
			else 
				if(strcmp(first,"pile")==0 && strcmp(second,"onto")==0){
					returnBlocks(origin,bi,bj+1);
					for(j=bj+1,i=aj;origin[ai][i]!=-1;j++,i++)
						origin[bi][j]=origin[ai][i];
					origin[ai][aj]=-1;
					origin[bi][j]=-1;
				}
				else
					if(strcmp(first,"pile")==0 && strcmp(second,"over")==0){
						for(j=bj;origin[bi][j]!=-1;j++);
						for(i=aj,t=j ;origin[ai][i]!=-1;i++,t++)
							origin[bi][t]=origin[ai][i];
						origin[bi][t]=-1;
						origin[ai][aj]=-1;
					}
cin>>first;
	}
	
	for (i=0; i<n; i++){
		cout<<i<<":";
		for (j=0; origin[i][j]!=-1;j++)
			cout<<" "<<origin[i][j];
		cout<<endl;
	}
}
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

opportunity
New poster
Posts: 9
Joined: Thu Sep 15, 2005 11:35 pm
Location: dhaka

Post by opportunity »

hi,
thanx for ur reply. i am little busy right now with my academics. that's why, i could not verify ur code. within two or three days u will get some clue of how to resolve the prob. by this time u can try some other problems. i am really really sorry for this inconvenience.....happy programming.... :wink:

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

Thanks very much..

Post by Fadia Ismael »

I'm waiting.. thank you very very much, I wish you can help me...
Thank you very much...
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

Thanks, I get accepted..

Post by Fadia Ismael »

Thank you very much, I made simple changes in my code and I get accepted, I appreciate your help.. Thanks very much.. If you want to see the changes that I made in my code, send me your mail and I will send you my code...

Thanks another time....
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

ztamimi
New poster
Posts: 1
Joined: Fri Oct 07, 2005 8:31 pm

Post by ztamimi »

I tried my code against all available samples on the board and it worked, yet I get WA each time. I double checked the format of the output, the spaces and everything. Would you please try my code? Thanks in advance.

#include <stdio.h>
#include <string.h>
#include <stack>

using std::stack;

void onto(short, short);
void over(short, short);
void pileonto(short, short);
void pileover(short, short);

stack<short> * stacks;
stack<short> temp;
short* loc;
short n;

main()
{
short i,x;
scanf("%d",&n);
stacks = new stack<short> [n];
loc = new short[n];
short a,b;
char op[5];
char fun[5];

/*init*/
for (i=0;i<n;i++)
{
loc = i;
stacks.push(i);
}

/*command*/
scanf("%s",op);
while (strcmp(op,"quit")!=0)//op[0]!= 'q' && op[0]!='Q')
{
scanf("%d%s%d", &a,fun,&b);
if (strcmp(op,"pile")==0)//op[0]=='p' || op[0]=='P')
if (strcmp(fun,"onto")==0)//fun[1]=='n' || fun[1]=='N')
pileonto(a,b);
else
pileover(a,b);
else
if (strcmp(fun,"onto")==0)//fun[1]=='n' || fun[1]=='N')
onto(a,b);
else
over(a,b);
scanf("%s",op);
}

/* output */
for (i=0;i<n;i++)
{
printf("%d:",i);
if (!stacks.empty())
{
do
{
x = stacks.top();
temp.push(x);
stacks.pop();
} while (!stacks.empty());

while (!temp.empty())
{
x = temp.top();
temp.pop();
printf(" %d",x);
// if (!temp.empty()) printf(" ");
}
}
/* if (i<n-1)*/ printf("\n");
}

delete [] stacks;
delete [] loc;

return 0;
}

void onto(short a, short b)
{
short x,a_loc,b_loc;
a_loc = loc[a];
b_loc = loc;
if (a==b || a_loc==b_loc) return;
while (stacks[a_loc].top() !=a)
{
x = stacks[a_loc].top();
stacks[a_loc].pop();
stacks[x].push(x);
loc[x] = x;
}

while (stacks[b_loc].top() !=b)
{
x = stacks[b_loc].top();
stacks[b_loc].pop();
stacks[x].push(x);
loc[x] = x;
}
stacks[a_loc].pop();
stacks[b_loc].push(a);
loc[a] = b_loc;
}

void over(short a, short b)
{
short x,a_loc, b_loc;
a_loc= loc[a];
b_loc = loc;
if (a==b || a_loc==b_loc) return;

while (stacks[a_loc].top() != a)
{
x = stacks[a_loc].top();
stacks[a_loc].pop();
stacks[x].push(x);
loc[x] = x;
}

stacks[a_loc].pop();
stacks[b_loc].push(a);
loc[a] = b_loc;
}

void pileover(short a, short b)
{
short x,a_loc,b_loc;

a_loc = loc[a];
b_loc = loc;

if (a==b || a_loc== b_loc) return;

while (stacks[a_loc].top() !=a)
{
x= stacks[a_loc].top();
temp.push(x);
stacks[a_loc].pop();
}

stacks[a_loc].pop();
stacks[b_loc].push(a);
loc[a] = b_loc;

while (!temp.empty())
{
x = temp.top();
temp.pop();
stacks[b_loc].push(x);
loc[x]=b_loc;
}
}

void pileonto(short a, short b)
{
short x,a_loc,b_loc;

a_loc = loc[a];
b_loc = loc;

if (a==b || a_loc== b_loc) return;

while (stacks[a_loc].top() !=a)
{
x= stacks[a_loc].top();
temp.push(x);
stacks[a_loc].pop();
}

while (stacks[b_loc].top() !=b)
{
x = stacks[b_loc].top();
stacks[b_loc].pop();
stacks[x].push(x);
loc[x] = x;
}

stacks[a_loc].pop();
stacks[b_loc].push(a);
loc[a] = b_loc;

while (!temp.empty())
{
x = temp.top();
temp.pop();
stacks[b_loc].push(x);
loc[x]=b_loc;
}
}

Fadia Ismael
New poster
Posts: 22
Joined: Wed Aug 17, 2005 3:04 pm

I think, but I'm not sure

Post by Fadia Ismael »

Hello,
I think that you must consider a situation like this:
3
move 1 over 3
quit
your code give:
0: 0
1:
2: 2
And I think that this is an error, because 3 is not in the range 0-2, and:
All illegal commands should be ignored and should have no affect on the configuration of blocks.
This may be true and may be not, so please try it, and tell us the result..
so keep posting.. :wink:
It's lack of faith that makes people afraid of meeting challanges, and I believe in myself.

15560ML
New poster
Posts: 1
Joined: Sun Oct 09, 2005 11:38 am

Another WA problem

Post by 15560ML »

Hello!
We got WA every time!!! All situations are checked. Here is our code:
program task101;
var
line: array[0..24, 0..24] of integer;
Lengths: array[0..24] of integer;
N: byte;
s: string;
i, j, k, m: integer;
cmd1, cmd2: string;
Param1, Param2: integer;
Pile1, Pile2: integer;
Delta: integer;
Flag: boolean;

function IntToStrEx(value: integer): string;
var
s: string;
begin
str(Value, s);
IntToStrEx := s;
end;


function StrToInt(Value:string):integer;
var
c,i:integer;
begin
val(Value,i,c);
Flag := False;
if c <> 0 then
Flag := True
else
StrToInt:=i;
end;


{
function StrToInt(Value: string): integer;
begin
Result := 0;
if Length(s) = 1 then
case Value[1] of
'0': Result := 0;
'1': Result := 1;
'2': Result := 2;
'3': Result := 3;
'4': Result := 4;
'5': Result := 5;
'6': Result := 6;
'7': Result := 7;
'8': Result := 8;
'9': Result := 9;
end
else
case Value[1] of
'2':
case Value[2] of
'0': Result := 20;
'1': Result := 21;
'2': Result := 22;
'3': Result := 23;
'4': Result := 24;
end;
'1':
case Value[2] of
'0': Result := 10;
'1': Result := 11;
'2': Result := 12;
'3': Result := 13;
'4': Result := 14;
'5': Result := 15;
'6': Result := 16;
'7': Result := 17;
'8': Result := 18;
'9': Result := 19;
end;
end;
end;
}

function GetPosition(Param: integer): integer;
begin
GetPosition := -1;
for k := 0 to N - 1 do
for m := 0 to Lengths[k] - 1 do
if Line[k, m] = Param then
begin
GetPosition := k;
exit;
end;
end;

procedure MoveOver;
begin
Pile1 := GetPosition(Param1);
Pile2 := GetPosition(Param2);

if (Pile1 = Pile2)or(Pile1=-1)or(Pile2=-1) then
exit;

Inc(Lengths[Pile2]);
Line[Pile2, Lengths[Pile2] - 1] := Param1;

for i := 0 to Lengths[Pile1] -1 do
if Param1 = Line[Pile1, i] then
begin
for j := i to Lengths[Pile1] - 2 do
Line[Pile1, j] := Line[Pile1, j + 1];
Lengths[Pile1] := Lengths[Pile1] - 1;
break;
end;
end;

procedure MoveOnto;
begin
Pile1 := GetPosition(Param1);
Pile2 := GetPosition(Param2);

if (Pile1 = Pile2)or(Pile1=-1)or(Pile2=-1) then
exit;

Inc(Lengths[Pile2]);
for i := 0 to Lengths[Pile2] -1 do
if Param2 = Line[Pile2, i] then
begin
for j := Lengths[Pile2] - 2 downto i + 1 do
Line[Pile2, j + 1] := Line[Pile2, j];
Line[Pile2, i + 1] := Param1;
break;
end;

for i := 0 to Lengths[Pile1] - 1 do
if Param1 = Line[Pile1, i] then
begin
for j := i to Lengths[Pile1] - 2 do
Line[Pile1, j] := Line[Pile1, j + 1];
Dec(Lengths[Pile1]);
break;
end;
end;

procedure PileOver;
begin
Pile1 := GetPosition(Param1);
Pile2 := GetPosition(Param2);

if (Pile1 = Pile2)or(Pile1=-1)or(Pile2=-1) then
exit;

for i := 0 to Lengths[Pile1] - 1 do
if Param1 = Line[Pile1, i] then
begin
Delta := Lengths[Pile1] - i;
Inc(Lengths[Pile2], Delta);
for j := i to Lengths[Pile1] - 1 do
Line[Pile2, Lengths[Pile2] - Delta + j - i] := Line[Pile1, j];
Dec(Lengths[Pile1], Delta);
break;
end;

end;

procedure PileOnto;
var
P2: integer;
Pos2: integer;
begin
Pile1 := GetPosition(Param1);
Pile2 := GetPosition(Param2);

if (Pile1 = Pile2)or(Pile1=-1)or(Pile2=-1) then
exit;

for i := 0 to Lengths[Pile1] - 1 do
if Param1 = Line[Pile1, i] then
begin
Delta := Lengths[Pile1] - i;
P2 := Lengths[Pile2] - 1;
for j := 0 to Lengths[Pile2] - 1 do
if Line[Pile2, j] = Param2 then
begin
Pos2 := j;
break;
end;

Inc(Lengths[Pile2], Delta);
for j := P2 downto Pos2 + 1 do
Line[Pile2, j + Delta] := Line[Pile2, j];
for j := i to Lengths[Pile1] - 1 do
Line[Pile2, Pos2 + j - i + 1] := Line[Pile1, j];
Dec(Lengths[Pile1], Delta);
break;
end;


end;

procedure ShowArray;
begin
for i := 0 to N - 1 do
begin
write(IntToStrEx(i) + ':');
for j := 0 to Lengths - 2 do
write(' ', Line[i, j]);
if i = N - 1 then
if Lengths > 0 then
write(' ', Line[i, Lengths - 1])
else
write
else
if Lengths > 0 then
writeln(' ', Line[i, Lengths - 1])
else
writeln;
end;
end;

begin
readln(N);
for i := 0 to N - 1 do
begin
Lengths := 1;
Line[i, 0] := i;
end;

readln(s);
while s <> 'quit' do
begin
cmd1 := copy(s, 1, pos(' ', s) - 1);
s := copy(s, pos(' ', s) + 1, length(s));
Param1 := StrToInt(copy(s, 1, pos(' ', s) - 1));
s := copy(s, pos(' ', s) + 1, length(s));
Cmd2 := copy(s, 1, pos(' ', s) - 1);
s := copy(s, pos(' ', s) + 1, length(s));
Param2 := StrToInt(s);
if Flag then
begin
readln(s);
continue;
end;

if Cmd1 = 'move' then
if Cmd2 = 'over' then
MoveOver
else
MoveOnto
else
if Cmd2 = 'over' then
PileOver
else
PileOnto;

readln(s);
end;

ShowArray;
end.


Can anyone help?

h001122
New poster
Posts: 9
Joined: Thu Feb 24, 2005 5:59 am

Hmm...

Post by h001122 »

Check your E-mail First.(If you checked 'The host I used to send that program' on 'You want to receive the replies from the Online Judge to : ', when you registered)

Your E-mail account maybe received the mail from Online Judge.
Just check the E-mail. Then, you will see what's the problem.

(Sorry to my silly English.)
Do you know why you're living?

ar2rd
New poster
Posts: 16
Joined: Sat Oct 22, 2005 2:05 am

101 RE and no ideas

Post by ar2rd »

Can anybody help with that code. I try to find some bug that makes runtime error but it always works. Thanks in advance, for any replies.

Code: Select all

#include <cstdio>
#include <cstring>
#define MAXN 25 
using namespace std;

long contains[MAXN][MAXN], pos[MAXN], size[MAXN];

void setup(){
	int i,j;

	for(i = 0; i < MAXN; i++)
	for(j = 0; j < MAXN; j++)
		contains[i][j] = 0;
	
	for(i = 0; i < MAXN; i++){
		 pos[i] = i;
		size[i] = 1;
		contains[i][0] = i;
	}
}
int main(){
	long i,j,from,to,N,temp,del;
	char com[MAXN];

	setup();

	scanf("%d", &N);
	
	while(1){
		
		scanf("%s", com);

		if(strcmp(com,"quit") == 0){
			for(i = 0; i < N; i++){
				printf("%d:", i);
				for(j = 0; j < size[i]; j++)
					printf(" %d", contains[i][j]);
			
				printf("\n");
			}
			break;			

		}


		if(strcmp(com, "move") == 0){
			
			scanf("%d %s %d", &from, com, &to);
			
			if(from < 0 || from >= N || to < 0 || to >= N || from == to || pos[from] == pos[to]);
			else{

				for(i = size[pos[from]]-1; i>=0 && contains[pos[from]][i] != from; i--){

					size[pos[from]]--;
					contains[contains[pos[from]][i]][size[contains[pos[from]][i]]++] = contains[pos[from]][i];
					pos[contains[pos[from]][i]] = contains[pos[from]][i];
				}
				if(strcmp(com, "onto") == 0)

					for(i = size[pos[to]]-1; i>=0 && contains[pos[to]][i] != to; i--){
					
						size[pos[to]]--;
						contains[contains[pos[to]][i]][size[contains[pos[to]][i]]++] = contains[pos[to]][i];
						pos[contains[pos[to]][i]] = contains[pos[to]][i];
					}

				contains[pos[to]][size[pos[to]]++] = contains[pos[from]][--size[pos[from]]];
				pos[from] = to;

			}
		}
		else if(strcmp(com, "pile") == 0){
			
			scanf("%d %s %d", &from, com, &to);
			
			if(from < 0 || from >= N || to < 0 || to >= N || from == to || pos[from] == pos[to]);
			else{
				
				if(strcmp(com, "onto") == 0)

					for(i = size[pos[to]]-1; i>=0 && contains[pos[to]][i] != to; i--){
					
						size[pos[to]]--;
						contains[contains[pos[to]][i]][size[contains[pos[to]][i]]++] = contains[pos[to]][i];
						pos[contains[pos[to]][i]] = contains[pos[to]][i];
					}

				for(i = 0; contains[pos[from]][i] != from; i++);
		
				temp = pos[from];
				
				for(del = 0; i < size[temp]; i++, del++){
					
					contains[pos[to]][size[pos[to]]++] = contains[temp][i];
					pos[contains[temp][i]] = to;
				}

				size[temp]-=del;
			}

			
		}
				
	}
	
	return 0;
	
}

chenhuansheng
New poster
Posts: 4
Joined: Fri Nov 04, 2005 5:12 pm

Post by chenhuansheng »

I thind now the code is OK .

#include <stdio.h>
#include <string.h>
#define MAXN 25

long contains[MAXN][MAXN], pos[MAXN], size[MAXN];

void setup(){
int i,j;

for(i = 0; i < MAXN; i++)
for(j = 0; j < MAXN; j++)
contains[j] = 0;

for(i = 0; i < MAXN; i++){
pos = i;
size = 1;
contains[0] = i;
}
}
int main(){
long i,j,from,to,N,temp,del;
char com[MAXN];

setup();

scanf("%d", &N);

while(1){

scanf("%s", com);

if(strcmp(com,"quit") == 0){
for(i = 0; i < N; i++){
printf("%d:", i);
for(j = 0; j < size; j++)
printf(" %d", contains[j]);

printf("\n");
}
break;

}


if(strcmp(com, "move") == 0){

scanf("%d %s %d", &from, com, &to);

if(from < 0 || from >= N || to < 0 || to >= N || from == to || pos[from] == pos[to]);
else{

for(i = size[pos[from]]-1; i>=0 && contains[pos[from]] != from; i--){

size[pos[from]]--;
contains[contains[pos[from]]][size[contains[pos[from]]]++] = contains[pos[from]];
pos[contains[pos[from]][i]] = contains[pos[from]][i];
}
if(strcmp(com, "onto") == 0)

for(i = size[pos[to]]-1; i>=0 && contains[pos[to]][i] != to; i--){

size[pos[to]]--;
contains[contains[pos[to]][i]][size[contains[pos[to]][i]]++] = contains[pos[to]][i];
pos[contains[pos[to]][i]] = contains[pos[to]][i];
}

contains[pos[to]][size[pos[to]]++] = contains[pos[from]][--size[pos[from]]];
pos[from] = to;

}
}
else if(strcmp(com, "pile") == 0){

scanf("%d %s %d", &from, com, &to);

if(from < 0 || from >= N || to < 0 || to >= N || from == to || pos[from] == pos[to]);
else{

if(strcmp(com, "onto") == 0)

for(i = size[pos[to]]-1; i>=0 && contains[pos[to]][i] != to; i--){

size[pos[to]]--;
contains[contains[pos[to]][i]][size[contains[pos[to]][i]]++] = contains[pos[to]][i];
pos[contains[pos[to]][i]] = contains[pos[to]][i];
}

for(i = 0; contains[pos[from]][i] != from; i++);

temp = pos[from];

for(del = 0; i < size[temp]; i++, del++){

contains[pos[to]][size[pos[to]]++] = contains[temp][i];
pos[contains[temp][i]] = to;
}

size[temp]-=del;
}


}

}

return 0;

}

chenhuansheng
New poster
Posts: 4
Joined: Fri Nov 04, 2005 5:12 pm

This my code

Post by chenhuansheng »

/*
for the block problem
December 2th
1. input.
2. process all command.
3. output.
*/

#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 25
#define M "move"
#define ON "onto"
#define OV "over"
#define P "pile"
#define Q "quit"
int pp[MAXNUM][MAXNUM];
int n;

int find_block(int);
void return_block(int);
void move_block(int, int);
void move_pile(int, int);

int main()
{
int i, j;
int b1, b2;
char c1[5], c2[5];
scanf("%d", &n);

for (i=0; i<n; i++) /*init the sequence*/
{
pp[0] = 1;
pp[1] = i;
}

for (;;)
{
scanf("%s ", c1);
if (!strcmp(c1, Q)) /*quit*/
break;
scanf("%d %s %d", &b1, c2, &b2);
if (b1==b2 || find_block(b1)==find_block(b2)) /*illegal command*/
continue;
else if (!strcmp(c1, M) && !strcmp(c2, ON)) /*move onto*/
{
return_block(b1);
return_block(b2);
move_block(b1, b2);
}
else if (!strcmp(c1, M) && !strcmp(c2, OV)) /*move over*/
{
return_block(b1);
move_block(b1, b2);
}
else if (!strcmp(c1, P) && !strcmp(c2, ON)) /*pile onto*/
{
return_block(b2);
move_pile(b1, b2);
}
else /*pile over*/
{
move_pile(b1, b2);
}

}

for (i=0; i<n; i++)
{
printf("%d:", i);
for (j=1; j<=pp[0]; j++)
{
printf(" %d", pp[j]);
}
printf("\n");
}
return 0;
}

int find_block(int b) /*where is block b located*/
{
int i, j;
if (b >= n)
return -1;
for (i=0; i<n; i++)
for (j=1; j<=pp[0]; j++)
if (b == pp[j])
return i;
}

void return_block(int b)
{
int i, j;
int length; /*the length of the pile*/
int stack, temp;

stack = find_block(b);
length = pp[stack][0];
for (i=1; i<=length; i++) /*find the position of the block*/
if (b == pp[stack])
break;

for (j=i+1; j<=length; j++) /*return the blocks top of block b*/
{
temp = pp[stack][j];
pp[temp][0]++;
pp[temp][pp[temp][0]] = temp;

pp[stack][0]--;
}
}

void move_block(int a, int b)
{
int s1, s2;
s1 = find_block(a);
s2 = find_block(b);
pp[s1][0]--;
pp[s2][0]++;
pp[s2][pp[s2][0]] = a;
}

void move_pile(int a, int b)
{
int i, j;
int s1, s2;
int len1, len2;

s1 = find_block(a);
s2 = find_block(b);
len1 = pp[s1][0];
len2 = pp[s2][0];

for (i=1; i<=len1; i++) /*find the position of the block*/
if (a == pp[s1])
break;
for (j=i; j<=len1; j++) /*move pile a over b*/
{
len2++;
pp[s2][len2] =pp[s1][j];
}
pp[s1][0] = i-1;
pp[s2][0] = len2;
}

Dewr
New poster
Posts: 6
Joined: Sun Dec 04, 2005 4:08 pm

help, 101

Post by Dewr »

judgeserver said "Wrong answer". :cry:

Code: Select all

#include <stdio.h>

#define NONE 666
#define QUIT 777

char input[20];		//input
int *abuffer;		//buffer used in save(int xl, int y, int* buffer)
int *bbuffer;		//buffer used in save(int xl, int y, int* buffer)
int length;			//length of blockworld
int *blockworld;	//blockworld
int i, j;			//i and j
int command[4];		//save command state (illegal/move/pile,onto/over,va1,va2)
int foundx, foundy;	//findit(int n)'s result
int va1x, va1y, va2x, va2y;	//save position of vars
int va1xl, va2xl;	//save va?x*length

void initworld();	//init
void report();		//report
void parse();		//parse
void move();		//move a block
void pile();		//carry a pile
int strtonum(char * start);	// extract integer from string
//void scanfa();		//input utility
int findit(int n); //find position of n
void pick(int xl, int y, int* buffer);	//save array
void put(int xl, int* buffer);			//load and split

int main()
{
lalala:
	if(scanf("%d", &length) == EOF)
		return 0;

	initworld();


	while(1)
	{
#ifndef ONLINE_JUDGE
		_flushall();
#endif
		gets(input);
		
		parse();

		if(command[0] == QUIT)
			break;
		else if(command[0] == 1)
			move();
		else if(command[0] == 2)
			pile();
		else
		{
#ifndef ONLINE_JUDGE
			printf("equal? error!\n");
#endif
			continue;
		}

#ifndef ONLINE_JUDGE
		report();
#endif
	}

	report();

	delete[] blockworld;
	delete[] abuffer;
	delete[] bbuffer;

	goto lalala;
}

void initworld()
{
	blockworld = new int[length*length+1];
	for(i=0; i<length; i++)
	{
		for(j=1; j<=length; j++)
		{
			if(j==1)
				blockworld[i*length+j] = i;
			else
				blockworld[i*length+j] = NONE;
		}
	}
	abuffer = new int[length];
	bbuffer = new int[length];
}

void report()
{
	for(i=0; i<length; i++)
	{
		printf("%d:", i);
		for(j=1; j<=length; j++)
		{
			if(blockworld[i*length+j] != NONE)
				printf(" %d", blockworld[i*length+j]);
			else
				break;
		}
		printf("\n");
	}
}

void parse()
{
	if(input[0] == 'm')
		command[0] = 1;		//move
	else if(input[0] == 'p')
		command[0] = 2;		//pile
	else
	{
		command[0] = QUIT;	//quit
		return;
	}

	command[2] = strtonum(input+5);
	command[3] = strtonum(input+12);

	if(command[2] == command[3])
	{
		command[0] = 0;
		return;
	}

	if(!findit(command[2]))
	{
		command[0] = 0;
		return;
	}
	va1x = foundx;
	va1y = foundy;
	if(!findit(command[3]))
	{
		command[0] = 0;
		return;
	}
	va2x = foundx;
	va2y = foundy;

	if(va1x == foundx)
	{
		command[0] = 0;
		return;
	}

	va1xl = va1x*length;
	va2xl = va2x*length;

	i = 7;
	while(1)
	{
		if(input[i] == 'n')
		{
			command[1] = 0;	//onto
			break;
		}
		else if(input[i] == 'v')
		{
			command[1] = 1; //over
			break;
		}
		i++;
	}
}

void move()
{
	blockworld[va1xl+va1y] = NONE;
	if(blockworld[va1xl+va1y+1] != NONE)
	{
		pick(va1xl, va1y+1, abuffer);
		put(va1xl, abuffer);
	}
	if(blockworld[va2xl+va2y+1] == NONE)
		blockworld[va2xl+va2y+1] = command[2];
	else
	{
		if(!command[1])
		{
			pick(va2xl, va2y+1, bbuffer);
			blockworld[va2xl+va2y+1] = command[2];
			put(va2xl, bbuffer);
		}
		else
		{
			while(blockworld[va2xl+va2y] != NONE)
				va2y++;
			blockworld[va2xl+va2y] = command[2];
		}
	}
}

void pile()
{
	pick(va1xl, va1y, abuffer);
	
	if(blockworld[va2xl+va2y+1] == NONE)
		put(va2xl, abuffer);
	else
	{
		if(!command[1])
		{
			pick(va2xl, va2y+1, bbuffer);
			put(va2xl, abuffer);
			put(va2xl, bbuffer);
		}
		else
			put(va2xl, abuffer);
	}
}

int strtonum(char * start)
{
	if(*start == ' ')
		start = start+1;
	if(*(start+1) != ' ' && (unsigned char)*(start+1) != 0)
		return ((unsigned char)*start - 48)*10 + (unsigned char)*(start+1) - 48;
	else
		return (unsigned char)*start - 48;
}

/*
void scanfa()
{
	i = 0;
	input[i] = getch();
	putch(input[i]);
	do{
		i++;
		input[i] = getch();
		putch(input[i]);
	}while(input[i] != 13);
	input[i] = 0;
	putch(10);
}*/

int findit(int n)
{
	for(i=0; i<length; i++)
	{
		for(j=1; j<=length; j++)
		{
			if(blockworld[i*length+j] == NONE)
				break;
			else if(blockworld[i*length+j] == n)
			{
				foundx = i;
				foundy = j;
				return 1;
			}
		}
	}
	return 0;
}

void pick(int xl, int y, int* buffer)
{
	i = 0;
	for(j=y; j<=length; j++)
	{
		if(blockworld[xl+j] == NONE)
			break;
		else
		{
			buffer[i] = blockworld[xl+j];
			blockworld[xl+j] = NONE;
		}
		i++;
	}
	buffer[i] = NONE;
}

void put(int xl, int* buffer)
{
	i = 1;

	while(blockworld[xl+i] != NONE)
		i++;

	for(j=0; i<=length; i++)
	{
		if(buffer[j] == NONE)
			break;
		blockworld[xl+i] = buffer[j];
		j++;
	}
}

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: 101 RunTime Error, but it works with me.. Please, HELP

Post by Martin Macko »

Fadia Ismael wrote:

Code: Select all

		for (i=0; i<n; i++)
			for (j=0; j<n; j++){
				if (origin[i][j]==a){
					ai=i;
					aj=j;}
				if (origin[i][j]==b){
					bi=i;
					bj=j;}
			}
You are searching in uninitialised part of the origin array. That may cause you find such an origin[ai][aj]==a that there is no origin[ai][x]==-1 (where x>aj). Thus returnBlocks function will loop out of the array and cause runtime error.

Post Reply

Return to “Volume 1 (100-199)”