模拟题目,考验写代码的耐心了,代码有点长但是含金量不高,堆栈和键值对的应用
#include <stdio.h>
#include <map>
#include <stack>
#include <string.h>
using namespace std;
typedef int BLOCK_INDEX;
typedef int POSITION;
stack<BLOCK_INDEX> pos[30];
map<BLOCK_INDEX,POSITION> m;
char buffer[300];
void clear_data(int n)
{
int i;
m.clear();
for(i=0; i<n; i++)
{
while(!pos[i].empty())
pos[i].pop();
pos[i].push(i);
m[i] = i;
}
}
void move_onto(int a, int b, int n)
{
if(a==b || m[a]==m[b])
return;
if(a<0 || a>=n)
return;
if(b<0 || b>=n)
return;
int pos_a, pos_b;
pos_a = m[a];
while(1)
{
if(pos[pos_a].top() != a)
{
m[pos[pos_a].top()] = pos[pos_a].top();
pos[pos[pos_a].top()].push(pos[pos_a].top());
pos[pos_a].pop();
}
else
{
break;
}
}
pos_b = m[b];
while(1)
{
if(pos[pos_b].top() != b)
{
m[pos[pos_b].top()] = pos[pos_b].top();
pos[pos[pos_b].top()].push(pos[pos_b].top());
pos[pos_b].pop();
}
else
{
break;
}
}
m[a] = m[b];
pos[pos_a].pop();
pos[pos_b].push(a);
}
void move_over(int a, int b, int n)
{
if(a==b || m[a]==m[b])
return;
if(a<0 || a>=n)
return;
if(b<0 || b>=n)
return;
int pos_a, pos_b;
pos_a = m[a];
while(1)
{
if(pos[pos_a].top() != a)
{
m[pos[pos_a].top()] = pos[pos_a].top();
pos[pos[pos_a].top()].push(pos[pos_a].top());
pos[pos_a].pop();
}
else
{
break;
}
}
pos_b = m[b];
m[a] = m[b];
pos[pos_a].pop();
pos[pos_b].push(a);
}
void pile_onto(int a, int b, int n)
{
if(a==b || m[a]==m[b])
return;
if(a<0 || a>=n)
return;
if(b<0 || b>=n)
return;
int pos_a, pos_b;
pos_b = m[b];
while(1)
{
if(pos[pos_b].top() != b)
{
m[pos[pos_b].top()] = pos[pos_b].top();
pos[pos[pos_b].top()].push(pos[pos_b].top());
pos[pos_b].pop();
}
else
{
break;
}
}
stack<BLOCK_INDEX> temp;
pos_a = m[a];
while(!pos[pos_a].empty())
{
temp.push(pos[pos_a].top());
if(pos[pos_a].top() == a)
{
pos[pos_a].pop();
break;
}
else
{
pos[pos_a].pop();
}
}
while(!temp.empty())
{
pos[pos_b].push(temp.top());
m[temp.top()] = pos_b;
temp.pop();
}
}
void pile_over(int a, int b, int n)
{
if(a==b || m[a]==m[b])
return;
if(a<0 || a>=n)
return;
if(b<0 || b>=n)
return;
int pos_a, pos_b;
pos_a = m[a];
pos_b = m[b];
stack<BLOCK_INDEX> temp;
while(!pos[pos_a].empty())
{
temp.push(pos[pos_a].top());
if(pos[pos_a].top() == a)
{
pos[pos_a].pop();
break;
}
else
{
pos[pos_a].pop();
}
}
while(!temp.empty())
{
pos[pos_b].push(temp.top());
m[temp.top()] = pos_b;
temp.pop();
}
}
void print_result(int n)
{
int i;
stack<BLOCK_INDEX> temp;
for(i=0; i<n; i++)
{
printf("%d:", i);
while(!temp.empty())
temp.pop();
while(!pos[i].empty())
{
temp.push(pos[i].top());
pos[i].pop();
}
while(!temp.empty())
{
printf(" %d", temp.top());
temp.pop();
}
printf("\n");
}
}
int main(void)
{
int n, i;
int a, b;
char t;
while(scanf("%d",&n) != EOF)
{
fscanf(stdin, "%c", &t);
clear_data(n);
while(1)
{
gets(buffer);
if(!strcmp(buffer,"quit"))
break;
if(strstr(buffer,"move") && strstr(buffer,"onto"))
{
sscanf(buffer, "move %d onto %d", &a, &b);
move_onto(a, b, n);
continue;
}
if(strstr(buffer,"move") && strstr(buffer,"over"))
{
sscanf(buffer, "move %d over %d", &a, &b);
move_over(a, b, n);
continue;
}
if(strstr(buffer,"pile") && strstr(buffer,"onto"))
{
sscanf(buffer, "pile %d onto %d", &a, &b);
pile_onto(a, b, n);
continue;
}
if(strstr(buffer,"pile") && strstr(buffer,"over"))
{
sscanf(buffer, "pile %d over %d", &a, &b);
pile_over(a, b, n);
continue;
}
}
print_result(n);
}
return 0;
}