A game-board consists of N fields placed around a circle. Fields are successively numbered from1 to N clockwise. In some fields there may be obstacles.
Player starts on a field marked with number 1. His goal is to reach a given field marked with number Z. The only way of moving is a clockwise jump of length K. The only restriction is that the fields the player may jump to should not contain any obstacle.
For example, if N = 13, K = 3 and Z = 9, the player can jump across the fields 1, 4, 7, 10, 13, 3, 6 and 9, reaching his goal under condition that none of these fields is occupied by an obstacle.
Your task is to write a program that finds the smallest possible number K.
Player starts on a field marked with number 1. His goal is to reach a given field marked with number Z. The only way of moving is a clockwise jump of length K. The only restriction is that the fields the player may jump to should not contain any obstacle.
For example, if N = 13, K = 3 and Z = 9, the player can jump across the fields 1, 4, 7, 10, 13, 3, 6 and 9, reaching his goal under condition that none of these fields is occupied by an obstacle.
Your task is to write a program that finds the smallest possible number K.
First line of the input consists of integers N, Z and M, 2 <= N <= 1000, 2 <= Z <= N, 0 <= M <= N - 2. N represents number of fields on the game-board and Z is a given goal-field.
Next line consists of M different integers that represent marks of fields having an obstacle. It is confirmed that fields marked 1 and Z do not contain an obstacle.
Next line consists of M different integers that represent marks of fields having an obstacle. It is confirmed that fields marked 1 and Z do not contain an obstacle.
Output a line contains the requested number K described above.
9 7 2 2 3
3
题解:
题意:
有n个格子围成的圈,开始从1开始,每次跳k个格子,要到达m处,有z个障碍,然后输入障碍下标,即障碍处不能走,让你求最小的k能到达目标处
思路:
看到数据范围那么小,直接暴力搜索,走到以前走过的或者是障碍格子就退出,搜下一个
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
#define ll long long
#define INF 100861111;
using namespace std;
int n,m,z,tag,flag;
int vis[1005];
void dfs(int index,int k)/当前下标为index,每次k步
{
if(index==m||tag)//如果成功
{
tag=1;
return;
}
if(flag)//如果走了重复的格子直接退出
return;
int i;
if(!vis[index])
{
vis[index]=1;
if(index+k>n)//处理下循环
{
dfs((index+k)%n,k);
}
else
dfs(index+k,k);
vis[index]=0;//回溯
}
else
flag=1;
}
int main()
{
int i,j,k,x;
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&n,&m,&z);
for(i=1;i<=z;i++)
{
scanf("%d",&x);
vis[x]=1;
}
tag=0;
for(i=1;i<=n;i++)
{
flag=0;
dfs(1,i);
if(tag)
{
printf("%d\n",i);
break;
}
}
return 0;
}