题目描述:

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

中文大意:

使用两个锅,盛取定量水。

两个锅的容量和目标水量由用户输入。

允许的操作有:灌满锅、倒光锅内的水、一个锅中的水倒入另一个锅。

在两个锅互相倒水的过程中,若一个锅被倒满,而原来的锅中还有水,则剩余在原来的锅中。

不断执行这三个过程,直到任意一个锅中,贮存了目标数量的水,过程结束。

思路:

队列节点记录的是当前两个锅的贮水量和之前的一系列操作。

在弹出队列首节点,获取了当前两个锅的水量信息后,后续怎么操作,有 6 种选择:FILL(1)、FILL(2)、DROP(1)、DROP(2)、POUR(1,2)、POUR(2,1)。

注意:记得声明一个 visited[105][105] 数组,用来记录当前的情况是否出现过,若没有出现过,再将其压入队列,否则会超时。

代码:

#include<iostream>
#include<queue>
#include<string>
using namespace std; int a,b,c; struct node{
int x,y;
vector<string> msg;
node(int x, int y, vector<string> msg){
this->x = x;
this->y = y;
this->msg = msg;
};
node(int x, int y){
this->x = x;
this->y = y;
};
}; bool visited[105][105] = {false}; void bfs(){
node start = node(0, 0);
node next = node(0, 0); visited[0][0] = true; queue<node> q;
q.push(start); while(!q.empty()){
start = q.front();
q.pop(); if(start.x == c || start.y == c){
int n = start.msg.size();
printf("%d\n", n); for(int i=0;i<n;i++){
cout<<start.msg[i]<<endl;
}
return;
} //FILL(1)
next = node(a, start.y, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("FILL(1)");
q.push(next);
} //FILL(2)
next = node(start.x, b, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("FILL(2)");
q.push(next);
} //DROP(1)
next = node(0, start.y, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("DROP(1)");
q.push(next);
} //DROP(2)
next = node(start.x, 0, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("DROP(2)");
q.push(next);
} //POUR(1,2)
int fill_num = b - start.y;
if(start.x >= fill_num){
next = node(start.x - fill_num, b, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(1,2)");
q.push(next);
}
}
else{
next = node(0, start.y + start.x, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(1,2)");
q.push(next);
}
} //POUR(2,1)
fill_num = a - start.x;
if(start.y >= fill_num){
next = node(a, start.y - fill_num, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(2,1)");
q.push(next);
}
}
else{
next = node(start.x + start.y, 0, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(2,1)");
q.push(next);
}
}
}
printf("impossible\n");
} int main(){
scanf("%d %d %d", &a, &b, &c);
bfs();
}

【BFS】hdu 1973 Prime Path的更多相关文章

  1. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  2. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  3. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  5. 【BFS】HDU 1495

    直达–> HDU 1495 非常可乐 相似题联动–>POJ 3414 Pots 题意:中文题,不解释. 思路:三个杯子倒来倒去,最后能让其中两个平分即可.可能性六种.判定的时候注意第三个杯 ...

  6. 【CF653G】Move by Prime 组合数

    [CF653G]Move by Prime 题意:给你一个长度为n的数列$a_i$,你可以进行任意次操作:将其中一个数乘上或者除以一个质数.使得最终所有数相同,并使得操作数尽可能小.现在我们想要知道$ ...

  7. 【题解】HDU Homework(倍增)

    [题解]HDU Homework(倍增) 矩阵题一定要多多检查一下是否行列反了... 一百个递推项一定要存101个 说多了都是泪啊 一下午就做了这一道题因为实在是太菜了太久没写这种矩阵的题目... 设 ...

  8. 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...

  9. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. post传参数 传json格式参数

    如下: const dataObject = JSON.stringify({                                         "base64str" ...

  2. Nginx+FFmpeg实现RTSP转RTMP

    RTSP转RTMP 本次转流采用Centos+Nginx+FFmpeg实现,具体实现如下: 1. 安装Ngxin 安装详细略(可以选择安装阿里的Tengine,官方[下载路径](Download - ...

  3. Tomcat 配置Vue history模式

    Tomcat 配置Vue  history模式 近日 , 在使用 Tomcat 部署Vue项目时 , 刷新项目出现404的异常 . 原因是 Vue使用了history模式 , 而tomcat没有相关配 ...

  4. IPC 经典问题:Reader & Writer Problem

    完整代码实现: #include <stdio.h> #include <unistd.h> #include <time.h> #include <stdl ...

  5. linux线程库

    linux 提供两个线程库,Linux Threads 和新的原生的POSIX线程库(NPTL),linux threads在某些情况下仍然使用,但现在的发行版已经切换到NPTL,并且大部分应用已经不 ...

  6. Java 安全之Weblogic 2018-2628&2018-2893分析

    Java 安全之Weblogic 2018-2628&2018-2893分析 0x00 前言 续上一个weblogic T3协议的反序列化漏洞接着分析该补丁的绕过方式,根据weblogic的补 ...

  7. Golang应用性能问题排查分析

    背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...

  8. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  9. gRPC-go源码(1):连接管理

    1 写在前面 在这个系列的文章中,我们将会从源码的层面学习和理解gRPC. 整个系列的文章的计划大概是这样的:我们会先从客户端开始,沿着调用路径逐步分析到服务端,以模块为粒度进行学习,考虑这个模块是为 ...

  10. SQLHelper ------ python实现

    SQLHelper ------ python实现 1.第一种: import pymysql import threading from DBUtils.PooledDB import Pooled ...