zoukankan      html  css  js  c++  java
  • [uva] 10067

    10067 - Playing with Wheels

    题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1008

    从一开始思路就不对,之后才焕然大悟……每次都是这样。

    还有,感觉搜索和图遍历有点分不清呢。

    在第63行加入

    1 if (u == target)
    2     return;

    可以提速很多,可以从300ms左右降低到100ms以内。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    #include <iostream>
    #include <queue>
     
    #define Dec true
    #define Inc false
     
    int  start;      // 开始数值
    int  target;     // 目标数值
    bool op[4];      // 4 个转盘的操作
    int  ban_number; // 禁止数的个数
    int  ban[10001]; // 记录禁止数的数组
     
    struct
    {
        int  parent;
        bool used;
    } buffer[10001]; // 顶点
     
    // 增大数值
    void decrease(int current[], int i)
    {
        current[i]--;
     
        if (current[i] == -1)
            current[i] = 9;
    }
     
    // 减小数值
    void increase(int current[], int i)
    {
        current[i]++;
     
        if (current[i] == 10)
            current[i] = 0;
    }
     
    // 广度优先搜索
    void bfs()
    {
        std::queue<int> q;
     
        // 初始化顶点
        for (int i = 0; i < 10000; i++)
        {
            buffer[i].parent = -1;
            buffer[i].used = false;
        }
     
        // 把禁止数标记为已发现
        for (int i = 0; i < ban_number; i++)
        {
            buffer[ban[i]].used = true;
        }
     
        // 初始化开始节点
        buffer[start].used = true;
        q.push(start);
     
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
     
            int depart[4];
            int a = u / 1000;
            int b = (u - a*1000) / 100;
            int c = (u - a*1000 - b *100) / 10;
            int d = (u - a*1000 - b *100 - c *10);
            depart[0] = a;
            depart[1] = b;
            depart[2] = c;
            depart[3] = d;
     
            for (int i = 0; i < 4; i++)
            {
                decrease(depart, i);
                {
                    int x = depart[0] * 1000 + depart[1] * 100 + depart[2] * 10 + depart[3];
                    if (buffer[x].used == false)
                    {
                        buffer[x].parent = u;
                        buffer[x].used = true;
                        q.push(x);
                    }
                }
                increase(depart, i);
     
                increase(depart, i);
                {
                    int x = depart[0] * 1000 + depart[1] * 100 + depart[2] * 10 + depart[3];
                    if (buffer[x].used == false)
                    {
                        buffer[x].parent = u;
                        buffer[x].used = true;
                        q.push(x);
                    }
                }
                decrease(depart, i);
            }
        }
    }
     
    // 读取四个个位数组成一个四位数,并返回这个四位数
    int read4()
    {
        int a, b, c, d;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        return a * 1000 + b *100 + c * 10 + d;
    }
     
    // main
    int main()
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            start  = read4();
            target = read4();
     
            scanf("%d", &ban_number);
            for (int j = 0; j < ban_number; j++)
            {
                ban[j] = read4();
            }
     
            bfs();
     
            {
                int x = target;
                int steps = 0;
                while(x != -1 && x != start)
                {
                    steps++;
                    x = buffer[x].parent;
                }
     
                if (x == start)
                    printf("%d ", steps);
                else
                    printf("-1 ");
            }
        }
     
        return 0;
    }
  • 相关阅读:
    Spring boot mybatis : Error creating bean with name 'com.github.pagehelper.autoconfigure.MapperAutoConfiguration': Invocation of init method failed;
    方法调用
    初识MQ
    Shell 变量
    Eclipse 导入本地 Git 项目
    IDEA 常用快捷键
    Xshell 配色方案
    冒泡排序
    递归
    安卓服务Service详解
  • 原文地址:https://www.cnblogs.com/night-ride-depart/p/5061567.html
Copyright © 2011-2022 走看看