[uva] 10067 - Playing with Wheels
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 falseint 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;}// mainint 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的更多相关文章
- uva10067 Playing with Wheels 【建图+最短路】
题目:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1008">uva10067 Play ...
- UVA 1482 - Playing With Stones(SG打表规律)
UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...
- UVa - 11283 - PLAYING BOGGLE
先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...
- uva 1482 - Playing With Stones
对于组合游戏的题: 首先把问题建模成NIM等经典的组合游戏模型: 然后打表找出,或者推出SG函数值: 最后再利用SG定理判断是否必胜必败状态: #include<cstdio> #defi ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- (转) Deep Reinforcement Learning: Playing a Racing Game
Byte Tank Posts Archive Deep Reinforcement Learning: Playing a Racing Game OCT 6TH, 2016 Agent playi ...
随机推荐
- Windbg双机调试环境配置(Windows7/Windows XP+VirtualBox/VMware+WDK7600)
简介:Windbg双机调试内核.驱动 下载软件: 下载Windbg(GRMWDK_EN_7600_1.ISO) 下载VirtualBox 5.2/VMware 12 一.安装WDK,这里要提一点的是D ...
- centos7安装串口终端kermit
1. 将usb转串口连接到PC上.通过dmesg命令可以查看USB转串口是否被PC识别.显示 ……attachec to ttyUSB0即被识别. 2. 安装kermit. [seif@cen ...
- node.js修改全局安装文件路径
在进行 node.js 的开发过程中,我们需要下载大量的依赖模块,为了不让 c 盘的东西太过于散乱,可以通过修改node的配置参数,来修改node依赖的下载路径. 步骤: ①创建两个文件夹:node_ ...
- atoi和stoi
vs环境下:stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则 ...
- ionic3 cordova 调取软键盘
应用场景,因为兼容ios,安卓问题,不能直接调用激活软键盘方法.只有在点击按钮时让input框自动获取焦点,激活软键盘.然后把input框定位在键盘上方,软键盘激活可以监听到键盘高度. 先下载keyb ...
- (转)CentOS7 搭建LVS+keepalived负载均衡(一)
原文:http://blog.csdn.net/u012852986/article/details/52386306 CentOS7 搭建LVS+keepalived负载均衡(一) CentOS7 ...
- 理解restful 架构 && RESTful API设计指南
restful是前端和后端接口中都会使用的设计思想. 网站即软件,我们也常说的webapp,这种互联网软件采用的是“客户端/服务器”模式,建立在分布式体系上. 网站开发,也可以完全采用软件开发的模式, ...
- 我的Python升级打怪之路【四】:Python之前的一些补充
字符串的格式化 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指定的key flags 可选,可供选择的值有: ...
- Vi/Vim命令壁纸图
下载地址 http://pan.baidu.com/s/1mtQdY
- c#各个版本的特性
现在unity2018.2已经支持c#7.2了 版本特性: https://www.cnblogs.com/zq20/p/6323205.html