http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22516

一道需要思考的搜索题。

题意:十个球按给定顺序从图中所示容器中下落,然后挡板可以让球落在左边或者右边,问给定球的顺序,是否存在两边从低到高都是递增的情况。

解1:只要给定的球能够分成两个递增的序列,那么一定是满足条件的,用dfs可以找出以第一个球为首的递增序列,把其标记,然后判断剩下的球是不是也构成一个递增序列。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 110
using namespace std; int arr[],used[]; void dfs(int pre,int cur)
{
if(cur<&&arr[pre]<arr[cur])
{
used[cur]=;
dfs(cur,cur+);
}
else if(cur<&&arr[pre]>=arr[cur])
dfs(pre,cur+);
}
int main()
{
//freopen("a.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
bool judge=false;
for(int i=;i<;i++)
scanf("%d",&arr[i]);
for(int i=;i<;i++)
{
memset(used,,sizeof(used));
used[i]=;
dfs(i,i+);
int pre=;
judge=false;
for(int j=;j<;j++)
{
if(!used[j])
{
if(pre>=arr[j])
{
judge=true;
break;
}
pre=arr[j];
}
}
if(!judge)
{
printf("YES\n");
break;
}
}
if(judge) printf("NO\n");
}
return ;
}

解2:当然可以采用二进制枚举(第一次知道这个算法),每个球要么落在左边要么在右边,10个球总共2的10次方=1024种状态。0-2013每一个数都代表一种状态。不断枚举直到找到合适的为止。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 110
using namespace std; int main()
{
//freopen("a.txt","r",stdin);
int t;
int arr[],l[],r[];
scanf("%d",&t);
while(t--)
{
int i,j,cnt;
bool flag;
for(i=;i<;i++) scanf("%d",&arr[i]);
for(i=;i<;i++)
{
int x=,y=;
for(cnt=,j=i;cnt<;cnt++,j>>=)
{
if(j&) l[x++]=arr[cnt];
else r[y++]=arr[cnt];
}
flag=true;
for(j=;j<x;j++)
{
if(l[j]<l[j-]) {flag=false;break;}
}
if(flag)
{
for(j=;j<y;j++)
{
if(r[j]<r[j-]) {flag=false;break;}
}
}
if(flag) {printf("YES\n");break;}
}
if(!flag) printf("NO\n");
}
return ;
}

AOJ -0033 Ball(DFS)的更多相关文章

  1. AOJ 0033 Ball【DFS】

    有一个筒,从A口可以放球,放进去的球可通过挡板DE使其掉进B管或C管里,现有带1-10标号的球按给定顺序从A口放入,问是否有一种控制挡板的策略可以使B管和C管中的球从下往上标号递增. 输入: 第一行输 ...

  2. aoj 0033 Ball【dfs/枚举】

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

  3. Aizu 0033 Ball(dfs,贪心)

    日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...

  4. Aizu/Aoj 0033 Ball

    题目大意: 有编号1到10共10个球,从上方丢下去,入口处可以选择进入左边或者右边,最后10个球全部落下去后如果左右两侧都是从小到大的顺序,则输出YES:否则输出NO. 题目原本的标签枚举,复杂度是2 ...

  5. AOJ 0033 深度优先搜索

    题意:按给定顺序从A口放标号位1-10的10个球,利用挡板可以使球落到B或C,问能否使B和C里的球标号从下往上递增. 分析:对于第 i 个球,若a[i]大于B口上方的球,则可放入B口:若a[i]大于C ...

  6. aoj 0033 玉

    図のように二股に分かれている容器があります.1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A から落とし.左の筒 B か右の筒 C に玉を入れます.板 D は支点 E を中心に左右 ...

  7. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  8. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  9. HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 2019最新Android面试题

    原文链接:https://blog.csdn.net/wen_haha/article/details/88362469版权声明:本文为博主原创文章,转载请附上博文链接! 前言 金三银四到来了,找工作 ...

  2. iOS 中集成百度echarts3.0

    突然项目中要用到图表,所以就用了百度的echarts,然后就是网上搜了一下,由于本人的JS不是很熟悉,但是研究了一下还是做出来了,其实也不是很难 最后做的效果大概如下图这种,由于界面上没调整,所以粗糙 ...

  3. swiper4初始化/swiper-init/data-swiper

    用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  4. php debug/phpstorm调试

    apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...

  5. CCF|游戏|Java

    import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...

  6. Farseer.net轻量级开源框架 中级篇:BasePage、BaseController、BaseHandler、BaseMasterPage、BaseControls基类使用

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: UrlRewriter 地址重写 下一篇:Farseer.net轻量级开源框架 中 ...

  7. ubunut在线音乐比方软件

    今天安装了一个音乐在线播放软件,忍不住要来赞一下, 之前一直都是用网页在线的qq音乐听的,这样就有点感觉不爽了, 今天突然想起来好像在网上看到的在ubuntu下有用网易云音乐的,就上网看了一下 还真的 ...

  8. Bash Template

    #/bin/bash #set -x set -e usage() { cat <<EOF Usage: `basename $` [OPTIONS] <non-option arg ...

  9. Swift protocol extension method is called instead of method implemented in subclass

    Swift protocol extension method is called instead of method implemented in subclass protocol MyProto ...

  10. zend studio汉化离线语言包安装方法