Hdoj 1495 very coke "BFS"

Source: Internet
Author: User

Very coke.Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5954 Accepted Submission (s): 2428


Problem description Everyone must feel the exercise after drinking Coke is a very satisfying thing, but seeyou don't feel so.

Because every time when Seeyou bought Coke, Ox asked to share this bottle of Coke with seeyou. And be sure to drink as much as seeyou. But Seeyou's hand has only two cups. Their capacity is respectively n ml and M ml Cola volume of S (s<101) ml (just fill a bottle), they are three between each other to pour cola (there is no scale.) and s==n+m,101>s>0. n>0,m>0).

Smart Acmer, do you think they can split it? Assuming that you can output the minimum number of Coke, assuming that you cannot output "no".
Input three integers: the volume of S Cola, N and M is the capacity of two cups, ending with "0 0 0".
Output is assumed to be equal to the number of times to be inverted, otherwise output "NO".
Sample Input

7 4 34 1 30 0 0

Sample Output
NO3

Analysis: You can view the subject as an implicit diagram, enumerating all the cases until you can split them evenly, or enumerate through them.

Code:

#include <cstdio> #include <queue>//Used Priority Queue # include <cstring> #include <algorithm>const int M = 101;using namespace Std;bool vis[101][101][101];    flag State struct node{int v[3];    int step;    BOOL operator < (const node &a) const{return step > A.step;  }};int Hal, V[3];    Hal is half, v array is the volume of each cup bool match (Node A) {int ans = 0;    for (int i = 0; i < 3; + + i) if (a.v[i] = = HAL) ++ans;  if (ans = = 2) return 1; return 0;}    int BFs () {memset (Vis, 0, sizeof (VIS));    Node St; St.v[0] = v[0];    ST.V[1] = st.v[2] = 0;    St.step = 0;    Priority_queue<node >q;    Q.push (ST);    Vis[v[0]][0][0] = 1;    if (Match (ST)) return st.step;        while (!q.empty ()) {Node temp = q.top ();        Q.pop (); for (int i = 0; i < 3; + + i) {if (Temp.v[i]) {//If a cup has water, let him pour water into the other two cups for (int j = 1; J < 3;                    + + j) {node cur = temp;                    int w = (i+j)%3; IF (Cur.v[w] < v[w]) {//Pour when the points are not full, assuming dissatisfaction then also need to infer whether can fill or not fill if (Cur.v[i] <= (v[w]-cur.v[w)) { CUR.V[W] + = Cur.v[i];                            Cur.v[i] = 0;                        if (Match (cur)) return cur.step+1;                            } else {Cur.v[i]-= (v[w]-cur.v[w]);                            CUR.V[W] = v[w];                        if (Match (cur)) return cur.step+1; } if (!vis[cur.v[0]][cur.v[1]][cur.v[2]]) {VIS[CUR.V[0]][CUR.V[1]][CU                                R.V[2]] = 1;                                cur.step++;                        Q.push (cur); }}}}} return-1;} int main () {while (scanf ("%d%d%d", &v[0], &v[1], &v[2]), v[0]| | v[1]| |            V[2]) {if (v[0]&1) {printf ("no\n");        Continue       } else {     Hal = V[0]/2;            int ans = BFS ();            if (ans >= 0) printf ("%d\n", ans);        else printf ("no\n"); }} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Hdoj 1495 very coke "BFS"

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.