POJ3414 2021-12-09 图 POJ3414pots 【POJ3414】这些bfs题都很妙啊,多做做肯定有好处的 思路: 每到一次可以作为一个方向,总共有6种方向 ①把A装满 ②把B装满 ③把A倒了 ④把B倒了 ⑤把A倒入B ⑥把B倒入A 其实就是bfs,每次操作先看看是否到达过,然后标记,然后顺着找。总共三个数(100以内),能有多少种组合? 直接用二维数组就可以 #include #include #include #include #include #include #include #include #include #include #include using namespace std; int a,b,c; int vis [105][105]; struct node { int u,v; int level; int caozuo; int id; int pre; node(int a,int a1,int a2,int a3,int a4,int a5) { u=a; v=a1; level=a2; caozuo=a3; id=a4; pre=a5; } }; vector arr; int cnt=0; int ans; string str[]= { "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)" }; void bfs() { queue cun; cun.push(node(0,0,0,-1,0,-1)); arr.push_back(node(0,0,0,-1,0,-1)); memset(vis,0,sizeof(vis)); vis[0][0]=1; cnt++; ans=-1; while(!cun.empty()) { node tmp=cun.front(); cun.pop(); if(tmp.u==c||tmp.v==c) { ans=tmp.id; break; }for(int i=0; i<6; i++) { if(i==0)//fill a { if(a-tmp.u>0&&vis[a][tmp.v]==0) { vis[a][tmp.v]=1; node n(a,tmp.v,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); } } else if(i==1) //fill b { if(b-tmp.v>0&&vis[tmp.u][b]==0) { vis[tmp.u][b]=1; node n(tmp.u,b,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); } } else if(i==2) //d a { if(tmp.u>0&&vis[0][tmp.v]==0) { vis[0][tmp.v]=1; node n(0,tmp.v,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); } } else if(i==3) //d b { if(tmp.v>0&&vis[tmp.u][0]==0) { vis[tmp.u][0]=1; node n(tmp.u,0,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); } } else if(i==4) //1->2 { if(tmp.u!=0&&tmp.v!=b) { int dao=min(b-tmp.v,tmp.u); if(vis[tmp.u-dao][tmp.v+dao]==0) { vis[tmp.u-dao][tmp.v+dao]=1; node n(tmp.u-dao,tmp.v+dao,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); }} } else if(i==5) { if(tmp.v!=0&&tmp.u!=a) { int dao=min(a-tmp.u,tmp.v); if(vis[tmp.u+dao][tmp.v-dao]==0) { vis[tmp.u+dao][tmp.v-dao]=1; node n(tmp.u+dao,tmp.v-dao,tmp.level+1,i,cnt,tmp.id); cnt++; arr.push_back(n); cun.push(n); }} }} }if(ans!=-1) { node tmp=arr[ans]; int cnt=0; stack arrx; while(1) { if(tmp.caozuo>=6||tmp.caozuo<0) break; arrx.push(tmp.caozuo); ans=tmp.pre; if(ans==-1) break; tmp=arr[tmp.pre]; cnt++; } cout<>a>>b>>c; bfs(); return 0; } 推荐阅读 mysql回收站功能 mysql收回所有权限 kb4523205更新了什么 如何开发安卓系统,如何开始android开发 回归分析点预测与区间预测的联系 电子信息类就业前景分析,大学专业电子信息类就业前景 什么叫特殊韵母 飞机有喇叭吗?为什么没有声音 飞机有喇叭吗?为什么? 红酒在多少度时喝最好 vc精华液的作用和功效 vc的作用和功效 安卓 苹果 内存机制,为什么比安卓更快?原来是这样 威能锅炉显示5s是什么怎么回事,遇到这种故障解决方法步骤 兰博基尼毒药能上路吗 里番acg全彩资源站 孕妇奶粉哪家好?推荐几款好喝的孕妇奶粉 吸烟|不能饮酒,不能吸烟,不能熬夜,养生专家的话有多少值得相信? 安卓3d开发,本书作者专门出版unity3.x书籍 缅甸花梨木价格暴跌 缅甸黄花梨一斤多少钱 运动女鞋哪种牌子好?想买一双跑步锻炼的鞋子? 电动车合格证可以补办吗 天猫618活动真的便宜吗 2021年天猫618预售便宜还是618便宜 Codeforces Round #245 (Div. 2)-C. Xor-tree 图|LeetCode 133. Clone Graph(克隆图) #|数据结构之图的存储结构(邻接表法) 前端|实现一个通用的可视化中间件需要怎么做? POJ1272小希的迷宫(并查集+map)