G - Pots -BFS水杯状态

G - Pots

 POJ - 3414 

题意:
找到最快的一种能够使其中一个容器中的水为c的状态。

思路:

vis[]记录两个杯子中水的状态.然后搜索的时候按照六种操作进行搜索。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;
#define maxn 150
#define inf 0x3f3f3f3f
int n,m,k,a,b,c,ans;
int to[5][2]= {{0,1},{0,-1},{1,0},{-1,0}};
char mmp[maxn][maxn];
bool vis[maxn][maxn];
int in[maxn][maxn];
struct node
{
    int x,y,step;
} top,temp,out[maxn][maxn];
stack<int>stk;
int bfs()
{
    queue<node>q;
    q.push((node)
    {
        0,0,0
    });
    vis[0][0]=1;
    while(!q.empty())
    {
        top=q.front();
        q.pop();
        if(top.x==c||top.y==c)
        {
            while(top.x!=0||top.y!=0)
            {
                stk.push(in[top.x][top.y]);
                k=top.x;
                top.x=out[top.x][top.y].x;
                top.y=out[k][top.y].y;
            }
            cout<<stk.size()<<endl;
            while(!stk.empty())
            {
                if(stk.top()==1)
                    cout<<"FILL(1)"<<endl;
                else if(stk.top()==2)
                    cout<<"FILL(2)"<<endl;
                else if(stk.top()==3)
                    cout<<"DROP(1)"<<endl;
                else if(stk.top()==4)
                    cout<<"DROP(2)"<<endl;
                else if(stk.top()==5)
                    cout<<"POUR(1,2)"<<endl;
                else
                    cout<<"POUR(2,1)"<<endl;
                stk.pop();
            }
            return 1;
        }
        temp.step=top.step+1;
        if(top.x<a&&!vis[a][top.y])
        {
            temp.x=a;
            temp.y=top.y;
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=1;
            q.push(temp);
        }
        if(top.y<b&&!vis[top.x][b])
        {
            temp.x=top.x;
            temp.y=b;
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=2;
            q.push(temp);
        }
        if(top.x>0&&!vis[0][top.y])
        {
            temp.x=0;
            temp.y=top.y;
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=3;
            q.push(temp);
        }
        if(top.y>0&&!vis[top.x][0])
        {
            temp.x=top.x;
            temp.y=0;
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=4;
            q.push(temp);
        }
        if(top.x>0&&top.y<b&&!vis[top.x-min(top.x,b-top.y)][top.y+min(top.x,b-top.y)])
        {
            temp.x=top.x-min(top.x,b-top.y);
            temp.y=top.y+min(top.x,b-top.y);
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=5;
            q.push(temp);
        }
        if(top.y>0&&top.x<a&&!vis[top.x+min(top.y,a-top.x)][top.y-min(top.y,a-top.x)])
        {
            temp.x=top.x+min(top.y,a-top.x);
            temp.y=top.y-min(top.y,a-top.x);
            vis[temp.x][temp.y]=1;
            out[temp.x][temp.y].x=top.x;
            out[temp.x][temp.y].y=top.y;
            in[temp.x][temp.y]=6;
            q.push(temp);
        }
    }
    return -1;
}
int main()
{
    cin>>a>>b>>c;
    ans=bfs();
    if(ans==-1)
        cout<<"impossible"<<endl;
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/82710712
今日推荐