USACO Section 1.4 Mother's Milk 搜索
来源:互联网 发布:仙界网络直播间txt书包 编辑:程序博客网 时间:2024/06/05 18:28
这道题是一个让人做完觉得很爽的搜索题,可以用深搜也可以用宽搜。 相对来说,深搜的代码量稍微小一点。 搜索策略就是模拟倒来倒去的过程,并且出现重复的没有意义。
BFS版本: 我是人工写了个队列,这样比STL中的快一点
/*ID: sdj22251PROG: calfflacLANG: C++*/#include <iostream>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cmath>#include <ctime>#define MAX 2000000000#define LOCA#define M 1007using namespace std;int a, b, c;bool can[M];bool flag[M][M];struct wwj{ int x, y, z; wwj(){} wwj(int xx, int yy, int zz) { x = xx; y = yy; z = zz; }}q[M * M];void bfs(int x, int y, int z){ int head = 0, tail = 0; wwj A(x, y, z); q[tail++] = A; flag[x][y] = true; if(A.x == 0) can[A.z] = true; while(head < tail) { A = q[head++]; if(A.x == 0) can[A.z] = true; if(A.x < a && A.z > 0) { if(A.z > a - A.x && !flag[a][A.y]) {wwj B(a, A.y, A.z - a + A.x); flag[B.x][B.y] = true; q[tail++] = B;} if(A.z <= a - A.x && !flag[A.x + A.z][A.y]) {wwj B(A.x + A.z, A.y, 0); flag[B.x][B.y] = true; q[tail++] = B;} } if(A.y < b && A.x > 0) { if(A.x > b - A.y && !flag[A.x + A.y - b][b]) {wwj B(A.x + A.y - b, b, A.z); flag[B.x][B.y] = true; q[tail++] = B;} if(A.x <= b - A.y && !flag[0][A.x + A.y]) {wwj B(0, A.x + A.y, A.z); flag[B.x][B.y] = true; q[tail++] = B;} } if(A.z < c && A.y > 0) { if(A.y > c - A.z && !flag[A.x][A.y + A.z - c]) {wwj B(A.x, A.y + A.z - c, c); flag[B.x][B.y] = true; q[tail++] = B;} if(A.y <= c - A.z && !flag[A.x][0]) {wwj B(A.x, 0, A.z + A.y); flag[B.x][B.y] = true; q[tail++] = B;} } if(A.x > 0 && A.z < c) { if(A.x > c - A.z && !flag[A.x + A.z - c][A.y]) {wwj B(A.x + A.z - c, A.y, c);flag[B.x][B.y] = true; q[tail++] = B;} if(A.x <= c - A.z && !flag[0][A.y]) {wwj B(0, A.y, A.z + A.x);flag[B.x][B.y] = true; q[tail++] = B;} } if(A.y > 0 && A.x < a) { if(A.y > a - A.x && !flag[a][A.y + A.x - a]) {wwj B(a, A.y + A.x - a, A.z);flag[B.x][B.y] = true; q[tail++] = B;} if(A.y <= a - A.x && !flag[A.x + A.y][0]) {wwj B(A.x + A.y, 0, A.z);flag[B.x][B.y] = true; q[tail++] = B;} } if(A.z > 0 && A.y < b) { if(A.z > b - A.y && !flag[A.x][b]) {wwj B(A.x, b, A.z + A.y - b);flag[B.x][B.y] = true; q[tail++] = B;} if(A.z <= b - A.y && !flag[A.x][A.y + A.z]) {wwj B(A.x, A.y + A.z, 0);flag[B.x][B.y] = true; q[tail++] = B;} } }}int main(){#ifdef LOCAL freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout);#endif int i; scanf("%d%d%d", &a, &b, &c); bfs(0, 0, c); for(i = 0; i <= c; i++) if(can[i]) printf("%d ", i); printf("\n"); return 0;}DFS版本: 可以看到,步骤都很明确
/*ID: sdj22251PROG: calfflacLANG: C++*/#include <iostream>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cmath>#include <ctime>#define MAX 2000000000#define LOCA#define M 1007using namespace std;int a, b, c;bool can[M];bool flag[M][M];void dfs(int x, int y, int z){ if(flag[x][y]) return; flag[x][y] = true; if(x == 0) can[z] = true; if(x < a && z > 0) { if(z > a - x) dfs(a, y, z - a + x); if(z <= a - x) dfs(x + z, y, 0); } if(y < b && x > 0) { if(x > b - y) dfs(x + y - b, b, z); if(x <= b - y) dfs(0, x + y, z); } if(z < c && y > 0) { if(y > c - z) dfs(x, y + z - c, c); if(y <= c - z) dfs(x, 0, z + y); } if(x > 0 && z < c) { if(x > c - z) dfs(x + z - c, y, c); if(x <= c - z) dfs(0, y, z + x); } if(y > 0 && x < a) { if(y > a - x) dfs(a, y + x - a, z); if(y <= a - x) dfs(x + y, 0, z); } if(z > 0 && y < b) { if(z > b - y) dfs(x, b, z + y - b); if(z <= b - y) dfs(x, y + z, 0); }}int main(){#ifdef LOCAL freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout);#endif int i; scanf("%d%d%d", &a, &b, &c); dfs(0, 0, c); for(i = 0; i <= c; i++) if(can[i]) printf("%d ", i); printf("\n"); return 0;}
- USACO Section 1.4 Mother's Milk 搜索
- USACO-Section 1.4 Mother Milk[模拟][搜索]
- USACO section 1.4.4 Mother's Milk
- USACO Section 1.4 Mother's Milk
- USACO Section 1.4 Mother's Milk
- USACO Section 1.4 Mother's Milk
- 【USACO】Mother's Milk(搜索)
- USACO Section 1.4 Mother's Milk - 输出错了好几次..
- USACO Section 1.4 - Mother's Milk(DFS + 记忆化)
- USACO-Section 1.4 Mother's Milk(BFS)
- Section 1.4 Mother's Milk
- [usaco]1.4 Mother’s Milk
- USACO 1.4 Mother's Milk
- USACO 1.4 Mother's Milk
- USACO 1.4-Mother's Milk
- USACO 1.4 Mother's Milk
- USACO 1.4 Mother's Milk
- USCAO section Mother's Milk(搜索)
- hadoop 最小化运行
- 中国移动举行“移动生活卡”首发仪式
- JAVA设计模式-State
- flex组件生命周期对mxml和as的影响_悄悄俏俏
- 同方微电子3S理念护航金融IC卡
- USACO Section 1.4 Mother's Milk 搜索
- 29. A good conscience is a soft pillow. 不做亏心事,不怕鬼叫门
- 关于Windows程序的一些东西(断点、头文件)
- Cocoa Programming for Mac OS X 第四章(Memory Management)摘录
- 代码中标注的方法
- HtmlEditor (在线html编辑器)的使用
- 数据库获取字段长度的函数
- 数据库 - in 和exists
- oracle与sql server2000中不在同一个服务器上的数据库数据操作的方法对比