poj 2657 Comfort

Comfort
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2500 Accepted: 859

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. 

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.

Output

Output a line contains the requested number K described above.

Sample Input

9 7 2
2 3

Sample Output

3

Source

Croatia OI 2002 National – Juniors


题意可以通过看input,output还有Description中的关键字,再结合案例演算一遍就可以很快地理解,之后的暴力代码也很快写出来了,但是交上去就超时了,为什么呢?经过学长的提醒,我写的代码对一些案例是会陷入死循环的,例如:6 4 1 2这组因为枚举到k==2时就一直在1 3 5 1 这个死循环里了,当然也就超时了,解决的办法是加一个visited数组来标记已访问过的,但是这里还要注意每次枚举k要初始化visited数组。


//Code:
#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
using namespace std;

bool visited[1005];

int main()
{
    //freopen("in.txt","r",stdin);
    int n,z,m;
    set<int> s;
    scanf("%d%d%d",&n,&z,&m);
    for(int i=0; i<m; i++)
    {
        int tp;
        scanf("%d",&tp);
        s.insert(tp);
    }
    for(int i=1;; i++)
    {
        memset(visited,false,sizeof(visited));
        for(int j=1;;)
        {
            if(j+i>n)
            {
                j+=i;
                j-=n;
            }
            else j+=i;
            if(s.count(j)||visited[j])
                break;
            visited[j]=true;
            if(j==z)
            {
                printf("%d\n",i);
                return 0;
            }
        }
    }
    return 0;
}




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

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值