POJ 1059 Chutes and Ladders

1. 一下午就做了这么一道题,蛋疼的模拟题。刚开始没有考虑当超过100的时候怎么处理,一直WA,上网看解题报告才知道。。。

2. memset()函数的用法。只能初始化为0或者-1。当初始值为其他数时,是在每一位上是这个数(这句话没明白),记住只能把数组初始化为0或者-1就行了;

3. 此题属于模拟题,不得不说,模拟题的if语句就是多啊;


#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int step[100], map[101], turn[7], cur[7];
    int step_num, i, peo, sx, ex, again, ans, j;
    memset(step, 0, sizeof(step));
    i = 0;
    while (cin >> step[i])
    {
        if (step[i] == 0)
            break;
        i++;
    }
    step_num = i - 1;
    while(cin >> peo)
    {
        if (peo == 0)
            break;
        memset(map, 0, sizeof(map));
        while (cin >> sx >> ex)
        {
            if (sx == 0 && ex == 0)
                break;
            else map[sx] = ex;
        }
        while (cin >> again)
        {
            if (again == 0)
                break;
            else if (again > 0)
                map[again] = -1;//重新扔一次
            else map[-again] = -2;//下次不扔
        }
        memset(turn, 0, sizeof(turn));//为0表示不turn
        memset(cur, 0, sizeof(cur));
        for (i = 0, ans = 0; i <= step_num; i++)
        {
            if (!turn[ans])
            {
                j = cur[ans] + step[i];
                if (j == 100)
                    break;
                if (j > 100)
                {
                    ans = (ans + 1) % peo;
                    continue;
                }
                if (map[j] == 0)
                {
                    cur[ans] = j;
                    ans = (ans + 1) % peo;
                    continue;
                }
                if (map[j] > 0)
                {
                    cur[ans] = map[j];
                    ans = (ans + 1) % peo;
                    continue;
                }
                if (map[j] == -1)
                {
                    cur[ans] = j;
                    continue;
                }
                if (map[j] == -2)
                {
                    cur[ans] = j;
                    turn[ans] = 1;
//                    continue;
                }
            }
            else
                turn[ans] = 0;
            ans = (ans + 1) % peo;
        }
        cout << ans + 1 << endl;
    }
    return 0;
}


原文地址:https://www.cnblogs.com/dollarzhaole/p/3188931.html