poj 3414BFS


題意:2つの容積がそれぞれaとbのpotを与え、以下の3つの操作方式に従って、一定のステップ数後、使者2つのpotのうちの1つの水量がcであるかどうかを求める.
      1.FILL(i):ipotを水で満たします.
      2.DROP(i):ipotを空にします.
      3.POUR(i,j):ipotの水をjpotに注いで、ipotが空になるか、jpotが満タンになるまで.
構想:bfsは最短経路を求め,1426と類似しているが,各ノードのサブノード数は6個である.
コードは次のとおりです.
#include<iostream>
using namespace std;
const int Max = 101;

 

struct node
{
    int ope;  int a;  int b;  node *pre;
}que[Max * Max];  //      ,ope       ,a,b       pot     。


bool vis[Max][Max];

char str[6][10] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};

 //            。

 

int min(int a, int b)
{
    return a < b ? a : b;
}

 

void print(node now)
{
    if(now.pre != NULL)
	{
        print(*now.pre);
        cout << str[now.ope] << endl;
    }
}//               

 

void bfs(int a, int b, int c)
{
    int steps = 0;
    int head = 0, tail = 1;
    que[0].a = que[0].b = 0;
    que[0].pre = NULL; 
    while(tail - head > 0)
	{
        int count = tail - head;
        while(count --)//             
		{
            node now = que[head];
            if(now.a == c || now.b == c)
			{
                cout << steps << endl;
                print(now);
                return;
            }
            if(!vis[a][now.b])
			{
                que[tail].ope = 0;
                que[tail].a = a;
                que[tail].b = now.b;
                que[tail].pre = &que[head];
                vis[a][now.b] = true;
                tail ++;
            }
            if(!vis[now.a][b])
			{
                que[tail].ope = 1;
                que[tail].a = now.a;
                que[tail].b = b;
                que[tail].pre = &que[head];
                vis[now.a][b] = true;
                tail ++;
            }
            if(!vis[0][now.b])
			{
                que[tail].ope = 2;
                que[tail].a = 0;
                que[tail].b = now.b;
                que[tail].pre = &que[head];
                vis[0][now.b] = true;
                tail ++;
            }
           if(!vis[now.a][0])
		   {
                que[tail].ope = 3;
                que[tail].a = now.a;
                que[tail].b = 0;
                que[tail].pre = &que[head];
                vis[now.a][0] = true;
                tail ++;
            }
            int wat1 = min(now.a, b - now.b);
            if(!vis[now.a - wat1][now.b + wat1])
			{
                que[tail].ope = 4;
                que[tail].a = now.a - wat1;
                que[tail].b = now.b + wat1;
                que[tail].pre = &que[head];
                vis[now.a - wat1][now.b + wat1] = true;
                tail ++;
            }
            int wat2 = min(a - now.a, now.b);
            if(!vis[now.a + wat2][now.b - wat2])
			{
                que[tail].ope = 5;
                que[tail].a = now.a + wat2;
                que[tail].b = now.b - wat2;
                que[tail].pre = &que[head];
                vis[now.a + wat2][now.b - wat2] = true;
                tail ++;
            }
            head ++;
        }
        steps ++;
    }
    cout << "impossible" << endl;
}

 

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    memset(vis, false, sizeof(vis));//   
    vis[0][0] = true;//   
    bfs(a, b, c);
    return 0;
}