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

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

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

    Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

    Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

    Input

    On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

    Output

    The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

    Sample Input

    3 5 4

    Sample Output

    6

    FILL(2)

    POUR(2,1)

    DROP(1)

    POUR(2,1)

    FILL(2)

    POUR(2,1)

       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: Graph G ;
      30: Table T ;
      31: gSlot myQueue[60000] ;
      32: string pathArr[6] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)" } ;
      33:  
      34: void PrintPath(int a,int b)
      35: {
      36:     if( a==0 && b==0 )
      37:         return ;
      38:     PrintPath( T[a][b].preVertex.a , T[a][b].preVertex.b ) ;
      39:     cout<<pathArr[T[a][b].path]<<endl;
      40: }
      41:  
      42: void run3414()
      43: {
      44:     int A,B,C ;
      45:     int i,j ;
      46:     int adji,adjj ;
      47:     int k ;
      48:     int front,rear,size ;  //myQueue
      49:     int curDist ;
      50:     gSlot adjVertex[6] ;  //通过计算得到在某状态时由6种操作得到的6个邻接状态
      51:  
      52:     scanf( "%d%d%d", &A,&B,&C );
      53:  
      54:     gSlot s(0,0) ;  //起点
      55:  
      56:     size = 0 ;      //queue初始化
      57:     rear = 0 ;
      58:     front = 1 ;
      59:  
      60:     T[0][0].dist = 0 ;    //table初始化
      61:     myQueue[++rear] = s ; //起点入队
      62:     ++size ;
      63:  
      64:     while( size != 0 )
      65:     {
      66:         i = myQueue[front].a ;   //得到出队节点和其距离
      67:         j = myQueue[front].b ;
      68:         curDist = T[i][j].dist ;
      69:         ++front ;
      70:         --size ;
      71:  
      72:         if( i==C || j==C )
      73:         {
      74:             printf( "%d\n", curDist );
      75:             PrintPath(i,j) ;
      76:             return ;
      77:         }
      78:  
      79:         //得到(i,j)邻接节点,与pathArr的操作对应
      80:         adjVertex[0].a = A; adjVertex[0].b = j;  //FILL(1) --> (A,j)
      81:         adjVertex[1].a = i; adjVertex[1].b = B;  //FILL(2) --> (i,B)
      82:         adjVertex[2].a = 0; adjVertex[2].b = j;  //DROP(1) --> (0,j)
      83:         adjVertex[3].a = i; adjVertex[3].b = 0;  //DROP(2) --> (i,0)
      84:         
      85:         if( i+j>=B )  //POUR(1,2) --> (i+j-B,B) OR (0,i+j)
      86:         {
      87:             adjVertex[4].a = i+j-B; adjVertex[4].b = B;
      88:         }
      89:         else
      90:         {
      91:             adjVertex[4].a = 0; adjVertex[4].b = i+j;
      92:         }
      93:  
      94:         if( i+j>=A )  //POUR(2,1) --> (A,i+j-A) OR (i+j,0)
      95:         {
      96:             adjVertex[5].a = A; adjVertex[5].b = i+j-A;
      97:         }
      98:         else
      99:         {
     100:             adjVertex[5].a = i+j; adjVertex[5].b = 0;
     101:         }
     102:  
     103:         for( k=0 ; k<6 ; ++k )  //对邻接的6个节点依次处理
     104:         {
     105:             adji = adjVertex[k].a ;
     106:             adjj = adjVertex[k].b ;
     107:  
     108:             if( T[adji][adjj].dist == INF )
     109:             {
     110:                 T[adji][adjj].dist = curDist+1 ;
     111:                 T[adji][adjj].path = k ;   //对应pathArr中的操作
     112:                 T[adji][adjj].preVertex.a = i ;
     113:                 T[adji][adjj].preVertex.b = j ;
     114:  
     115:                 //得到结果
     116:                 if( adji==C || adjj==C )
     117:                 {
     118:                     printf( "%d\n", curDist+1 );
     119:                     PrintPath(adji,adjj) ;
     120:                     return ;
     121:                 }
     122:  
     123:                 //入队
     124:                 ++size ;
     125:                 ++rear ;
     126:                 myQueue[rear].a = adji ;
     127:                 myQueue[rear].b = adjj ;
     128:             }
     129:         }
     130:     }
     131:     printf("impossible\n") ;
     132: }

    如果您满意我的博客,请点击“订阅Allen Sun的技术博客”即可订阅,谢谢:)

    原创文章属于Allen Sun
    欢迎转载,但请注明文章作者Allen Sun和链接
  • 相关阅读:
    解决Select2控件不能在jQuery UI Dialog中不能搜索的bug
    Markdown编辑器入门
    使用Ubuntu 12.04作为日常电脑环境
    DIV元素水平和垂直居中
    xocodebulid 自动化打包 解决提示 ld: library not found for -lPods 问题
    IOS 项目的瘦身工具
    Core Animation系列之CADisplayLink
    IOS7 新特性
    objc中国 和 翻译团队博客 (不错的学习ios 资源)
    IOS6 IOS7 Mapkit draw Rout(地图划线)
  • 原文地址:https://www.cnblogs.com/allensun/p/1872083.html
Copyright © 2011-2022 走看看