題目鏈接:https://vjudge.net/problem/Aizu-0033
Description
如圖所示,容器中間有一樞軸,下方分為兩路。容器上方開口,從1到10連續編號的小球從容器開口A放入。通過調整樞軸E的方向,可以使小球經過D而落入左側B筒或者右側C筒。現給出從A放入小球的順序,請你判斷能否最終小球落入B和C時,號碼大的球總是位于號碼小的球的上側。如果可能則在一行中輸出”YES”,否則輸出 “NO”
Input
第一行一個整數N(N<1000)
接下來有N行,每行10個由空格分隔的[1,10]區間內的整數,分別代表10個小球放入A筒的順序
Output
對每組數據,輸出一行YES或NO
Sample Input
2
3 1 4 2 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
Sample Output
YES
NO
題解:
#include <iostream>
using namespace std;
int v[10];
int l, r;
bool dfs(int x)
{
if (x == 10)
return true;
if (v[x] > l)
{
l = v[x];
return dfs(x + 1);
}
else if (v[x] > r)
{
r = v[x];
return dfs(x + 1);
}
else
return false;
}
int main()
{
int n;
cin >> n;
while (n--)
{
l = r = 0;
for (int i = 0; i < 10; i++)
cin >> v[i];
if (dfs(0))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}