Search by Boshi Exam Summary

Source: Internet
Author: User
Tags hash printf stdin

Learn some advanced search algorithms, of course, the exam is unavoidable, so there is a nightmare of 4 hours ... Title Record

UVA11549
UVA11212
UVA11882 UVA11549

Test Instructions
Give you an n-digit number as the limit, K as the original number. Each operation squares the K and takes the previous n bits as the new K value.
Ask to do several operations, can get the maximum K value.
Ideas
For the subject, when to stop the operation is the key. Because of the n<=9, it is obvious that the direct use of BOOL will lead to the MLE, with the map will tle. However, I did not try to write a hash, I am too water, hash write not good ... Then you can use a magic algorithm-the turtle-Rabbit race algorithm, also called Floyd algorithm. Have not heard of, can learn to understand.
Its principle is still well understood. Make a fast pointer (fast), and a slow pointer (slow) from a position. Each time fast goes to the next node of the next node, slow goes to the next node, if there is a ring, then the loop, until Fast=slow, at this time fast must have traversed the entire ring, both to ensure that the entire loop can be traversed, but also to get the termination condition.
Code

#include <algorithm> #include <iostream> #include <cstdio> using namespace std;
typedef unsigned long long ll;
    Template <typename tp> void Read (Tp &x) {x=0;
    Char Ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') Ch=getchar ();
while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
} ll z,n,k,fast,slow,maxx,limit[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
    LL Calc (ll n,ll num) {num*=num;
    while (Num>=limit[n]) num/=10;
return num;
    } int main () {freopen ("calc.in", "R", stdin);
    Freopen ("Calc.out", "w", stdout);
    Read (z);
        while (z--) {read (n);
        Read (k);
        Maxx=k;
        Fast=slow=k;
            do{Fast=calc (n,fast);
            Maxx=max (Fast,maxx);
            Fast=calc (N,fast);
            Maxx=max (Fast,maxx);
        Slow=calc (N,slow);
        }while (Fast!=slow);
    printf ("%llu\n", Maxx);
} return 0; }
UVA11212

Test Instructions
To give you a sequence of numbers, each operation can insert a continuous sequence of columns into any position, asking the minimum number of operations required to make this sequence of columns into order.
Ideas
The sequence of the transform is [i,j], which is inserted into the rear position of K.
As a result of n<=9, then 5 steps will be able to solve, in addition to each operation can change the successor of 3 number, namely I-1,j,k, so we have two very important pruning strategy. Then the rest is pure simulation.
Code

#include <iostream> #include <cstring> #include <cstdio> using namespace std;
int n,depth,a[16];
BOOL Found,flag;
    Template <typename tp> void Read (Tp &x) {x=0;
    Char Ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') Ch=getchar ();
while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
    } bool Check () {for (int j=1;j<=n;j++) if (a[j]!=j) return false;
return true;
    } int h () {int ans=0;
    for (int j=0;j<=n;j++) if (a[j]+1!=a[j+1]) ans++;
return ans;
    } void Dfs (int d) {if (check ())//Every time you want to detect found=true; if (found| |
    (d-1) *3+h () >depth*3) return;
    int i,j,k,p,tmp[16];
                for (i=1;i<=n;i++) {for (j=i;j<=n;j++) {for (k=1;k<=i-1;k++) {
                memcpy (tmp,a,sizeof (TMP));
                for (p=k; p<=i-1; p++) a[p+j-i+1]=tmp[p]; for (p=i; p<=j; p++) a[p-i+k]=tmp[p];
                DFS (D+1);
                if (found) return;
            memcpy (a,tmp,sizeof (TMP));
                } for (k=j+1; k<=n; k++) {memcpy (tmp,a,sizeof (TMP));
                for (p=j+1; p<=k; p++) a[p-j+i-1]=tmp[p];
                for (p=i; p<=j; p++) a[p+k-j]=tmp[p];
                DFS (D+1);
                if (found) return;
            memcpy (a,tmp,sizeof (TMP));
    }}}} int main () {freopen ("hanoi.in", "R", stdin);
    Freopen ("Hanoi.out", "w", stdout);
        while (1) {read (n);
        if (n==0) break;
        Flag=true;
            for (int i=1;i<=n;i++) {read (a[i]);
        if (a[i]!=i) Flag=false;
        } a[0]=0,a[n+1]=n+1;
            if (flag) {printf ("0\n");
        Continue;
        } found=depth=0;
   while (!found)     {depth++;
            DFS (1);
        if (depth>=4) break;
        } if (found) printf ("%d\n", depth);
    else printf ("5\n");
} return 0; }
UVA11882

Test Instructions
Give you a map, the map includes the 1~9 and the # characters, #是不能进入的, you can appear from any one can go, and constantly move to the surrounding four lattice, but can not walk the repeated path, each to a removable lattice, the value of the transformation is the rule is the new lattice in the number of the current value of the back one, namely: now= Now*10+value.
Ideas
Because n*m<=30, if the value as a number to store, or a bit of trouble, after all, unsigned long long has not been 20-bit, but also because the transformation rule is to be followed, which conforms to the addition of string type rules, So we thought of the string type as the storage variable.
Second, when there are limit data, such as the map is full of numbers, then it is easy to get stuck in time. Then, you also need a valuation function to prune, that is, the wide search can reach all the points, if you can reach all the points are not able to make it more than the optimal answer has been searched longer, then you can directly pruning.
It is worth noting that the maximum length of a path that the current point can legitimately reach is not properly calculated. Because we only need a general valuation function, accurate to calculate, but make this valuation function to increase the complexity of time, moreover, in general, all points and the maximum length is not too much.
Code

#include <iostream> #include <cstring> #include <cstdio> using namespace std;
const int maxn=30;
struct Date {int x, y;};
int n,m,v[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
BOOL B[MAXN][MAXN],HT[MAXN][MAXN];;
Char MAP[MAXN][MAXN];
String ans,tmp;
Date Q[MAXN*MAXN];
    BOOL CMP (string a,string b) {int lena=a.length (), lenb=b.length (); Return (LENA&GT;LENB) | |
(LENA==LENB&AMP;&AMP;A&GT;B);
    } int h (int x,int y) {int head=0,tail=0,cnt=0;
    Q[tail].x=x;
    Q[tail++].y=y;
    memcpy (Ht,b,sizeof (HT)); while (Head!=tail) {for (int i=0;i<4;++i) {int xx=q[head].x+v[i][0],yy=q[head].y+v[i][1
            ]; if (xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!= ' # ' &&!ht[xx][yy]
                ) {q[tail].x=xx;
                Q[tail++].y=yy;
                ++cnt;
            Ht[xx][yy]=1;
    }} head++;
} return CNT; } void Dfs (int x,int Y,strinG S,int deep) {int l=h (x, y);
    if (L+deep<ans.size ()) return;
    if (L+deep==ans.size () && (s+ "z") <ans) return;
    if (CMP (S,ans)) ans=s;
        for (int i=0;i<4;++i) {int xx=x+v[i][0],yy=y+v[i][1];
        if (xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]!= ' # ' &&!b[xx][yy])
            {b[xx][yy]=true;
            DFS (XX,YY,S+MAP[XX][YY],DEEP+1);
        B[xx][yy]=false;
    }}} int main () {freopen ("mine.in", "R", stdin);
    Freopen ("Mine.out", "w", stdout); while (scanf ("%d%d", &n,&m)!=eof,n| |
        m) {ans= "";       
        for (int i=0;i<n;++i) scanf ("%s", Map[i]); for (int i=0;i<n;++i) for (int j=0;j<m;++j) if (map[i][j]!= ' # ') {mem
                Set (b,0,sizeof (b));
                B[i][j]=1;
                Tmp= "";
                TMP+=MAP[I][J];
            DFS (i,j,tmp,1);
  }      cout<<ans<< "\ n";
} return 0; }

Thus, when a topic takes time, a * idea is important, and writing a valuation function can help subtract a lot of unnecessary calculations.
In addition to the Turtle and Rabbit race algorithm, very good algorithm. Harvest quite abundant.

Contact Us

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.