10067 - Playing with Wheels

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

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

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

在第63行加入

 if (u == target)
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\n", steps);
            else
                printf("-1\n");
        }
    }
 
    return 0;
}

[uva] 10067 - Playing with Wheels的更多相关文章

  1. uva10067 Playing with Wheels 【建图+最短路】

    题目:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1008">uva10067 Play ...

  2. UVA 1482 - Playing With Stones(SG打表规律)

    UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...

  3. UVa - 11283 - PLAYING BOGGLE

    先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...

  4. uva 1482 - Playing With Stones

    对于组合游戏的题: 首先把问题建模成NIM等经典的组合游戏模型: 然后打表找出,或者推出SG函数值: 最后再利用SG定理判断是否必胜必败状态: #include<cstdio> #defi ...

  5. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  6. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  7. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  8. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  9. (转) Deep Reinforcement Learning: Playing a Racing Game

    Byte Tank Posts Archive Deep Reinforcement Learning: Playing a Racing Game OCT 6TH, 2016 Agent playi ...

随机推荐

  1. x86/x64/AnyCPU之间的区别

    原文链接 http://blog.csdn.net/lordwish/article/details/52312015 x86操作系统 目标平台 程序类型 运行结果 x86 应用程序exe 在32位C ...

  2. dotnetCore增加MiddleWare的Run,Use Map MapThen四个扩展方法

    dotnetCore增加MiddleWare的Run,Use Map MapThen四个扩展方法 http://www.mamicode.com/info-detail-1439628.html

  3. lua table、ipairs/pairs基础知识

    1.什么是table? table是Lua最复杂最强大的数据结构,Lua本身并不是面向对象语言,但是对面向对象中毒比较深的程序员,可以借助table”完美”地模拟面向对象编程.最简单地,我们可以把ta ...

  4. Json 装 list<object>objectList

    List<MallGoodComment> mallGoodCommentList = JSONObject.parseArray(mallGoodComments, MallGoodCo ...

  5. WCF系列教程之初识WCF

    本随笔参考自WCF编程系列(一)初识WCF,纯属读书笔记,加深记忆. 1.简介:Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程 ...

  6. 关于reduce的使用方法

    var rowData=[ {data:4,date:'06',code:'cr_3',name:'桥吊3'}, {data:1,date:'03',code:'cr_1',name:'桥吊1'}, ...

  7. Mac开发利器之程序员编辑器MacVim学习总结(转)

    一.关于Vim   Emacs和Vim都是程序员专用编辑器,Emacs被称为神的编辑器,Vim则是编辑器之神.至于两者到底哪个更好用,网络上两大派系至今还争论不休.不过,相比之下,Emacs更加复杂, ...

  8. 用C语言实现Ping程序功能---转

    ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具.ping命令的工作原理是:向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文一模一样地传回给发送者,这 ...

  9. .NET环境下的DPAPI加密编程

    Windows的本地加密保护机制提供了简单的调用接口,密钥的生成.保护等事项一概由系统来处理,其编程接口称为DPAPI.这一加密保护机制的边界是用户登录帐户或者本地计算机系统,使用操作系统设定的加密处 ...

  10. 学习Python要知道哪些重要的库和工具

    本文转自:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具. ...