BFS——NYOJ 21 三个水杯

三个水杯

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
输入
第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
输出
每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
样例输入
2
6 3 1
4 1 1
9 3 2
7 1 1
样例输出
3
-1


唉...这道题写了好久啊!
简单的宽度优先搜索,三个水杯之间的相互倒水如下图6种情况:


对于每一次倒水都会引起三个水杯水量状态的改变,这样就可以得到如下的一个解空间树:


按照上图中得到解空间树的方法,代码如下:

[cpp]  view plain  copy
 print ?
  1. #include <cstdio>  
  2. #include <memory.h>  
  3. #include <queue>  
  4.   
  5. using namespace std;  
  6.   
  7. #define EMPTY    0  
  8.   
  9. struct data_type  
  10. {  
  11.     int state[3];  
  12.     int step;  
  13. };  
  14.   
  15. int cupCapacity[3], targetState[3];  
  16.   
  17. bool visited[100][100][100];  
  18.   
  19. bool AchieveTargetState(data_type current)  
  20. {  
  21.     for (int i = 0; i < 3; i++)  
  22.     {  
  23.         if (current.state[i] != targetState[i])  
  24.         {  
  25.             return false;  
  26.         }  
  27.     }  
  28.     return true;  
  29. }  
  30.   
  31. void PourWater(int destination, int source, data_type &cup)  
  32. {  
  33.     int waterYield = cupCapacity[destination] - cup.state[destination];  
  34.     if (cup.state[source] >= waterYield)  
  35.     {  
  36.         cup.state[destination] += waterYield;  
  37.         cup.state[source] -= waterYield;  
  38.     }  
  39.     else  
  40.     {  
  41.         cup.state[destination] += cup.state[source];  
  42.         cup.state[source] = 0;  
  43.     }  
  44. }  
  45.   
  46. int BFS(void)  
  47. {  
  48.     int i, j, k;  
  49.     data_type initial;  
  50.     queue<data_type> toExpandState;  
  51.   
  52.     memset(visited, falsesizeof(visited));  
  53.     initial.state[0] = cupCapacity[0];  
  54.     initial.state[1] = initial.state[2] = 0;  
  55.     initial.step = 0;  
  56.     toExpandState.push(initial);  
  57.     visited[initial.state[0]][0][0] = true;  
  58.   
  59.     while (!toExpandState.empty())  
  60.     {  
  61.         data_type node = toExpandState.front();  
  62.         toExpandState.pop();  
  63.         if (AchieveTargetState(node))  
  64.         {  
  65.             return node.step;  
  66.         }  
  67.         for (i = 0; i < 3; i++)  
  68.         {  
  69.             for (j = 1; j < 3; j++)  
  70.             {  
  71.                 k = (i+j)%3;  
  72.                 if (node.state[i] != EMPTY && node.state[k] < cupCapacity[k])  
  73.                 {  
  74.                     data_type newNode = node;  
  75.                     PourWater(k, i, newNode);  
  76.                     newNode.step = node.step + 1;  
  77.                     if (!visited[newNode.state[0]][newNode.state[1]][newNode.state[2]])  
  78.                     {  
  79.                         visited[newNode.state[0]][newNode.state[1]][newNode.state[2]] = true;  
  80.                         toExpandState.push(newNode);  
  81.                     }  
  82.                 }  
  83.             }  
  84.         }  
  85.     }  
  86.     return -1;  
  87. }  
  88.   
  89. int main(void)  
  90. {  
  91.     int testNum;  
  92.     scanf("%d", &testNum);  
  93.     while (testNum -- != 0)  
  94.     {  
  95.         scanf("%d%d%d", &cupCapacity[0], &cupCapacity[1], &cupCapacity[2]);  
  96.         scanf("%d%d%d", &targetState[0], &targetState[1], &targetState[2]);  
  97.         printf("%d\n", BFS());  
  98.     }  
  99.     return 0;  
  100. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DFS算法是一种用于图遍历或树遍历的算法。其核心思想是从起点开始递归地深入每一个可能的分支,直到无法继续为止,然后回溯到上一个节点,继续尝试其他分支。 DFS算法有两种实现方式:递归实现和非递归实现(使用栈)。 递归实现的DFS算法伪代码: ``` DFS(node): if node is None: return visit(node) for child in node.children: DFS(child) ``` 非递归实现的DFS算法伪代码: ``` DFS(node): stack = [node] while stack: node = stack.pop() if node.visited: continue visit(node) node.visited = True for child in node.children: stack.append(child) ``` 其中,visit(node)表示对节点node进行操作,例如打印节点值、记录路径等。 DFS算法的时间复杂度为O(V+E),其中V为节点数,E为边数。因为每个节点和每条边都只会被访问一次。 下面是一个例题,用DFS算法求解从起点到终点的路径(leetcode 79题): 给定一个二维网格和一个单词,找出该单词是否存在于网格中。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: ``` board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false. ``` 实现代码: ```python class Solution: def exist(self, board: List[List[str]], word: str) -> bool: if not board or not board[0]: return False m, n = len(board), len(board[0]) visited = [[False] * n for _ in range(m)] directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] def dfs(i, j, k): if k == len(word): return True if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] or board[i][j] != word[k]: return False visited[i][j] = True for dx, dy in directions: if dfs(i+dx, j+dy, k+1): return True visited[i][j] = False return False for i in range(m): for j in range(n): if dfs(i, j, 0): return True return False ``` 时间复杂度为O(m*n*3^k),其中m、n为网格大小,k为单词长度。因为每个格子都有可能走,所以是O(m*n),而每次调用dfs函数会向四个方向递归,每个方向都不能重复走,所以是3^k。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值