做完这道就七十五个了,四分之三了,今天已经正月初十了,本来还有四五天我就要离开我亲爱的故乡了,但是由于疫情严重,不知何时才能回去我那个倾注了感情的数院,还剩二十五个题,也一点不着急了。。。。
题意翻译
初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作:
move a onto b: 把a和b上方的木块全部放回初始的位置,然后把a放到b上面
move a over b: 把a上方的木块全部放回初始的位置,然后把a放在b所在木块堆的最上方
pile a onto b: 把b上方的木块部放回初始的位置,然后把a和a上面所有的木块整体放到b上面
pile a over b: 把a和a上面所有的木块整体放在b所在木块堆的最上方
一组数据的结束标志为"quit",如果有非法指令(a和b在同一堆),应当忽略。
输出:所有操作输入完毕后,从左到右,从下到上输出每个位置的木块编号。 感谢U27114 jxdql2001 提供的翻译
输入输出样例
输入 #1复制
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
输出 #1复制
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
经过这个题我还发现点事,index事UVa评测机器环境的敏感变量
//
// Created by DELL on 2020/2/3.
// 2020年2月3日19:41:30
// 发现pile 操作都是将a和a上面的木块 移动到b的上面 (且不管b上面的木块是否放回原处)
//
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#define Maxn 25
using namespace std;
int num[Maxn],stack[Maxn][Maxn],Index[Maxn];// Index 记录每个数字现在在哪里
inline void Move(int a) {
int Ind = Index[a]; // a有可能不在本来的地方
while(stack[Ind][num[Ind]] != a) {
int tmp = stack[Ind][num[Ind]];
Index[tmp] = tmp; num[Ind]--;
stack[tmp][++num[tmp]] = tmp;
}
}
inline void Atab(int a,int b) {
int temp[Maxn],i;
for(i=0; num[Index[a]] >= 0; i++) {
temp[i] = stack[Index[a]][num[Index[a]]--];
Index[temp[i]] = Index[b];
}
i--;
while(i >= 0)
stack[Index[b]][++num[Index[b]]] = temp[i--];
}
int main(int argc,char* argv[]) {
int n,a,b; string s1,s2;
while(scanf("%d",&n) == 1) {
for(int i=0; i<n; i++) {
num[i] = 0;
stack[i][0] = i;
Index[i] = i;
}
while(cin >> s1 && s1 != "quit") {
scanf("%d",&a); cin >> s2; scanf("%d",&b);
if(Index[a] == Index[b]) continue;// 非法操作请忽略
if(s1 == "move") Move(a);
if(s2 == "onto") Move(b);
Atab(a,b);
}
for(int i=0; i<n; i++) {
int k = 0;
printf("%d:",i);
while(k <= num[i]) printf(" %d",stack[i][k++]);
printf("\n");
}
}
return 0;
}