回溯笔记+费马小定理+剪枝

回溯

搜索树 考回溯问题,心中要有一棵树
回溯问题最要注意的就是操作完成一定要恢复原状

回溯之生成排列

排列

遇上需要逐个枚举的题时,需要用递归生成排列。几种题型如下
1.生成全排列

递归变量为深度,每层递归有一层循环选取当前可选的数字。用vis数组记录数字是否用过。

2.生成部分排列

l类似全排列,递归边界改一下

3.生成圆排列

圆排列:123 231相等 。共有 n!/n=(n-1)! 用1打头生成全排列即可

4.生成部分圆排列

5.重排列

重排列中的元素有重复,去掉vis数组即可,不用考虑元素是否出现过。


组合

在1~N里选取m个数

从小到大枚举即可,后数不小于前数


费马小定理

p为质数,a不是p的倍数
则a^(p-1)≡1(%p)
反过来
a不是p的倍数,而a^(p-1)≡1(%p)
则p大概率是质数

适用于判断大质数
可以选取多个a进行验证,a一般选取2到p-1的数,2+rand()%(p-2)
判断四次即可
另外要先写个快速幂

bool is_prime(int p)
{
	if(p==2)
		return 1;
	int t=4;
	long long a;
	while(t--)
	{
		a=2+rand()%(p-2);
		if(qpow(a,p-1,p)!=1)
			return 0;
	}
	return 1;
}

关于剪枝

题目会卡数据,有时某个剪枝没剪会T。

贪心类剪枝 如 01背包问题中,不要它不够就必须要他
对称性剪枝 剪了这个是不是另一个对应的也可以剪
奇偶性剪枝 (01剪枝) 比如地图上01交替,从0只能到达1。从0到0的时间是偶数,从0到1的时间是奇数,据此剪枝 (dx+dy) ^( tx+ty)^(t)=0,奇偶性不随时间变化,
所以可以一开始判断一次。
乐观估计、悲观估计 如估算到达终点的最小时间和最长时间,据此剪枝。
顺序考虑 考虑搜索顺序 某些找到答案即可的题目,要考虑先选取哪个有可能是答案。

例题

Tempter of the Bone

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.

The input is terminated with three 0’s. This test case is not to be processed.
Output
For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.
Sample Input
4 4 5
S.X.
…X.
…XD

3 4 5
S.X.
…X.
…D
0 0 0
Sample Output
NO
YES
题目大意有个地图,给你起点终点,走过的地方不能走,问能不能刚好在T时刻到达终点
剪枝
首先,根据题意剪枝,如果时间到达却没有到达终点,剪掉。
然后,乐观估计、悲观估计剪枝。乐观估计到达终点所需的最小步数,如果大于T,剪掉
悲观估计把所有剩余格子都走完,到达终点所要的最长时间,如果小于T,剪掉。
最后,01剪枝。如果把地图标成01交替的样子,那么x+y为奇,则此处应为1,x+y为偶,地图此处为0。从0到1需要奇数的T,从0到0需要偶数的T。据此判断T是否符合要求。因为奇偶性不变,所以只用检测一次即可,不合要求直接退出。
代码如下

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int map[10][10],n,m;
int P,T;
int sx,sy,ex,ey;
const int dir[5][2]={{0,-1},{0,1},{-1,0},{1,0}};
bool read()
{
    char str[10];
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
            map[i][j]=-1;
    P=0;
    scanf("%d%d%d",&n,&m,&T);
    if(!n&&!m&&!T)
        return 0;
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        for(int j=0;j<m;j++)
        {
            if(str[j]=='S')
            {
                sx=i;
                sy=j;
                ++P;
            }
            else if(str[j]=='D')
            {
                ex=i;
                ey=j;
                ++P;
                map[i][j]=0;
            }
            else if(str[j]=='.')
            {
                map[i][j]=0;
                ++P;
            }
        }
    }
    return 1;
} 
void printmap()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            if(map[i][j]==0)
                cout<<".";
            else if(map[i][j]==-1)
                cout<<"X";
        else    printf("%d",map[i][j]);
        cout<<endl;
    }
}
int evamin(int x,int y,int now)
{
    return abs(ex-x)+abs(ey-y)+now;
}
int evamax(int p)
{
    return P-p;
}
bool check()
{
    //cout<<((sx^sy^ex^ey^T)&1)<<endl;
//    printf("%d %d %d %d\n",sx,sy,ex,ey);
    return (sx^sy^ex^ey^T)&1;
}
bool dfs(int x,int y,int now,int p)
{
//    cout<<"DFS  "<<x<<","<<y<<":"<<now<<" "<<p<<endl;
//    printmap();cout<<endl;
    
    if(x==ex&&y==ey&&now==T)
        return 1;
    if(x==ex&&y==ey&&now!=T)
        return 0;
    if(now>=T)
        return 0;
    if(evamin(x,y,now)>T)
        return 0;
    if(evamax(p)<T-now)
        return 0;
    int xx,yy;
    for(int i=0;i<4;i++)
    {
        xx=x+dir[i][0];
        yy=y+dir[i][1];
        if(xx<0||yy<0||xx>=n||yy>=m||map[xx][yy]!=0)
            continue;
        map[xx][yy]=1;
        if(dfs(xx,yy,now+1,p+1))
            return 1;
        map[xx][yy]=0;
    }
    return 0;
}
int main()
{
    while(1)
    {
        if(!read())
            break;
        if(check())
        {
            printf("NO\n");
            continue;
        }
        if(dfs(sx,sy,0,1))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;    
}

本题很好的体现了几种剪枝思想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值