poj 3414 Pots bfs+模拟

#include<iostream>
#include<cstring>
#define fillA 1
#define pourAB 2
#define dropA 3
#define fillB 4
#define pourBA 5
#define dropB 6

#define N 10000

using namespace std;
int vis[105][105];
class node
{
public:
   int A, B;
   int dir;
   node()
   {
      A=0;
      B=0;
   }
};

int a, b, c, tot;
node q[N];
int pre[N];

void printPath(int index)
{
   if(index==0)
   {
       cout<<tot<<endl;
       return ;
   }
   ++tot;
   printPath(pre[index]);
   switch(q[index].dir)
   {
      case 1:
           cout<<"FILL(1)";
           break;
      case 2:
           cout<<"POUR(1,2)";
           break;
      case 3:
           cout<<"DROP(1)";
           break;
      case 4:
           cout<<"FILL(2)";
           break;
      case 5:
           cout<<"POUR(2,1)";
           break;
      case 6:
           cout<<"DROP(2)";
           break;
   }
   cout<<endl;
}

int bfs()
{
   int head=0, rear=1, r;
   node cur, next;
   memset(vis, 0, sizeof(vis));
   pre[0]=0;
   vis[0][0]=1;
   while(head<rear)
   {
       cur=q[head];
       if(cur.A==c || cur.B==c)
       {
             printPath(head);
             return 1;
       }
       next=cur;
       if(!vis[a][cur.B])//将 A 装满
       {
             next.A=a;
             next.dir=fillA;
             pre[rear]=head;
             vis[a][cur.B]=1;
             q[rear++]=next;
       }
       if(!vis[0][cur.B])//将 A 倒掉
       {
             next.A=0;
             next.dir=dropA;
             pre[rear]=head;
             vis[0][cur.B]=1;
             q[rear++]=next;
       }
       r=b-cur.B;
       next.dir=pourAB;
       if(r>cur.A && !vis[0][cur.B+cur.A])//将A 倒向 B
       {
             next.A=0;
             next.B=cur.B+cur.A;
             pre[rear]=head;
             q[rear++]=next;
             vis[0][cur.B+cur.A]=1;
       }
       else if(r<=cur.A && !vis[cur.A-r][cur.B+r])
       {
             next.A=cur.A-r;
             next.B=cur.B+r;
             pre[rear]=head;
             q[rear++]=next;
             vis[cur.A-r][cur.B+r]=1;
       }

       next=cur;//开始错在这里了, 忘记了从新给next赋值了
       if(!vis[cur.A][b])//将 B 装满
       {
             next.B=b;
             next.dir=fillB;
             pre[rear]=head;
             q[rear++]=next;
             vis[cur.A][b]=1;
       }
       if(!vis[cur.A][0])//将 B 倒掉
       {
             next.B=0;
             next.dir=dropB;
             pre[rear]=head;
             q[rear++]=next;
             vis[cur.A][0]=1;
       }
       r=a-cur.A;
       next.dir=pourBA;
       if(r>cur.B && !vis[cur.B+cur.A][0])//将B 倒向 A
       {
             next.B=0;
             next.A=cur.B+cur.A;
             pre[rear]=head;
             q[rear++]=next;
             vis[cur.B+cur.A][0]=1;
       }
       else if(r<=cur.B && !vis[cur.A+r][cur.B-r])
       {
             next.A=cur.A+r;
             next.B=cur.B-r;
             pre[rear]=head;
             q[rear++]=next;
             vis[cur.A+r][cur.B-r]=1;
       }
      ++head;
   }
   return 0;
}

int main()
{
   while(cin>>a>>b>>c)
   {
      tot=0;
      if(c>a && c>b)
      {
         cout<<"impossible"<<endl;
         continue;
      }
      if(!bfs())
         cout<<"impossible"<<endl;
   }
   return 0;
}
时间: 2024-10-30 16:13:17

poj 3414 Pots bfs+模拟的相关文章

poj 3414 (POTS) (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12295   Accepted: 5190   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 1835 宇航员:模拟&amp;amp;三维向量旋转

Description 问题描述: 宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下图所示: 现对六个方向分别标号,x,y,z正方向分别为0,1,2,负方向分别为3,4,5:称它们为绝对方向.宇航员在宇宙中只沿着与绝对坐标系xyz轴平行的方向行走,但是他不知道自己当前绝对坐标和自己面向的绝对方向. 任务描述: 请根据宇航员对自己在相对方向上移动的描述确定宇航员最终的绝对坐标和面向的绝对

UVa 706 / POJ 1102 LCD Display (模拟)

706 - LCD Display Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647 http://poj.org/problem?id=1102 A friend of you has just bought a new computer. Until

算法题:POJ 1068 Parencodings 栈模拟

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence). q By an integer

poj 1724ROADS(bfs和dfs做法)

/* dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! */ #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #define Max 0x3f3f3f3f using namespace std; struct node{ int D; int L, T; node(int D, int L,

算法题之POJ 1573 Robot Motion 模拟

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are N north (up the page) S south (down the page) E east (to the right on

UVa 10274:Fans and Gems, 神牛Rujia Liu的神题(三)

题目链接: UVa :  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1215 zoj    : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1237 类型:  隐式图搜索+恶心模拟+哈希判重+bfs+dfs 原题: Tomy's

POJ 3206 Borg Maze:BFS+Prim

Borg Maze:http://poj.org/problem?id=3026 大意:给你一个m*n的迷宫,可以上下左右的走,只能走空格或字母,求出将所有字母连通起来的最小耗费. 思路:先用BFS求出S到所有A的距离,再用Prim求最小生成树,求出最小耗费.这个题坑的不在题,是数据太坑了,在空格处理上没弄好,贡献了好几个WA和CE,看Discuss才知道很坑,最后用G++过了的代码,C++还RE,实在不知道说什么好了  =.= 更多精彩内容:http://www.bianceng.cnhttp

POJ 3371 Flesch Reading Ease (模拟题)

Flesch Reading Ease:http://poj.org/problem?id=3371 题目很水,就是看懂题就行. 题意: 给出一篇规范的文章,求其 句子数.单词数 和 音节数把这3个值代入题目给出的公式,输出其结果,保留2位小数. 标记单词分隔符: 逗号(,) 和 空格( ) 句子分隔符:句号(.) 问号(?) 冒号(:) 分号(;) 感叹号(!) 音节处理要求: (1)当单词总长度<=3时,音节数无条件+1 (2) 当单词总长度>3时,单词中每出现一个元音字母(a.e.i.o