Rails UVA - 514 (栈+模拟 难度3)
题目链接 一道模拟+栈的题目 直接遍历判断就好了,不必全部预处理. 对于每节车厢,有三种操作: 由A直接驶向B 由A先驶向C 由C直接驶向B 注意这道题的输入有些独特,稍加判断 #include #include #include #include #include #include #include #include #include #include #include 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 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |