Iam unable to minimize this code........this is as far as i can go to reduce the complexity of the problem....could u guys please tell me a way in which i can reduce the Time limit.....
Code: Select all
#include<iostream>
#include<string.h>
using namespace std;
class Block
{
public:
int value;
int next;
int prev;
Block()
{
value = 0;
next = -1;
prev = -1;
}
Block(int val)
{
value = val;
next = -1;
prev = -1;
}
};
class Operation
{
public:
static Block block[25];
int temp,t,temp1;
Operation()
{
}
static void MakeBlocks()
{
for(int i=0;i<25;i++)
{
block[i].value = i;
}
}
int check(int a,int b)
{
for(t=block[a].value;t != -1;t=block[t].next)
{
if(t==b)
return 0;
}
for(t=block[a].value;t != -1;t=block[t].prev)
{
if(t==b)
return 0;
}
return 1;
}
void Moveofx(int a)
{
temp = block[a].next;
while(temp != -1)
{
temp1 = block[temp].next;
block[temp].next = -1;
block[temp].prev = -1;
temp = temp1;
}
}
void MoveToEnd(int a)
{
temp = block[a].value;
while(block[temp].next != -1)
{
temp = block[temp].next;
}
}
void MoveOnto(int a,int b)
{
int temp3,temp4;
Moveofx(a);
Moveofx(b);
temp = block[a].prev;
if(temp != -1)
block[temp].next = -1;
block[a].next = -1;
block[a].prev = b;
block[b].next = a;
}
void MoveOver(int a,int b)
{
Moveofx(a);
temp = block[a].prev;
if(temp != -1)
block[temp].next = -1;
block[a].next = -1;
MoveToEnd(b);
block[a].prev = block[temp].value;
block[temp].next = block[a].value;
}
void PileOnto(int a,int b)
{
Moveofx(b);
temp = block[a].prev;
if(temp != -1)
block[temp].next = -1;
block[a].prev = b;
block[b].next = a;
}
void PileOver(int a,int b)
{
MoveToEnd(b);
temp1 = block[a].prev;
if(temp1 != -1)
block[temp1].next = -1;
block[a].prev = b;
block[temp].next = a;
}
void Display(int n)
{
for(int i=0;i<n;i++)
{
temp = i;
if(block[temp].prev != -1)
{
cout<<temp<<": "<<endl;
}
else
if((block[temp].next == -1 ))
{
cout<<temp<<": "<<block[temp].value<<endl;
}
else
{
cout<<temp<<":";
for(;temp !=-1;temp = block[temp].next)
{
cout<<" "<<block[temp].value;
}
cout<<endl;
}
}
}
};
Block Operation::block[25];
int main()
{
int n;
char command[10],oper[10];
int a,b,i;
Operation op = Operation();
Operation::MakeBlocks();
while(cin>>n)
{
for(;;)
{
cin>>command;
if(strcmp(command,"quit")==0)
{
op.Display(n);
break;
}
cin>>a;
cin>>oper;
cin>>b;
if(op.check(a,b) == 0)
continue;
if(strcmp(command,"move")==0)
{
if(strcmp(oper,"onto") ==0)
{
op.MoveOnto(a,b);
}
else
{
op.MoveOver(a,b);
}
}
if(strcmp(command,"pile")==0)
{
if(strcmp(oper,"onto") ==0)
{
op.PileOnto(a,b);
}
else
{
op.PileOver(a,b);
}
}
}
}
}