作者 陈越
单位 浙江大学
一种自动包装机的结构如图 1 所示。首先机器中有 N 条轨道,放置了一些物品。轨道下面有一个筐。当某条轨道的按钮被按下时,活塞向左推动,将轨道尽头的一件物品推落筐中。当 0 号按钮被按下时,机械手将抓取筐顶部的一件物品,放到流水线上。图 2 显示了顺序按下按钮 3、2、3、0、1、2、0 后包装机的状态。
图1 自动包装机的结构
图 2 顺序按下按钮 3、2、3、0、1、2、0 后包装机的状态
一种特殊情况是,因为筐的容量是有限的,当筐已经满了,但仍然有某条轨道的按钮被按下时,系统应强制启动 0 号键,先从筐里抓出一件物品,再将对应轨道的物品推落。此外,如果轨道已经空了,再按对应的按钮不会发生任何事;同样的,如果筐是空的,按 0 号按钮也不会发生任何事。
现给定一系列按钮操作,请你依次列出流水线上的物品。
输入格式:
输入第一行给出 3 个正整数 N(≤100)、M(≤1000)和 Smax(≤100),分别为轨道的条数(于是轨道从 1 到 N 编号)、每条轨道初始放置的物品数量、以及筐的最大容量。随后 N 行,每行给出 M 个英文大写字母,表示每条轨道的初始物品摆放。
最后一行给出一系列数字,顺序对应被按下的按钮编号,直到 −1 标志输入结束,这个数字不要处理。数字间以空格分隔。题目保证至少会取出一件物品放在流水线上。
输出格式:
在一行中顺序输出流水线上的物品,不得有任何空格。
输入样例:
3 4 4
GPLT
PATA
OMSA
3 2 3 0 1 2 0 2 2 0 -1
输出样例:
MATA
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<queue>
using namespace std;
//遍历
void print(vector<queue<char>>v)
{
for (vector<queue<char>>::iterator it = v.begin(); it != v.end(); it++)
{
while (!it->empty())
{
cout << it->front() << " ";
it->pop();
}
cout << endl;
}
}
//1.n条轨道的创建
vector<queue<char>> createrail(int n)
{
vector<queue<char>>v(n);
return v;
}
//2.推入栈中 夹出栈的操作
void operation(int n, int m, int max)
{
vector<queue<char>>v(n);
string outcome;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
char a;
cin >> a;
v[i].push(a);
}
}
int max_sta=0;
int i = 1;
stack<char> store;
while (i)
{
//输入操作数
int opera; cin >> opera;
if (opera == -1)//到-1停止
break;
else
{
//1.0和其他 2.栈满 3.队列为空 4.栈空
if (opera == 0)
{
if (!store.empty())
{
outcome += store.top();
store.pop();
}
}
else
{
if (!v[opera - 1].empty())
{
if (store.size() == max)
{
outcome += store.top();
store.pop();
}
store.push(v[opera - 1].front());
v[opera - 1].pop();
}
}
}
}
cout << outcome << endl;
}
int main()
{
int N, M; cin >> N >> M;
int MAX; cin >> MAX;
operation(N, M, MAX);
return 0;
}