poj3414 pots(经典搜索题目, 题目本身不难,处理有点繁琐)
来源:互联网 发布:mac装win10网卡驱动 编辑:程序博客网 时间:2024/05/09 20:09
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int a, b, c;int vist[105][105];struct node { int x, y; int id; int op; int num; int pre;}s[100005];int cnt;node cur , next;node judge(node cur, int k) { //6种状态的处理代码; if(k == 1) cur.x = 0; else if(k == 2) cur.y = 0; else if(k == 3) cur.x = a; else if(k == 4) { cur.y = b; }else if(k == 5) { if(b - cur.y < cur.x) { cur.x = cur.x - (b - cur.y); cur.y = b; }else { cur.y += cur.x; cur.x = 0; } }else { if(a - cur.x < cur.y) { cur.y = cur.y - (a - cur.x); cur.x = a; }else { cur.x += cur.y; cur.y = 0; } } return cur;}int dfs(int ta, int tb) { queue<node>que; s[0].id = 0; s[0].x = ta; s[0].y = tb; s[0].num = 0; s[0].op = 0; s[0].pre = 0; cnt++; vist[ta][tb] = 1; que.push(s[0]); while(!que.empty()) { next = que.front(); que.pop(); // printf("%d %d %d %d\n", next.x, next.y, next.pre, next.id); if(next.x == c || next.y == c) { return next.id; } for(int i = 1; i <= 6; i++) { // printf("%d %d, %d\n", cur.x, cur.y, i); cur = judge(next, i); // printf("h %d %d, %d\n", cur.x, cur.y, i); if(!vist[cur.x][cur.y]) { // printf("hh %d %d\n", cur.x, cur.y); s[cnt].pre = next.id; s[cnt].id = cnt; s[cnt].x = cur.x; s[cnt].y = cur.y; s[cnt].num = cur.num + 1; s[cnt++].op = i; que.push(s[cnt-1]); vist[cur.x][cur.y] = 1; } } } return -1;}void put(int i) { //处理的6个状态对应的操作输出。 if(i == 1) { printf("DROP(1)\n"); }else if(i == 2) { printf("DROP(2)\n"); }else if(i == 3) { printf("FILL(1)\n"); }else if(i == 4) { printf("FILL(2)\n"); }else if(i == 5) { printf("POUR(1,2)\n"); }else { printf("POUR(2,1)\n"); } }void output(int k) { //递归输出结果, 即反推回去; if(s[k].pre == 0) { put(s[k].op); }else{ output(s[k].pre); put(s[k].op); }}int main(){ while(scanf("%d%d%d", &a, &b, &c) != EOF) { memset(vist, 0, sizeof(vist)); cnt = 0; int ans = dfs(0, 0); if(ans == -1) printf("impossible\n"); else { printf("%d\n", s[ans].num); output(ans); } } return 0;}
0 0
- poj3414 pots(经典搜索题目, 题目本身不难,处理有点繁琐)
- POJ3414,Pots,隐式图搜索
- poj3414 Pots(搜索)
- 【宽度优先搜索】poj3414 Pots
- POJ3414 - Pots - 广度优先搜索
- POJ3414(Pots)
- POJ3414 Pots(BFS)
- 【POJ3414】Pots(BFS)
- 二分搜索经典题目
- POJ3414 Pots
- poj3414 Pots
- poj3414 Pots
- poj3414 - Pots
- POJ3414--Pots
- POJ3414 Pots
- poj3414 Pots
- poj3414 Pots
- POj3414 Pots
- 程序员不可不知的eclipse操作快捷键
- 黑马程序员-IOS-OC基础-浅析Copy语法
- 权限系统收获——将一个窗体上的多条数据在另一个窗体上显示
- MFC程序设计 模拟手机通话管理系统
- java中的排序
- poj3414 pots(经典搜索题目, 题目本身不难,处理有点繁琐)
- 机器人常用软件
- 黑马程序员_学习笔记第10天——异常
- 线性表之循环链表
- Linux安装vsftp
- 数据库操作2
- xe6 IDE 新功能 新变化
- 移动互联网时代: 妈,我回不去了
- 【android初学日志】Test run failed: Test run incomplete. Expected 1 tests, received 0<二>