B - For Water

Pour Problems "fill A" represents A filling cups, "empty A" represents A cup emptied, "pour AB" represented by A is poured into water and the cup B B A cup filling or emptying.
Input
input comprising a plurality of sets of data. Each set of data inputs A, B, C data range 0 <A <= B, C <= B <= 1000, A and B are relatively prime.
Output
Your program's output will be a series of instructions. These output lines can cause a jar just any unit C comprises water. Finally, each data line of output should be "success". Output lines start from the first column should not be any empty lines or trailing spaces.
The Input the Sample
2. 7. 5
2. 7. 4
Sample Output
Fill B
pour BA
Success
Fill A
pour AB
Fill A
pour AB
Success
the Notes
If your output Sample Output different, it does not matter. For the answer to this question is multiple solutions in a "AB C", not by the standard text comparison to determine the correctness of your program. So this question by the SPJ (Special Judge) program to determine whether the code you write correctly.
Problem-solving ideas
The A, B state as water bottles, corresponding to each point in the maze. V water in the initial state A, B is 0,0 bottles. Each operating state will change. A total of six operating state. Use bfs to search. Each state in the path of the recording operation of the array. When fill A, in a case when not full of water, the water in the cup a full set, b remains as water in the cup, into the queue. When the fill B, b in case when the water is non-full, the full set b cup water, water A cup intact, into the queue. When the drop A, the water in a blank, b of the same water, into the queue. When drop B, b in the water blank, a is constant in the water, into the queue. When pour (a, b), it is determined that this time need points, if b is greater than a water filled in the remaining water, put the full set b cup, otherwise the water will be poured into a b, a is set air. pour (b, a) are similar.
Once put the current operation corresponding to each recording bfs operand path in the array.

#include <iostream>
#include<stdio.h>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 1500;
int a, b, c;
bool vis[MAXN+1][MAXN+1];
struct node {
        int a, b;
		int path[MAXN+1];
		int plen;

};
char path[6][20] = {

     "fill A"

    ,"fill B"

    ,"empty A"

    ,"empty B"

    ,"pour A B"

    ,"pour B A"

};
void output_result(int p[], int n)
{

    for(int i=0; i<n; i++)

        printf("%s\n",path[p[i]]);
        
        printf("success\n");

}
void bfs()
{
     queue<node> q;
     memset(vis, true, sizeof(vis));
      node f;
	f.a = 0;
	  f.b = 0;
     
      memset(f.path, 0, sizeof(f.path));
      f.plen = 0;
      q.push(f);
     vis[f.a][f.b] = false;
      while(!q.empty()) {
          f = q.front();
            q.pop();
         if(f.a == c || f.b == c) {
            output_result(f.path, f.plen);
                return;
         }
       node v;
        v = f;
         v.plen++;

        // FILL(a)
        if(a - f.a > 0) {
              v.a = a;

            v.b = f.b;

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 0;

                q.push(v);

                vis[v.a][v.b] = false;

            }

        }

        // FILL(b)

        if(b - f.b > 0) {

            v.a = f.a;

            v.b = b;

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 1;

                q.push(v);

               vis[v.a][v.b] = false;

            }

        }

        // DROP(a)

        if(f.a) {

            v.a = 0;

            v.b = f.b;

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 2;

                q.push(v);

                vis[v.a][v.b] = false;

            }

        }

        // DROP(b)

        if(f.b) {

            v.a = f.a;

            v.b = 0;

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 3;

                q.push(v);

                vis[v.a][v.b] = false;

            }

        }

        // POUR(a,b)

        if(f.a && (f.b < b)) {

            if(f.a > (b - f.b)) {

                v.a = f.a -(b - f.b);

                v.b = b;

            } else {

                v.a = 0;

                v.b = f.b + f.a;

            }

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 4;

                q.push(v);

                vis[v.a][v.b] = false;

            }

        }

        // POUR(b,a)

        if(f.b && (f.a < a)) {

            if(f.b > (a - f.a)) {

                v.a = a;

                v.b = f.b -(a - f.a);

            } else {

                v.a = f.a + f.b;

                v.b = 0;

            }

            if(vis[v.a][v.b]) {

                v.path[f.plen] = 5;

                q.push(v);

               vis[v.a][v.b] = false;

            }

        }

    }
}
int main()
{
     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
        bfs();   
   return 0;

}
Released five original articles · won praise 0 · Views 50

Guess you like

Origin blog.csdn.net/qq_32205311/article/details/104692946