zoukankan      html  css  js  c++  java
  • poj 1606 Jugs (bfs)

    http://poj.org/problem?id=1606

    猛一看没思路,仔细想想,记住当前a,b水量及操作就可以bfs了,纯粹的暴力啊。。

    输出需要路径,要在node中用指针记录当前状态的前一状态,根据oper输出即可。

    code:

    #include<cstdio>
    #include<cstring>
    int a, b, n, h, r ;
    bool vis[1001][1001] ;
    int ans[100001] ;
    struct node{
        int va, vb, oper, pre ;
    }queue[1000001] ;
    void output(){
        int i = 1 ;
        ans[0] = queue[h].oper ;
        h = queue[h].pre ;
        while(h){
            ans[i++] = queue[h].oper ;
            h = queue[h].pre ;
        }
        for(int j=i-1; j>=0; j--){
            switch(ans[j]){
                case 1: printf("fill A\n") ;break ;
                case 2: printf("fill B\n") ;break ;
                case 3: printf("empty A\n") ;break ;
                case 4: printf("empty B\n") ;break ;
                case 5: printf("pour A B\n") ;break ;
                case 6: printf("pour B A\n") ;break ;
            }
        }
        printf("success\n") ;
    }
    void bfs(){
        h = r = 0 ;
        queue[r].va = 0 ;
        queue[r++].vb = 0 ;
        while(h!=r){
            node t ;
            if(queue[h].vb==n){
                output() ;
                return ;
            }
            t.va = a ;
            t.vb = queue[h].vb ;
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 1 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            t.va = queue[h].va ;
            t.vb = b ;
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 2 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            t.va = 0 ;
            t.vb = queue[h].vb ;
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 3 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            t.va = queue[h].va ;
            t.vb = 0 ;
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 4 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            if(queue[h].va-(b-queue[h].vb)<0){
                t.va = 0 ;
                t.vb = queue[h].vb + queue[h].va ;
            }
            else{
                t.va = queue[h].va-(b-queue[h].vb) ;
                t.vb = b ;
            }
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 5 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            if(queue[h].vb-(a-queue[h].va)<0){
                t.vb = 0 ;
                t.va = queue[h].va + queue[h].vb ;
            }
            else{
                t.vb = queue[h].vb-(a-queue[h].va) ;
                t.va = a ;
            }
            if(!vis[t.va][t.vb]){
                vis[t.va][t.vb] = true ;
                t.oper = 6 ;
                t.pre = h ;
                queue[r++] = t ;
            }

            h ++ ;
        }
    }
    int main(){
        while(~scanf("%d%d%d", &a, &b, &n)){
            memset(vis, falsesizeof(vis)) ;
            bfs() ;
        }
        return 0 ;} 
  • 相关阅读:
    架构师时间~白话OAuth2
    vue前端各种问题
    windows命令行导入sql
    正则表达式概述
    XPath语法 在C#中使用XPath示例
    面试宝典
    Repository,UnitOfWork,DbContext(1)
    EF Code First:实体映射,数据迁移,重构(1)
    表单常用的正则表达式
    Lambda表达式
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2404100.html
Copyright © 2011-2022 走看看