Raise questions set brush camp DAY 1 pm

DFS

Depth-first search

By searching to get a tree

Strategy: as long as the point of discovery did not come, it will come. There are more points to go to pick one, if nowhere to go back to back, look there's no point to go through.

Looking for the path on the map [minority] can be used to solve the shortest path: the shortest path can not solve sequential, which is related through this road before ignition if the right side of the path, it can only be found deep

Recursive form to solve the problem

There aftereffect of choice

Combinatorial problem

State may be a lot, so the data is generally small range

1, represents the state

2, pruning

       Pruning method:

              Best answer pruning

              Memory of pruning

              Feasibility pruning

              ……

 

1, the flood [1s 32M] 

answer

Small-scale data, search

 

 

 

 

 

 

 

2, hard old gardener [1s 128M] [1s 32M] 

 

 answer

FIG contravened find groups (simply, a group is complete subgraph of G)

FIG built originally entered can not have even edges, after anti FIG construction, even the sides of the can is communication between the two markers

Pruning:

1. If a point and i do not even current point edge, then after never even side, it can be deleted from the point i largest group of candidates

2. Use feasible point pruning sum + tot <= ans, Discard

3. The answer to the current use of pruning ans (2) + tot <= ans

(Tot is the group continues to accumulate weights, sum sequence which is the weight of everything and the rest of the

    If the sum + tot <= ans, discarded)

Probably the largest group of dots in a sequence beginning in

Remove a point, expanding, when the sequence is empty, over extended

Take out 1, then the sequence is updated to point 1 connected

Expanding until the queue is empty, looking for the complete group

 

 

 

 

 

 

 

 

 

 

 ---> + sum tot <= ans 

 

 

Maximum main function groups:

 

 Why look for the largest group instead of the maximum independent set it? ?

 

 

3, birthday cake [1s 10M] 

 

 answer

 

 

 

 

 

 

 

 

4, only the number of target-shaped [2s128M] 

 

 

 

 

 answer

 

 

 

 

 

 

 

 

 

5, chessboard [1s 16M] 

 

 answer

 

 

 

 

 

 

 


 

BFS 

BFS

 

 

1, Room Escape [1s 64M] 

 

 answer

queue<pair<int,int> > Q;
int FindPath(pair<int,int> b,pair<int,int> e) {
    for (int i=0;i<n;++i) for (int j=0;j<m;++j) dis[i][j]=1e9+10;
    Q.push(b); dis[b.first][b.second]=0;
    while (!Q.empty()) {
        pair<int,int> u=Q.front(); Q.pop();
        int x=u.first,y=u.second;
        for (int i=0;i<4;++i) {
            int tx=x+dx[i],ty=y+dy[i];
            if (CoordValid(tx,ty) && mp[tx][ty]!=0 && dis[tx][ty]>dis[x][y]+1) {
                dis[tx][ty]=dis[x][y]+1;
                Q.push(make_pair(tx,ty));    
            }
        }
    }
    return dis[e.first][e.second];
}

 

 

2、Prime Path[ 1s 64M ] 

 

 

 题解

记录当前素数的值

每次选择一个位置,将其该改为另一个数

检查新的数是否是素数

 

 

 

 

 

3、拯救行动 [ 1s 64M ] 

 

 

 题解

 

 

(Python源码)

BFS部分

 

 

 

 

 

 

4、抓住那头牛 [ 1s 64M ] 

 

 

 题解

 

 

 

 

 

 BFS

 

 

 

 

双向BFS(DBFS)

 

 

 

对比BFS和DFS 

广搜一般用于状态表示比较简单求最优策略的问题l

优点:是一种完备策略,即只要问题有解,它就一定可以找到解。并且,广度优先搜索找到的解,还 一定是路径最短的解。

缺点:盲目性较大,尤其是当目标节点距初始节点较远时,将产生许多无用的节点,因此其搜索效率较低。需要保存所有扩展出的状态,占用的空间大

深搜几乎可以用于任何问题

只需要保存 从起始状态到当前状态路径上的节点

根据题目要求凭借自己的经验和对两个搜索的熟练程度做出选择

 

 


 

枚举

1、苹果消消乐 [ 1s 64MB] 

 

 

 题解

选择连续的香蕉时最优

枚举选择的香蕉起始位置,计算答案

 

 

 

2、Matrix  [ 2s  512MB ]  

 

 题解

 

 

 Check函数:

 

 

4、特殊密码锁[ 1s 64M ]

 

 

 题解

1、  已知,在首位状态固定后,后续的操作是确定的。

2、  只需要枚举首位是否按即可。

 

 

 

 

5、恼人的青蛙[ 2s 64M ] 

 

题解

①不是一条行走路径:只有两棵被踩踏的水稻;

②是一条行走路径,但不包括(2,6)上的水道;

③不是一条行走路径:虽然有3棵被踩踏的水稻,

但这三棵水稻之间的距离间隔不相等。

例如,图4的答案是7,

因为第6行上全部水稻恰好构成一条青蛙行走路径。

 

 

 

 

 

 

 题解

 

 

枚举主函数

 

 

 

枚举得到最大步数

 

 

 

重识枚举

枚举:基于已知信息的猜测,从可能的答案集合中枚举并验证

验证复杂度尽可能小

枚举范围尽可能小(利用条件缩小枚举空间)

选择合理的枚举顺序(正序,倒序)

枚举什么?

怎么枚举?

怎么减少枚举?

 

 


二进制枚举

5、熄灯问题[ 1s 64M ] 

 

 

 

 

 题解

 

 

 

 

 二进制枚举

推导最后一行

 

 

二进制枚举

二进制的枚举一般用以枚举集合

对集合的枚举涉及到不同的集合内部元素的选择

枚举子集

         for(int S1=S;S1!=0;S1=(S1-1)&S){

    S2=S^S1;

}

 

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/11617337.html