华为机试题-猴子过桥

 

#include 
#include 

using namespace std;

#define MIN(x,y) ((x)<(y)?(x):(y))

int main()
{
    int L = 0;
    int SP = 0;
    int LP = 0;
    int M = 0;
    cin >> L;
    //while (cin >> L) {

    //}
    //cout << endl;
    cin >> SP >> LP >> M;
    int tp = 0;
    vector stone_num;
    for (int ix = 0; ix < M; ++ix)
    {
        cin >> tp;
        stone_num.push_back(tp);
    }


    vector stoneflag(L+1, false);
    vector stepnum(L+1, 0);
    vector stepflag(L + 1, false);
    for (int j = 0; j < M; ++j)
    {
        stoneflag[stone_num[j]] = true;
    }


    int tmp = 0;
    stepflag[0] = true;
    stepflag[SP] = true;
    stepflag[LP] = true;

    stepnum[2] = 1;
    stepnum[3] = 1;
    for (int i = LP+1; i <= L; ++i)
    {

        if (stepflag[i - SP] || (stepnum[i - LP]))
        {
            if (stepflag[i - SP] && stepnum[i - LP])
            {
                tmp = MIN(stepnum[i - SP], stepnum[i - LP]);
            }
            else
                if (stepflag[i - SP])
                {
                    tmp = stepnum[i - SP];
                }
                else
                    if (stepnum[i - LP])
                    {
                        tmp = stepnum[i - LP];
                    }

            if (stoneflag[i])
            {
                stepnum[i] = tmp + 1;
            }
            else
            {
                stepnum[i] = tmp;
            }

            stepflag[i] = true;            
        }
    }

    /*
    25
    2 3 9
    2 3 5 8 9 11 14 16 19
    */
    cout << stepnum[L]<

 

华为机试题-猴子过桥_第1张图片

你可能感兴趣的:(Leetcode)