uva老不让过……
注意:
- move a onto b
where a and b areblock numbers, puts block a onto block b afterreturning any blocks that are stacked on top of blocks a and b to their initial positions.
to their initial positions.是指一开始的位置,即4号石块回到第4号栈。
Any command in which a = b orin which a and b are in the same stack ofblocks is an illegal command. All illegal commands should be ignored and shouldhave no affect on the configuration of blocks.
这个千万要小心,一不留神会造成程序runtime error,这种非法情况直接忽略。不忽略的话,小心堆成一个很高很高的栈……
效率情况:
Total Submissions | Users that tried it | Users that solved it |
65347 | 13675 | 8497 |
Your best accepted try | ||||||
Ranking | Submission | Run Time | Language | Submission Date | ||
490 | 9624560 | 0.008 | C++ | 2012-01-07 05:20:01 |
读题相当重要,比刷牙还重要。不把题目读得精透,千万不要急着下手……
#include<stdio.h>
const int maxn=30;
int s[maxn][maxn],top[maxn],pos[maxn],t[maxn],t_top,n;
void init()
{
for(int i=0;i<n;i++)
{
s[i][1]=i;
top[i]=1;
pos[i]=i;
}
}
void move(int p,int x)
{
while(s[p][top[p]]!=x)
{
int a=s[p][top[p]--];
s[a][++top[a]]=a;
pos[a]=a;
}
}
void print_ans()
{
for(int i=0;i<n;i++)
{
printf("%d:",i);
for(int j=1;j<=top[i];j++)
printf(" %d",s[i][j]);
printf("\n");
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("101.txt","r",stdin);
#endif
scanf("%d",&n);
init();
char s1[5],s2[5];
int a,b;
while(scanf("%s%d%s%d",s1,&a,s2,&b)==4)
{
if(a==b || pos[a]==pos[b]) continue;
if(s1[0]=='m')
{
move(pos[a],a);
top[pos[a]]--;
pos[a]=pos[b];
if(s2[1]=='n')
{
move(pos[b],b);
s[pos[b]][++top[pos[b]]]=a;
}
else s[pos[b]][++top[pos[b]]]=a;
}
else
{
t_top=0;
while(1)
{
t[++t_top]=s[pos[a]][top[pos[a]]--];
pos[t[t_top]]=pos[b];
if(t[t_top]==a) break;
}
if(s2[1]=='n')
move(pos[b],b);
while(t_top)
s[pos[b]][++top[pos[b]]]=t[t_top--];
}
//printf("%s %d %s %d\n",s1,a,s2,b);
//print_ans();
}
print_ans();
return 0;
}