# AOJ 0033 Ball "DFS"

Source: Internet
Author: User
Tags bitset

There is a tube, from a can play ball, put in the ball can be through the baffle de to fall into the B-tube or C-tube, the existing 1-10-marking ball in the given order from a mouth, ask whether there is a control of the baffle strategy can make B-tube and C-tube ball from the bottom to the upper marking increment.

Input:

The first row of input data group N. Next n behavior n sets of specific data, each set of data has 10 integers, which represent the order in which the ball is placed.

Output:

For each set of data, if the policy exists, the output is yes;

Solution 1:dfs

Idea: Each time to determine whether the current ball is larger than the top of the left container of the ball, if you can put, otherwise go to see the right. Once found on both sides can not put, it can only be judged to be no.

`#include <cstdio> #include <string> #include <iostream> #include <cstring> #include < Algorithm>using namespace Std;typedef long long ll;const int INF = 0x7fffffff;const int MAXN = 1e5 + 10; int a[11],vis[11],flag;void dfs (int x,int l,int r) {    int i;    if (x==11) return;     if (flag) return;    if (a[x]>l) DFS (x+1,a[x],r);    else if (a[x]>r) DFS (x+1,l,a[x]);    Else{flag=1;return;}} int main () {  int t,i,j,k;  scanf ("%d", &t);  while (t--)  {    flag=0;    for (i=1;i<=10;i++)      scanf ("%d", &a[i]);    memset (vis,0,sizeof (Vis));    DFS (1,0,0);    if (flag) printf ("no\n");    else printf ("yes\n");  } return 0;}`

Solution 2: Binary Enumeration

Thought: as 0,1 sequence

`#include <cstdio> #include <cstring>int a[15], l[15], r[15], T, I, J, LC, RC, Cnt;bool vis[15], Flag;bool solve () {for (i = 0; i < 1024x768; ++i) {memset (l, 0, sizeof (l)), memset (r, 0, sizeof (R)), LC = RC = 0;for (cnt = 0, j = i; CNT < 1 0; ++cnt, J >>= 1) {if (J & 1) l[lc++] = A[cnt];else r[rc++] = a[cnt];} Flag = true;for (j = 1; j < LC; ++j) if (L[j] < l[j-1]) {flag = False;break;} if (flag) for (j = 1; j < RC; ++j) if (r[j-1] > R[j]) {flag = False;break;} if (flag) return true; return false;} int main () {scanf ("%d", &t), while (t--) {memset (Vis, 0, sizeof (VIS)), and for (i = 0; i < ++i) scanf ("%d", &a[i]);p UTS (Solve ()? "YES": "NO");} return 0;}`

Bitset Enhanced Version

`#include <iostream> #include <bitset> #include <algorithm>using namespace std; int main () {int n;cin >> n;while (n--) {int ball[10];for (int i = 0; i < ++i) {cin >> ball[i];} bitset<  ;10> Direction;int all = 1024;while (all--> 0) {direction = static_cast<bitset<10> > (all); bool Perfect = True;int left = 0;int right = 0;for (int i = 0; i <, ++i) {if (Direction[i]) {if (Ball[i] > left) {left = Ball[i];} Else{perfect = False;break;}} Else{if (Ball[i] > right) {right = Ball[i];} Else{perfect = False;break;}}} if (perfect) {break;}} if (all >= 0) {cout << "YES" << Endl;} Else{cout << "NO" << Endl;}} return 0;}`

AOJ 0033 Ball "DFS"

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.