POJ -3414-Pots
来源:互联网 发布:php相册源码 编辑:程序博客网 时间:2024/06/14 04:18
题意:
给你,两个容量分别为a,b的杯子,让你看需要几步可以得到c的水,不能的话,impossible,可以的话,输出步数,和路径
思路:
BFS,暴搜
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespace std;int a, b, c;struct node{ int step; int k; int n, m; int qb;} ls[1010],now, next;int vis[101][101];int ki[1010];void bfs(){ memset(vis,0,sizeof(vis)); queue<node> que; now.step = 0; now.k = 0; now.n = 0; now.m = 0; que.push(now); vis[0][0] = 1; int i; int ti = 0; while(!que.empty()) { now = que.front(); que.pop(); if(now.n == c || now.m == c) { printf("%d\n",now.step); int ko = now.step; i = 1; int c = now.qb; ki[0] = now.k; while(1) { ki[i++] = ls[c].k; if(i > ko) break; c = ls[c].qb; } for(i = ko-1; i >= 0; i--) { switch(ki[i]) { case 5: printf("FILL(1)\n");break; case 6: printf("FILL(2)\n");break; case 3: printf("POUR(1,2)\n");break; case 4: printf("POUR(2,1)\n");break; case 1: printf("DROP(1)\n");break; case 2: printf("DROP(2)\n");break; } } return ; } ls[++ti] = now; for(i = 1; i <= 6; i++) { if(i == 1) { next.n = 0; next.m = now.m; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 2) { next.n = now.n; next.m = 0; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 3) { if(now.n > b-now.m) { next.n = now.n-(b-now.m); next.m = b; } else { next.n = 0; next.m = now.n+now.m; } next.k = i; next.step = now.step+1; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 4) { if(now.m > a-now.n) { next.m = now.m-(a-now.n); next.n = a; } else { next.m = 0; next.n = now.m+now.n; } next.k = i; next.step = now.step+1; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 5) { next.n = a; next.m = now.m; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 6) { next.n = now.n; next.m = b; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } } } printf("impossible\n");}int main(){ while(~scanf("%d%d%d",&a,&b,&c)) { bfs(); } return 0;}
0 0
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- POJ-3414-Pots
- POJ 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- oracle实现查询每一个部门的员工工资排在前三的员工的基本信息详细举例
- CAS:Compare And Swap 无锁
- eclipse 搭建activiti5.17项目生成流程过程
- Intent(8.19)
- 九度OJ 题目1065:输出梯形
- POJ -3414-Pots
- 人体所需的7大营养素
- 自己平时长期积累的java资料可供大家学习
- 数据库读写分离和垂直分库、水平分表
- Windows环境下教你用Eclipse ADT 插件生成.h/.so文件,Java下调用JNI,轻松学习JNI
- Mac下安装from matplotlib import pyplot或者 import matplotlib.pyplot as plt报错
- UVA442矩阵乘法之求出多个矩阵相乘乘法的次数
- viewpager 分页请求数据库并展示
- PAT 1047. Student List for Course (25)