USACO 1.4 Search Techniques

来源:互联网 发布:橱柜设计软件下载 编辑:程序博客网 时间:2024/05/27 01:14
  • 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)
procedure IDDFS(root)    for depth from 0 to ∞        found ← DLS(root, depth)        if found ≠ null            return foundprocedure 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;}
0 0
原创粉丝点击