[POJ] 1606 Jugs(BFS+路径输出)

题目地址:http://poj.org/problem?id=1606

广度优先搜索的经典问题,倒水问题。算法不需要多说,直接BFS,路径输出采用递归。最后注意是Special Judge

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int K=1511;
queue<int> Q;
int c[K][K],ca,cb,N,ax,bx;
char map[7][20]={
    {"fill A"},
    {"fill B"},
    {"empty A"},
    {"empty B"},
    {"pour A B"},
    {"pour B A"}
};
struct node {
    int x,y,step;
} d[K][K];

void init()
{
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
}
void print(int xa,int xb)
{
    if(xa==0 && xb==0) return ;
    print(d[xa][xb].x,d[xa][xb].y);
    printf("%s\n",map[d[xa][xb].step]);
}
void bfs(int cax,int cbx,int N)
{
    while(!Q.empty()) Q.pop();
    c[0][0]=1;
    Q.push(cax);Q.push(cbx);
    
    while(!Q.empty()) {
        int xa=Q.front(); Q.pop();
        int xb=Q.front(); Q.pop();
        
        if(xb==N ) {
                print(xa,xb);
                printf("success\n");
                break;
        }
        if(!c[ca][xb]){
            c[ca][xb]=1;

            d[ca][xb].x=xa;
            d[ca][xb].y=xb;
            d[ca][xb].step=0;
            
            Q.push(ca);
            Q.push(xb);
        }
        if(!c[xa][cb]){
            c[xa][cb]=1;
            
            d[xa][cb].x=xa;
            d[xa][cb].y=xb;
            d[xa][cb].step=1;
            
            Q.push(xa);
            Q.push(cb);
        }
        if(!c[0][xb]){
            c[0][xb]=1;
            
            d[0][xb].x=xa;
            d[0][xb].y=xb;
            d[0][xb].step=2;
            
            Q.push(0);
            Q.push(xb);
        }
        if(!c[xa][0]){
            c[xa][0]=1;
            
            d[xa][0].x=xa;
            d[xa][0].y=xb;
            d[xa][0].step=3;
                
            Q.push(xa);
            Q.push(0);
        }

        if(xa<=cb-xb){
            if(!c[0][xb+xa]){
                c[0][xb+xa]=1;
                
                d[0][xb+xa].x=xa;
                d[0][xb+xa].y=xb;
                d[0][xb+xa].step=4;
                
                Q.push(0);
                Q.push(xb+xa);
            }
        } else {
            if(xa-(cb-xb)>=0 && !c[xa-(cb-xb)][cb]){
                c[xa-(cb-xb)][cb]=1;
                
                d[xa-(cb-xb)][cb].x=xa;
                d[xa-(cb-xb)][cb].y=xb;
                d[xa-(cb-xb)][cb].step=4;
                
                Q.push(ca-(cb-xb));
                Q.push(cb);
            }
        }

        if(xb<=ca-xa){
            if(!c[xa+xb][0]){
                c[xa+xb][0]=1;
                
                d[xa+xb][0].x=xa;
                d[xa+xb][0].y=xb;
                d[xa+xb][0].step=5;
                
                Q.push(xa+xb);
                Q.push(0);
            }
        } else {
            if(xb-(ca-xa)>=0 && !c[ca][xb-(ca-xa)]){
                c[ca][xb-(ca-xa)]=1;
                
                d[ca][xb-(ca-xa)].x=xa;
                d[ca][xb-(ca-xa)].y=xb;
                d[ca][xb-(ca-xa)].step=5;
                
                Q.push(ca);
                Q.push(xb-(ca-xa));
            }
        }
    }
    
}
int main()
{
    
    while(~scanf("%d%d%d",&ca,&cb,&N)){
          init();
          bfs(0,0,N);
        
    }
    
    
    return 0;
}

 

posted @ 2016-03-09 15:54  SXISZERO  阅读(239)  评论(0编辑  收藏  举报