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

9 篇文章 0 订阅

## 回溯之生成排列

1.生成全排列

2.生成部分排列

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

3.生成圆排列

4.生成部分圆排列

5.重排列

### 费马小定理

p为质数，a不是p的倍数

a不是p的倍数，而a^(p-1)≡1(%p)

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;
}


### 关于剪枝

#### 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

#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}};
{
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)
{
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
评论
07-05 5万+
08-10 540
05-09 569
09-29 611
03-24 702
04-02 3932
08-08 671
04-27
08-25
10-30
09-19
05-21

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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