Comfort
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2476 | Accepted: 847 |
Description
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.
Input
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
Output a line contains the requested number K described above.
Sample Input
9 7 2 2 3
Sample Output
3
Source
注意如果直到出现循环仍然没有走到Z,则说明该K值不合法。
#include <cstdio>
#include <cstring>
bool stepped[1010];
bool obs[1010];
int main()
{
freopen("2657.in","r",stdin);
freopen("2657.out","w",stdout);
int n,z,m;
while (scanf("%d%d%d",&n,&z,&m) == 3)
{
memset(obs,0,sizeof obs);
z --;
for (int i=1;i<=m;i++)
{
int tmp;
scanf("%d",&tmp);
tmp --;
obs[tmp] = true;
}
bool ok = false;
for (int t=1;t<=z&&!ok;t++)
{
for (int i=0;i<n;i++)
stepped[i] = false;
for (int ii=t;!stepped[ii%n]&&!obs[ii%n];ii+=t)
{
stepped[ii%n] = true;
if (ii%n == z)
{
ok = true;
printf("%d\n",t);
break;
}
}
}
}
return 0;
}