您的位置:首页 > 其它

AOJ 0033 Ball (dfs)

2015-02-21 22:20 344 查看
题意:

有一个形似央视大楼(Orz)的筒,从A口可以放球,放进去的球可通过挡板DE使其掉进B裤管或C裤管里,现有带1-10标号的球按给定顺序从A口放入,问是否有一种控制挡板的策略可以使B裤管和C裤管中的球从下往上标号递增。 

输入:

第一行输入数据组数N。接下来N行为N组具体数据,每组数据中有10个整数,代表球的放入顺序。 

输出:

对于每组数据,若策略存在,输出YES;若不存在,输出NO 

翻译来自http://blog.csdn.net/synapse7/article/details/14454885

dfs(k,b,c) k为深度 b为B最上面的球的数字大小 c为C最上面球的数字大小

AC代码如下:

//
// AOJ 0033 Ball
//
// Created by TaoSama on 2015-02-19
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int a[15]; //本题还可以使用最长递减子序列长度>2来判断 自行证明
bool dfs(int k, int b, int c) {
if(k == 11) return true;
if(a[k] > b) if(dfs(k + 1, a[k], c)) return true;
if(a[k] > c) if(dfs(k + 1, b, a[k])) return true;
return false;
}

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);

int t; cin >> t;
while(t--) {
for(int i = 1; i <= 10; ++i) cin >> a[i];
if(dfs(1, 0, 0)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs