Posted: Sat Dec 16, 2006 12:28 pm
Don't print unnecessary space and please remove your code..
Code: Select all
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class bb{
private:
int box[25][25];
int box_in[25];
public:
int use_box;
void re_set();
void re_put(int index);
void put(int fb,int tb);
void put_all(int fb,int tb);
void show();
bool check(int fb,int tb);
};
bb sbox;
int main(){
string da,db;
int ia,ib;
while(cin>>ia){
sbox.use_box=ia;
sbox.re_set();
re_load:
cin>>da;
if(da=="quit"){
sbox.show();
//system("pause");
//return 0;
}else{
cin>>ia>>db>>ib;
if(ia!=ib&&sbox.check(ia,ib)){
if(da=="move"){
if(db=="onto"){
sbox.re_put(ia);
sbox.re_put(ib);
sbox.put(ia,ib);
}
if(db=="over"){
sbox.re_put(ia);
sbox.put(ia,ib);
}
}
if(da=="pile"){
if(db=="onto"){
sbox.re_put(ib);
sbox.put_all(ia,ib);
}
if(db=="over"){
sbox.put_all(ia,ib);
}
}
}
goto re_load;
}
}
return 0;
}
void bb::re_set(){
int ax,ay;
for(ax=0;ax<25;ax++)
for(ay=0;ay<25;ay++){
box[ax][ay]=ax;
box_in[ax]=1;
}
}
void bb::re_put(int index){
int ax,ay,ac,ad,ae;
for(ax=0;ax<use_box;ax++)
for(ay=0;ay<box_in[ax];ay++)
if(box[ax][ay]==index){
ad=box_in[ax];
for(ac=ay+1;ac<ad;ac++){
box_in[ax]--;
ae=box[ax][ac];
box[ae][box_in[ae]]=ae;
box_in[ae]++;
}
return;
}
}
void bb::put(int fb,int tb){
int ax,ay,ac,ad;
for(ax=0;ax<use_box;ax++)
for(ay=0;ay<box_in[ax];ay++)
if(box[ax][ay]==fb){
box_in[ax]--;
for(ac=ay;ac<use_box;ac++){
box[ax][ac]=box[ax][ac+1];
}
for(ax=0;ax<use_box;ax++){
ad=box_in[ax];
for(ay=0;ay<ad;ay++)
if(box[ax][ay]==tb){
box[ax][box_in[ax]]=fb;
box_in[ax]++;
}
}
return;
}
}
void bb::put_all(int fb,int tb){
int ax,ay,ac,ad,ae;
for(ax=0;ax<use_box;ax++)
for(ay=0;ay<box_in[ax];ay++)
if(box[ax][ay]==fb)
for(ac=0;ac<use_box;ac++)
for(ad=0;ad<box_in[ac];ad++)
if(box[ac][ad]==tb){
ae=box_in[ax];
for(ad=ay;ad<ae;ad++){
box[ac][box_in[ac]]=box[ax][ad];
box_in[ax]--;
box_in[ac]++;
}
return;
}
}
void bb::show(){
int ax,ay;
for(ax=0;ax<use_box;ax++){
cout<<ax<<":";
for(ay=0;ay<box_in[ax];ay++){
cout<<" "<<box[ax][ay];
}
cout<<endl;
}
}
bool bb::check(int fb,int tb){
int ax,ay;
for(ax=0;ax<use_box;ax++)
for(ay=0;ay<box_in[ax];ay++)
if(box[ax][ay]==fb){
for(ay=0;ay<box_in[ax];ay++)
if(box[ax][ay]==tb)
return false;
return true;
}
}
Code: Select all
If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. Don't put any trailing spaces on a line.
Code: Select all
15
move 10 onto 1
move 8 over 1
move 2 over 9
move 6 over 10
pile 8 over 6
pile 8 onto 5
pile 1 onto 5
quit
move 12 over 1
move 14 onto 9
quit
Code: Select all
0: 0
1:<==no space here
2:
3: 3<==no space after last number
4: 4
5: 5 1 10
6: 6
7: 7
8: 8
9: 9 2
10:
11: 11
12: 12
13: 13
14: 14
<===cursor is here>
here is my out put part:dddolphin wrote:the above processing is correct.
but still encounter presentation error
Code: Select all
for(i=0; i<n; i++)
{
printf("%d:", i);
for(j=0; j<top[i]; j++)
printf(" %d", bl[i][j]);
puts("");
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
void initial(void);
char **init;
long high;
int i;
void popline(int);
void pushline(int);
void moveonto(int,int);
void moveover(int,int);
void pileonto(int,int);
void pileover(int,int);
void display(void);
char popli[25];
void pop(int);
void push(int);
char poptemp;
int now[25];
main()
{
initial();
while(1)
{
int input1,input2;
char in[5],in2[5];
scanf("%s",in);
if(!strcmp(in,"quit"))
break;
scanf(" %d",&input1);
scanf(" %s",in2);
scanf(" %d",&input2);
if(input1>high-1||input2>high-1)continue;
if(!strcmp(in,"move"))
if(!strcmp(in2,"onto"))
moveonto(input1,input2);
else if(!strcmp(in2,"over"))
moveover(input1,input2);
else continue;
else if(!strcmp(in,"pile"))
if(!strcmp(in2,"onto"))
pileonto(input1,input2);
else if(!strcmp(in2,"over"))
pileover(input1,input2);
else continue;
else continue;
}
display();
for(i=0;i<=high-1;i++)
free(init[i]);
free(init);
high=0;
}
void initial(void)
{
char *tpr;
while(high>24 || high<1)
scanf("%d",&high);
init=(char**)malloc(sizeof(char**)*high);
tpr=(char*)malloc(sizeof(char*)*high*high+50);
for(i=0;i<=high-1;i++)
{
init[i]=tpr+high*i;
//init[i]=(char*)malloc(sizeof(char*)*high);
}
for(i=0;i<=high-1;i++)
{
init[i][0]=i+'0';
init[i][1]='\0';
now[i]=i;
}
}
void pop(int x)
{
poptemp=init[x][(strlen(init[x])-1)];
init[x][strlen(init[x])-1]='\0';
}
void popline(int x)
{
int ser=0,put=0;
while(init[now[x]][ser]!=x+'0')
{
ser++;
}
strcpy(popli,&init[now[x]][ser]);
init[now[x]][ser]='\0';
/*while(popli[put]=init[now[x]][ser])
{
put++;ser++;
init[now[x]][ser-1]='\0';
if(popli[put-1]=='\0')
break;
}*/
}
void pushline(int x)
{
int len=strlen(popli);
strcat(init[now[x]],popli);
for(int put=0;put<len;put++)
now[popli[put]-'0']=x;
}
void push(int x)
{
if(!poptemp)return;
int len=strlen(init[x]);
init[x][len+1]='\0';
init[x][len]=poptemp;
now[poptemp-'0']=x;
}
void moveonto(int x,int y)
{
int len;
if(x==y)return ;
if(now[x]==now[y])return;
len=strlen(init[now[y]])-1;
while(y!=init[now[y]][len]-'0')
{
pop(now[y]);
len--;
push(poptemp-'0');
}
pop(now[x]);
while(x!=poptemp-'0')
{
push(poptemp-'0');
if(now[x]==(poptemp-'0'))return;
pop(now[x]);
}
push(now[y]);
}
void moveover(int x,int y)
{
if(x==y)return ;
if(now[x]==now[y])return;
pop(now[x]);
while(x!=poptemp-'0')
{
push(poptemp-'0');
if(now[x]==(poptemp-'0'))return;
pop(now[x]);
}
push(now[y]);
}
void pileonto(int x,int y)
{
int len;
if(x==y)return;
if(now[x]==now[y])return;
len=strlen(init[now[y]])-1;
while(y!=init[now[y]][len]-'0')
{
pop(now[y]);
len--;
push(poptemp-'0');
}
popline(x);
pushline(now[y]);
}
void pileover(int x,int y)
{
if(now[x]==now[y])return;
if(x==y)return;
popline(x);
pushline(now[y]);
}
void display(void)
{
char k=0;
for(i=0;i<=high-1;i++)
{
printf("%d: ",i);
k=0;
while(init[i][k]!='\0')
{
printf("%d ",init[i][k]-'0');
k++;
}
printf("\n");
}
}
Code: Select all
15
move 10 onto 1
move 8 over 1
move 7 over 9
move 6 over 10
pile 14 over 6
pile 8 onto 5
pile 1 onto 6
move 12 over 1
move 14 onto 9
quit
Code: Select all
0: 0
1:
2: 2
3: 3
4: 4
5: 5 8 6 1 10 12
6:
7: 7
8:
9: 9 14
10:
11: 11
12:
13: 13
14: