101 - The Blocks Problem
Moderator: Board moderators
Help on Problem 101
Initial state of blocks are:
0:
1:
2:
3:
4:
5:
6:
7:
8: 0 1 2 3
9: 4 5 6 7
From this state what would be state for each of the following commads?
move 1 onto 5
move 1 over 5
pile 1 onto 5
pile 1 over 5
Your help would be appreciated. Thanks in advance.
0:
1:
2:
3:
4:
5:
6:
7:
8: 0 1 2 3
9: 4 5 6 7
From this state what would be state for each of the following commads?
move 1 onto 5
move 1 over 5
pile 1 onto 5
pile 1 over 5
Your help would be appreciated. Thanks in advance.
101:WA[java]
Code: Select all
import java.io.*;
import java.util.*;
class Main
{
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args)
{
String input="";
String action="";
String prop="";
StringTokenizer idata;
int blockNumber,iLoop,jLoop;
int temp;
int i=-1;
int j=-1;
int a=-1;
int b=-1;
int ai=-1;
int aj=-1;
int bi=-1;
int bj=-1;
input=ReadLn(255);
idata=new StringTokenizer(input);
blockNumber = Integer.parseInt (idata.nextToken());
int [][] matrix=new int[blockNumber][blockNumber];
for(jLoop=0;jLoop<blockNumber;jLoop++)
matrix[0][jLoop]=jLoop;
for(jLoop=0;jLoop<blockNumber;jLoop++)
for(iLoop=1;iLoop<blockNumber;iLoop++)
matrix[iLoop][jLoop]=-3;
while ((input = Main.ReadLn (255)) != null)
{
idata=new StringTokenizer(input);
action=idata.nextToken();
if (action.equals("quit"))
{
break;
}
a = Integer.parseInt (idata.nextToken());
prop=idata.nextToken();
b = Integer.parseInt (idata.nextToken());
if (a==b)
{
continue;
}
for(iLoop=0;iLoop<blockNumber;iLoop++)
for(jLoop=0;jLoop<blockNumber;jLoop++)
{
if(matrix[iLoop][jLoop]==a) i=jLoop;
if(matrix[iLoop][jLoop]==b) j=jLoop;
}
if(i==j)
{
continue;
}
if (action.equals("move"))
{
if(prop.equals("onto"))
{
for(jLoop=0;jLoop<blockNumber;jLoop++)
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==a)
{
ai=iLoop;
aj=jLoop;
}
if(matrix[iLoop][jLoop]==b)
{
bi=iLoop;
bj=jLoop;
}
}
for(i=ai+1;matrix[i][aj]!=-3;i++)
{
temp=i;
for(i=0;matrix[i][(matrix[temp][aj])]!=-3;i++){}
matrix[i][(matrix[temp][aj])]=matrix[temp][aj];
matrix[temp][aj]=-3;
i=temp;
}
matrix[bi+1][bj]=matrix[ai][aj];
matrix[ai][aj]=-3;
}
if(prop.equals("over"))
{
for(jLoop=0;jLoop<blockNumber;jLoop++)
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==a)
{
ai=iLoop;
aj=jLoop;
}
if(matrix[iLoop][jLoop]==b)
{
bj=jLoop;
temp=iLoop;
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==-3)
{
bi=iLoop;
iLoop=temp;
break;
}
}
}
}
for(i=ai+1;matrix[i][aj]!=-3;i++)
{
temp=i;
for(i=0;matrix[i][(matrix[temp][aj])]!=-3;i++){}
matrix[i][(matrix[temp][aj])]=matrix[temp][aj];
matrix[temp][aj]=-3;
i=temp;
}
matrix[bi][bj]=matrix[ai][aj];
matrix[ai][aj]=-3;
}
}
if(action.equals("pile"))
{
if(prop.equals("onto"))
{
for(jLoop=0;jLoop<blockNumber;jLoop++)
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==a)
{
ai=iLoop;
aj=jLoop;
}
if(matrix[iLoop][jLoop]==b)
{
bi=iLoop;
bj=jLoop;
}
}
for(i=bi+1;matrix[i][bj]!=-3;i++)
{
temp=i;
for(i=0;matrix[i][(matrix[temp][bj])]!=-3;i++){}
matrix[i][(matrix[temp][bj])]=matrix[temp][bj];
matrix[temp][bj]=-3;
i=temp;
}
for(bi=bi+1;matrix[ai][aj]!=-3;ai++)
{
matrix[bi][bj]=matrix[ai][aj];
matrix[ai][aj]=-3;
bi=bi+1;
}
}
if(prop.equals("over"))
{
for(jLoop=0;jLoop<blockNumber;jLoop++)
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==a)
{
ai=iLoop;
aj=jLoop;
}
if(matrix[iLoop][jLoop]==b)
{
bj=jLoop;
temp=iLoop;
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==-3)
{
bi=iLoop;
iLoop=temp;
break;
}
}
}
}
for(;matrix[ai][aj]!=-3;ai++)
{
matrix[bi][bj]=matrix[ai][aj];
matrix[ai][aj]=-3;
bi=bi+1;
}
}
}
}
for(jLoop=0;jLoop<blockNumber;jLoop++)
{
System.out.print(jLoop+":");
for(iLoop=0;iLoop<blockNumber;iLoop++)
{
if(matrix[iLoop][jLoop]==-3) continue;
System.out.print(" "+matrix[iLoop][jLoop]);
}
System.out.println();
}
}
}
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
I got crazy about 101.....please help me
what's the result alfter "pile 4 onto 1":
0:0 1 2 3
1:
2:5
3:
4:4 6
5:
6:
I am not sure where 5,2 wll perform.....
0:0 1 2 3
1:
2:5
3:
4:4 6
5:
6:
I am not sure where 5,2 wll perform.....