Fzu (2188): Wolf sheep Cross River problem, state transfer, BFS solution

Source: Internet
Author: User
Tags min stdin cmath

Topic Connection: http://acm.fzu.edu.cn/problem.php?pid=2188

Meaning: The Chinese topic, is given the number of wolf sheep, the minimum volume of transport to send the wolves and sheep to the other side, the condition is that the shore and the ship's sheep can not be less than the number of wolves, and each ferry at least one animal. No solution is output-1.

As long as the careful analysis, still can think of the state transfer method.

Suppose the left bank has X, y sheep and wolves, respectively, the right bank has sheep and wolf m,n only, assuming that from the left bank of the Sheep Wolf transfer p,q only to the right bank, you must meet the conditions:

X-p not for 0 o'clock,x-p>=y-q;

M+p not for 0 o'clock,m+p>=n+q;

P Not for 0 o'clock,p>=q;

It is important to note that when x-p or M+q or P is 0 o'clock, special handling is required, at which time the wolf can be any value (must be objective) without being larger than the number of sheep.

The idea of the right bank to the left bank is similar.

Analysis to this.

Code:

Just started with BFS, tle, and then rewrite double BFS, the result is re, look carefully, incredibly is two staggered topic, really ashamed ah ....

However, the problem with BFS and double BFS gap is quite large, a 70ms+, one reached the 900ms+.

One-way BFS:

#include <iostream> #include <cstdio> #include <sstream> #include <cmath> #include <cstring
> #include <algorithm> #include <set> #include <queue> #include <vector> #include <map>

#include <string> #define LL __int64 #define INF 0xfffffff using namespace std;

struct state{int x,y;//The number of sheep wolves on the left bank int m,n;//The number of sheep wolves in the right bank int step;//transfer steps}; int vis[220][220][2];//The left Bank status flag, why is it 2?

Vis[x][y] There are two different values//may be transferred from the right bank to the left bank of X, Y, or from the right bank to the left bank//After the X, Y, which is not the same int x,y,n;
    void BFs () {queue<state> Q;
    Q.push (state) {x,y,0,0,0});
    Vis[x][y][0]=vis[x][y][1]=1; while (!
        Q.empty ()) {State tp=q.front ();
            if (!tp.x &&!tp.y) {cout<<tp.step<<endl;
        Return
        } q.pop (); if (Tp.step & 1) {//From right bank to left bank for (int p=0;p<=min (N,TP.M);p + +)//The following note is sheep for 0 o'clock, the number of wolves processed for (int q=0;! P && (Q<=min (TP.N, N)) | |
                    P && q<=p && (p+q) <=min (tp.m+tp.n,n); q++) {if (p+q==0) continue;
                    int tx=tp.x+p,ty=tp.y+q,tm=tp.m-p,tn=tp.n-q; The following also pay attention to the number of sheep is 0 o'clock, the number of wolves processing if (tm<0 | | tn<0 | | TM && TM&LT;TN | | tx&&tx<ty) Conti
                    Nue
                        if (!vis[tx][ty][1]) {vis[tx][ty][1]=1;
                    Q.push (state) {tx,ty,tm,tn,tp.step+1});  }}} else{//from left bank to right bank for (int p=0;p<=min (n,tp.x);p + +) for (int q=0;! P && (Q<=min (n,tp.y)) | |
                    P && q<=p && (p+q) <=min (N,TP.X+TP.Y); q++) {if (p+q==0) continue;
                    int tx=tp.x-p,ty=tp.y-q,tm=tp.m+p,tn=tp.n+q;
                    if (tx<0 | | ty<0 | | TX && TX&LT;TY | | tm&&tm<tn) continue; if (!vis[tx][ty][0]) {vis[tX][ty][0]=1;
                    Q.push (state) {tx,ty,tm,tn,tp.step+1});
}}}} cout<<-1<<endl;
    } int main () {//freopen ("C:\\Documents and Settings\\All Users\\ desktop \\in.txt", "R", stdin);
        while (cin>>x) {cin>>y>>n;
        memset (vis,0,sizeof (VIS));
    BFS ();
} return 0;
 }


Two-way BFS:

#include <iostream> #include <cstdio> #include <sstream> #include <cmath> #include <cstring
> #include <algorithm> #include <set> #include <queue> #include <vector> #include <map>

#include <string> #define LL __int64 #define INF 0xfffffff using namespace std;
    struct state{int x, y;
    int m,n;
int step;

};
int vis[220][220][2];
int vis2[220][220][2];
int x,y,n; Two-way BFS code looks longer, but is only the basis of BFS and then CTRL + C//need two Vis array to represent the access state before and after, mainly on the basis of the BFS deal with the situation of the encounter void BFs () {queue<state> Q,
    QQ;
    Q.push (state) {x,y,0,0,0});
    Qq.push (state) {0,0,x,y,0});
    Vis2[0][0][0]=vis2[0][0][1]=1;
    Vis[x][y][0]=vis[x][y][1]=1;
    int deep=0; while (! Q.empty () | | ! Qq.empty ()) {while (!
        Q.empty ()) {State tp=q.front ();
        if (tp.step!=deep) break;
        Q.pop (); if (Tp.step & 1) {for (int p=0;p<=min (N,TP.M);p + +) for (int q=0;! P && (Q<=min (tp.n,n)) | | P && q<=P && (p+q) <=min (tp.m+tp.n,n); q++) {if (p+q==0) continue;
                    int tx=tp.x+p,ty=tp.y+q,tm=tp.m-p,tn=tp.n-q;
                    if (tm<0 | | tn<0 | | TM && TM&LT;TN | | tx&&tx<ty) continue; if (Vis2[tx][ty][0]) {cout<< (deep) *2+1<<endl;return;}
                        When meeting two states must be coherent if (!vis[tx][ty][1]) {vis[tx][ty][1]=1;
                    Q.push (state) {tx,ty,tm,tn,tp.step+1}); }}} else{for (int p=0;p<=min (n,tp.x);p + +) for (int q=0;! P && (Q<=min (n,tp.y)) | |
                    P && q<=p && (p+q) <=min (N,TP.X+TP.Y); q++) {if (p+q==0) continue;
                    int tx=tp.x-p,ty=tp.y-q,tm=tp.m+p,tn=tp.n+q;
                    if (tx<0 | | ty<0 | | TX && TX&LT;TY | | tm&&tm<tn) continue; if (Vis2[tx][ty][1]) {COUT&LT;&LT;deep*2+1<<endl;return;}
                        if (!vis[tx][ty][0]) {vis[tx][ty][0]=1;
                    Q.push (state) {tx,ty,tm,tn,tp.step+1}); }}}} while (!
        Qq.empty ()) {State tp=qq.front ();
        if (tp.step!=deep) break;
        Qq.pop (); if (tp.step%2==0) {//Here need and above turn processing, can think carefully for (int p=0;p<=min (N,TP.M);p + +) for (int q=0;! P && (Q<=min (tp.n,n)) | |
                    P && q<=p && (p+q) <=min (tp.m+tp.n,n); q++) {if (p+q==0) continue;
                    int tx=tp.x+p,ty=tp.y+q,tm=tp.m-p,tn=tp.n-q;
                    if (tm<0 | | tn<0 | | TM && TM&LT;TN | | tx&&tx<ty) continue;
                    if (vis[tx][ty][1]) {cout<< (1+deep) *2<<endl;return;}
                        if (!vis2[tx][ty][0]) {vis2[tx][ty][0]=1; Qq.push (state) {tx,ty,tm,tn,tp.step+1}); }}} else{for (int p=0;p<=min (n,tp.x);p + +) for (int q=0;! P && (Q<=min (n,tp.y)) | |
                    P && q<=p && (p+q) <=min (N,TP.X+TP.Y); q++) {if (p+q==0) continue;
                    int tx=tp.x-p,ty=tp.y-q,tm=tp.m+p,tn=tp.n+q;
                    if (tx<0 | | ty<0 | | TX && TX&LT;TY | | tm&&tm<tn) continue;
                    if (Vis[tx][ty][0]) {cout<< (deep+1) *2<<endl;return;}
                        if (!vis2[tx][ty][1]) {vis2[tx][ty][1]=1;
                    Qq.push (state) {tx,ty,tm,tn,tp.step+1});
    }}}} deep++;
} cout<<-1<<endl;
    } int main () {//freopen ("C:\\Documents and Settings\\All Users\\ desktop \\in.txt", "R", stdin);
        while (cin>>x) {cin>>y>>n;
        memset (vis2,0,sizeof (VIS2)); memset (vis,0, sizeof (VIS));
    BFS ();
} return 0;
 }


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.