Rails UVA - 514 (棧+模擬 難度3)
阿新 • • 發佈:2019-01-14
題目連結
一道模擬+棧的題目
直接遍歷判斷就好了, 不必全部預處理.
對於每節車廂, 有三種操作:
- 由A直接駛向B
- 由A先駛向C
- 由C直接駛向B
注意這道題的輸入有些獨特, 稍加判斷
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const LL maxn = 1010;
int n, m, B[maxn];
int main()
{
while(cin >> n && n){
ms(B, 0);
while(cin >> m && m){
if(m != 0) B[1] = m;
else break;
for(int i = 2; i <= n; i++)
cin >> B[i];
int A = 1, i = 1;
stack<int> C;
bool flag = true;
while(i <= n){
if(A == B[i]) //情況1: 直接A->B
A++, i++;
else if(!C.empty() && C.top()==B[i]) //情況2: C->B
i++, C.pop();
else if(A <= n) //情況3: A->C
C.push(A++);
else {
flag = false;
break;
}
}
if(flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
cout << endl;
}
return 0;
}