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 pleasant thing, but seeyou don't think so. Because every time when Seeyou bought Coke, Ox asked to share this bottle of Coke with seeyou, and must drink as much as seeyou. But Seeyou's hands only two cups, their capacity is N ml and M ml cola volume for S (s<101) ml (just fill a bottle), they can pour coke between three each other (there is no scale, and s==n+m,101>s>0,n>0,m>0). Smart Acmer, do you think they can split it? If you can output the minimum number of Coke, if 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 if it can be divided into a minimum number of times to pour, 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, enumerate 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 the time also is not full, if dissatisfaction then also need to determine 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;}


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.