USACO 1.4 Search Techniques
2016-02-16 23:14
483 查看
n皇后(DFS) 即任意两个皇后都不能处于同一行、同一列或同一斜线上
骑士周游问题(BFS) 走马步
Depth First with Iterative Deepening (IDDFS)
if the answer is expected to be in the leaves, use the simpler depth first search.
IDDFS(wiki)
原文
人家的分类
然后我发现wiki里面有一个搜索章节= =
题解是这么说的,枚举每个等差数列前两个判断
所以说,搜索的方法关键在于是否枚举对初始数值
- 好像发现自己被坑在哪了..
- bool数组get后要拿int数组来枚举啊亲= =
语法trick
- 在函数里面也可以用{ }初始化数组
dfs是肯定的,一开始拿六个变量写,然后发现太麻烦了(其实是写到最后一个变量发现dfs顺序很讨嫌,重构,就改成两个数组枚举,用3-a-b来表示剩下的那个
但是这样又有个问题,(写题解时候发现根本不是问题= =),就是状态三维数组表示不知道下标,就装逼写了一个hash..
然后test2坑了一下为0的情况,就是最后枚举时候last初始值不能设为0
hash
枚举
枚举
题解中的语法trick
骑士周游问题(BFS) 走马步
Depth First with Iterative Deepening (IDDFS)
if the answer is expected to be in the leaves, use the simpler depth first search.
IDDFS(wiki)
procedure IDDFS(root) for depth from 0 to ∞ found ← DLS(root, depth) if found ≠ null return found procedure DLS(node, depth) if depth = 0 and node is a goal return node else if depth > 0 foreach child of node found ← DLS(child, depth−1) if found ≠ null return found return null
Search | Time | Space | When to use | 路径 |
---|---|---|---|---|
DFS | O(ck) | O(k) | 完整搜索,或不需最小数 | X |
BFS | O(cd) | O(cd) | 答案浅,或需最小数 | √ |
DFS+ID | O(cd) | O(d) | 想BFS,空间不够 | √ |
人家的分类
然后我发现wiki里面有一个搜索章节= =
ariprog
查题解过的..罪过罪过题解是这么说的,枚举每个等差数列前两个判断
所以说,搜索的方法关键在于是否枚举对初始数值
- 好像发现自己被坑在哪了..
- bool数组get后要拿int数组来枚举啊亲= =
//快排算法 void quicksort (int array[], int start, int last) { int pivot; if (start < last) { pivot = pivotlist(array, start,last); quicksort (array, start,pivot-1); quicksort (array, pivot+1,last); } } int pivotlist(int array[], int f, int l) { int pivotpoint; int pivotvalue, temp; pivotvalue = array[f]; pivotpoint = f; for (int i = f+1;i<=l; i++) { if (array[i]<pivotvalue) { pivotpoint++; temp = array[i]; array[i] = array[pivotpoint]; array[pivotpoint] = temp; } } temp = array[f]; array[f] = array[pivotpoint]; array[pivotpoint] = temp; return pivotpoint; }
语法trick
- 在函数里面也可以用{ }初始化数组
倒牛奶问题
三个杯子,已知容量,最后一个装满,求当第一个空时候最后一个可能取值dfs是肯定的,一开始拿六个变量写,然后发现太麻烦了(其实是写到最后一个变量发现dfs顺序很讨嫌,重构,就改成两个数组枚举,用3-a-b来表示剩下的那个
但是这样又有个问题,(写题解时候发现根本不是问题= =),就是状态三维数组表示不知道下标,就装逼写了一个hash..
然后test2坑了一下为0的情况,就是最后枚举时候last初始值不能设为0
hash
int trans(int *ori){ return ori[0]+ori[1]*21; }
枚举
void dfs(int *nleft){ vis[trans(nleft)]=true; if (nleft[0]==0) {//这里一开始懵逼,以为b满也可以 vs[nleft[2]]=true; } //a=>b int t[3]; for (int a=0; a<3; a++) for (int b=0; b<3; b++) if (nleft[a]&&a!=b&&nleft[b]<bottle[b]) { t[a]=0,t[b]=nleft[b]+nleft[a],t[3-a-b]=nleft[3-a-b];//这里一次倒完,思路比if里面判断要清晰很多 if (t[b]<=bottle[b]) { if (vis[trans(t)]==false) { dfs(t); } }else { t[a]=t[b]-bottle[b]; t[b]=bottle[b]; if (vis[trans(t)]==false) { dfs(t); } } } }
枚举
int last=-1; for (int i=0; i<maxn; i++) { if (vs[i]) { if (last>=0) { fout<<last<<" "; last=i; }else last=i; } } fout<<last<<endl;
题解中的语法trick
State state(int a, int b, int c) { State s; s.a[0] = a; s.a[1] = b; s.a[2] = c; return s; }
State pour(State s, int from, int to) { int amt; amt = s.a[from]; if(s.a[to]+amt > cap[to]) amt = cap[to] - s.a[to]; s.a[from] -= amt; s.a[to] += amt; return s; }
相关文章推荐
- iOS开发UI篇—UIWindow简单介绍
- Building Maintainable Software-java篇之Automate Tests
- Hqueue Distributed Simulations(Houdini分机解算)
- UIPopoverController的使用
- Winform的ListBox的ValueMember和DisplayMember绑定的名称所属对象必须是Public的。
- DuiVision设计器预览版
- Building Maintainable Software-java篇之Keep Your Codebase Small
- java 反编译利器JD-JUI
- QuickFix/J 源代码
- Java进阶08 GUI
- question - Could not find Developer Disk Image
- Qt调用Ui文件
- 如何配置QuickFIX/N
- UI进阶第一天(给大家拜个晚年)
- QuickFIX/N 动态存储配置信息
- [LeetCode] Increasing Triplet Subsequence
- Static table views are only valid when embedded in UITableViewController instances
- Codeforces 626A Robot Sequence
- 数据库连接池的配置问题-空闲线程的监控和回收. druid 1.8的一个bug
- Arduino代码机制-Serial上