【原】 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: }

转载于:https://www.cnblogs.com/allensun/archive/2010/11/05/1870084.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1915是一道经典的搜索题目,也被称为“Knight Moves”。下面我将为您提供解题思路和解题步骤: 1. 题目描述 在8x8的国际象棋棋盘上,棋子“马”从初始位置出发,允许走日字形的移动。给定目标位置,求出从初始位置到目标位置最少需要几步。 2. 解题思路 这道题目可以采用广度优先搜索(BFS)算法解决。将初始位置加入队列中,然后依次处理队列中的每个位置,根据“马”的走法,生成下一步可以到达的位置,如果这个位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数。 3. 解题步骤 具体的解题步骤如下: (1)将初始位置加入队列中,并记录步数为0。 (2)依次处理队列中的每个位置,生成下一步可以到达的位置。 (3)如果下一步可以到达的位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数为当前位置的步数+1。 (4)重复步骤(2)和(3),直到队列为空。 (5)如果目标位置被访问过,则返回到达目标位置的步数,否则返回-1。 4. 注意事项 在实现BFS算法的过程中,需要注意以下几点: (1)需要记录每个位置是否被访问过,以避免重复访问。 (2)需要注意边界条件,防止数组越界。 (3)需要注意判断目标位置是否合法,即是否在棋盘范围内。 5. 总结 本题是一道经典的搜索题目,采用BFS算法可以求解出从初始位置到目标位置最少需要几步。在解题过程中,需要注意边界条件和目标位置是否合法等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值