zoukankan      html  css  js  c++  java
  • 【原】 POJ 1606 Jugs 状态BFS 解题报告

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


    方法:
    该题简化之后即为求从初始状态(0,0)到终止状态(i,C)的最短路径
    对于每个状态(i,j)存在由6种操作得到的6个邻接状态,即为图中的邻接节点
    将每种操作和得到的节点状态对应上以便打印路径,即pathArr[6]和adjVertex[6]

    注意:
    此题的input无终止条件,所以一定要在scanf后面加上!=EOF,不然会造成Output Limit Exceeded

    Description

    In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle. 
    You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A. 
    A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are 
    fill A 
    fill B 
    empty A 
    empty B 
    pour A B 
    pour B A 
    success 
    where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished. 
    You may assume that the input you are given does have a solution.

    Input

    Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

    Output

    Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

    Sample Input

    3 5 4

    5 7 3

    Sample Output

    fill B

    pour B A

    empty A

    pour B A

    fill B

    pour B A

    success

    fill A

    pour A B

    fill A

    pour A B

    empty B

    pour A B

    success

       1: #include <stdio.h>
       2: #include <iostream>
       3: #include <vector>
       4: #include <string>
       5:  
       6: using namespace std ;
       7:  
       8: const int INF = 0x7fffffff ;
       9:  
      10: struct gSlot
      11: {
      12:     gSlot(){}
      13:     gSlot(int i,int j):a(i),b(j){}
      14:     int a ;
      15:     int b ;
      16: };
      17:  
      18: struct tSlot
      19: {
      20:     tSlot():dist(INF){preVertex.a=0 ; preVertex.b=0;}
      21:     int dist ;
      22:     gSlot preVertex ;
      23:     int path ;  //结合pathArr的下标记录路径的操作
      24: };
      25:  
      26: typedef vector< struct gSlot > Graph[101][101] ;
      27: typedef tSlot Table[101][101] ;
      28:  
      29: gSlot myQueue[60000] ;
      30: string pathArr[6] = { "fill A","fill B","empty A","empty B","pour A B","pour B A" } ;
      31:  
      32: void PrintPath(Table T,int a,int b)
      33: {
      34:     if( a==0 && b==0 )
      35:         return ;
      36:     PrintPath( T , T[a][b].preVertex.a , T[a][b].preVertex.b ) ;
      37:     cout<<pathArr[T[a][b].path]<<endl;
      38: }
      39:  
      40: void run1606()
      41: {
      42:     int A,B,C ;
      43:     int i,j ;
      44:     int adji,adjj ;
      45:     int k ;
      46:     int front,rear,size ;  //myQueue
      47:     int curDist ;
      48:     bool found ;
      49:     gSlot adjVertex[6] ;  //通过计算得到在某状态时由6种操作得到的6个邻接状态
      50:  
      51:     while( scanf( "%d%d%d", &A,&B,&C )!=EOF )
      52:     {
      53:         Graph G ;
      54:         Table T ;
      55:         found = false ;
      56:         gSlot s(0,0) ;  //起点
      57:  
      58:         size = 0 ;      //queue初始化
      59:         rear = 0 ;
      60:         front = 1 ;
      61:  
      62:         T[0][0].dist = 0 ;    //table初始化
      63:         myQueue[++rear] = s ; //起点入队
      64:         ++size ;
      65:  
      66:         while( !found && size != 0 )
      67:         {
      68:             i = myQueue[front].a ;   //得到出队节点和其距离
      69:             j = myQueue[front].b ;
      70:             curDist = T[i][j].dist ;
      71:             ++front ;
      72:             --size ;
      73:  
      74:             if( j==C )
      75:             {
      76:                 //printf( "%d\n", curDist );
      77:                 PrintPath(T,i,j) ;
      78:                 printf("success\n") ;
      79:                 found = true ;
      80:                 break ;
      81:             }
      82:  
      83:             //得到(i,j)邻接节点,与pathArr的操作对应
      84:             adjVertex[0].a = A; adjVertex[0].b = j;  //FILL(1) --> (A,j)
      85:             adjVertex[1].a = i; adjVertex[1].b = B;  //FILL(2) --> (i,B)
      86:             adjVertex[2].a = 0; adjVertex[2].b = j;  //DROP(1) --> (0,j)
      87:             adjVertex[3].a = i; adjVertex[3].b = 0;  //DROP(2) --> (i,0)
      88:             
      89:             if( i+j>=B )  //POUR(1,2) --> (i+j-B,B) OR (0,i+j)
      90:             {
      91:                 adjVertex[4].a = i+j-B; adjVertex[4].b = B;
      92:             }
      93:             else
      94:             {
      95:                 adjVertex[4].a = 0; adjVertex[4].b = i+j;
      96:             }
      97:  
      98:             if( i+j>=A )  //POUR(2,1) --> (A,i+j-A) OR (i+j,0)
      99:             {
     100:                 adjVertex[5].a = A; adjVertex[5].b = i+j-A;
     101:             }
     102:             else
     103:             {
     104:                 adjVertex[5].a = i+j; adjVertex[5].b = 0;
     105:             }
     106:  
     107:             for( k=0 ; k<6 ; ++k )  //对邻接的6个节点依次处理
     108:             {
     109:                 adji = adjVertex[k].a ;
     110:                 adjj = adjVertex[k].b ;
     111:  
     112:                 if( T[adji][adjj].dist == INF )
     113:                 {
     114:                     T[adji][adjj].dist = curDist+1 ;
     115:                     T[adji][adjj].path = k ;   //对应pathArr中的操作
     116:                     T[adji][adjj].preVertex.a = i ;
     117:                     T[adji][adjj].preVertex.b = j ;
     118:  
     119:                     //得到结果
     120:                     if( adjj==C )
     121:                     {
     122:                         //printf( "%d\n", curDist+1 );
     123:                         PrintPath(T,adji,adjj) ;
     124:                         printf("success\n") ;
     125:                         found = true ;
     126:                         break ;
     127:                     }
     128:  
     129:                     //入队
     130:                     ++size ;
     131:                     ++rear ;
     132:                     myQueue[rear].a = adji ;
     133:                     myQueue[rear].b = adjj ;
     134:                 }
     135:             }//for( k=0 ; k<6 ; ++k )
     136:         }//while( !found && size != 0 )
     137:     }//while( scanf( "%d%d%d", &A,&B,&C ) )
     138: }
  • 相关阅读:
    (转)ab(apachebench)测试与loadrunner
    hibernate学习总结
    Oracle 11G在用EXP 导入、导出时,若有空表对导入导出中遇到的问题的解决
    Nginx可以做什么
    Oracle的表空间、用户和表的区别和联系
    oracle11g 导出表报EXP-00011:table不存在。
    tomcat、nginx、apache、tengine都是什么,及其作用
    注解和依赖注入框架
    js中innerHTML与innerText的用法与区别
    Linux中 /boot 目录介绍
  • 原文地址:https://www.cnblogs.com/allensun/p/1870084.html
Copyright © 2011-2022 走看看