JZOJ 5793. 【NOIP2008模拟】小S练跑步
5793. 【NOIP2008模拟】小S练跑步
(File IO): input:run.in output:run.out
Description
但不久后,他就开始为在重复的地点练习感到厌烦了,他就打算去B公园跑步。
但是小S由于没有去过B公园,他不知道B公园是否适合练习跑步,又不知道在B公园怎样跑是最优的。所以小S就去B公园进行了一番勘测。
小S在进行了一番勘测后,画出了一张地图,地图每一个位置上都辨识了小S到达该位置后不能往哪一个方位移动。其中有5种表示的符号:“U”代表不能向上移动,“D”代表不能向下移动,“L”代表不能向左移动,“R”代表不能向右移动,如果该位置有障碍物,小S到达那里后无法继续训练,就用“S”来代表。整个公园共有n行m列,小S会从第1行第1列出发,到第n行第m列结束他的练习。
现在小S想要知道,从起点(即第1行第1列)到终点(第n行第m列),途中最少要改变几次方向(即上一次移动的方向和这一次移动的方向不一样)?
注意:小S如在训练途中离开公园(即地图范围),则算是结束训练。
Input
第2~n+1行,每行有m个字符,表示小S的移动方向。
Output
Sample Input
3 3
ULL
LDU
SUD
Sample Output
1 【样例解释】
小S先向右移动移动了2格,再向下移动2格,就到达了终点,这样只用改变一次方向。
Data Constraint
10%的数据是题目的馈赠。
30%的数据,1≤n,m≤10。
50%的数据,1≤n,m≤50。
70%的数据,1≤n,m≤250。
100%的数据,1≤n,m≤500.
其中50%的数据是先构造出路径,再构造地图的。
100%数据是随机构造的。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define M 507
using namespace std;
int n, m, map[M][M], dis[M][M][], list[][];
string s;
int dy[] = {, , , -, };
int dx[] = {, -, , , }; void init()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
cin >> s;
for (int j = ; j < m; j++)
{
if (s[j] == 'U') map[i][j + ] = ;
if (s[j] == 'D') map[i][j + ] = ;
if (s[j] == 'L') map[i][j + ] = ;
if (s[j] == 'R') map[i][j + ] = ;
if (s[j] == 'S') map[i][j + ] = ;
}
}
} bool check(int x, int y, int w, int to)
{
if (map[x][y] == &&(x != n || y != m)) return ;
if (x > n || x < ) return ;
if (y > m || y < ) return ;
if (to >= dis[x][y][w]) return ;
if (to >= dis[x][y][] + ) return ;
dis[x][y][] = min(dis[x][y][], to);
dis[x][y][w] = to;
return ;
} void Bfs()
{
int head = , tail = ;
memset(dis, 0x7f7f7f7f, sizeof(dis));
dis[][][] = ;
dis[][][] = ;
dis[][][] = ;
dis[][][] = ;
for (int i = ; i <= ; i++)
if (map[][] != i)
{
list[++tail][] = ;
list[tail][] = ;
list[tail][] = i;
}
while (head < tail)
{
int u = list[++head][], v = list[head][], now = list[head][], val = list[head][];
for (int i = ; i <= ; i++)
if (map[u][v] != i)
{
int ain_u = u + dx[i], ain_v = v + dy[i];
int W = val;
if (now != i) W++;
if (!check(ain_u, ain_v, i, W)) continue;
list[++tail][] = ain_u;
list[tail][] = ain_v;
list[tail][] = i;
list[tail][] = W;
}
}
int ans = 0x7f7f7f7f;
for (int i = ; i <= ; i++)
ans = min(ans, dis[n][m][i]);
if (ans == 0x7f7f7f7f) printf("No Solution");
else printf("%d", ans);
} int main()
{
freopen("run.in", "r", stdin);
freopen("run.out", "w", stdout);
init();
Bfs();
}
JZOJ 5793. 【NOIP2008模拟】小S练跑步的更多相关文章
- JZOJ 5777. 【NOIP2008模拟】小x玩游戏
5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms Memory Limits ...
- JZOJ 5776. 【NOIP2008模拟】小x游世界树
5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms Me ...
- JZOJ 5775. 【NOIP2008模拟】农夫约的假期
5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms Memory Lim ...
- JZOJ 5773. 【NOIP2008模拟】简单数学题
5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms Memory Limits ...
- JZOJ5776. 【NOIP2008模拟】小x游世界树
题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...
- JZOJ 5809. 【NOIP2008模拟】数羊
5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms Memory Limits: ...
- JZOJ 5791. 【NOIP2008模拟】阶乘
5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms Memory ...
- JZOJ 5771. 【NOIP2008模拟】遨游
5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms Memory Limits: 2 ...
- 「SCOI2015」小凸想跑步 解题报告
「SCOI2015」小凸想跑步 最开始以为和多边形的重心有关,后来发现多边形的重心没啥好玩的性质 实际上你把面积小于的不等式列出来,发现是一次的,那么就可以半平面交了 Code: #include & ...
随机推荐
- POJ 2796:Feel Good 单调栈
题目,给定一个数列,n <= 1e5 .要求找出一个区间,使得其内区间最小值 * 区间总和的值最大,要求输出区间. 首先先维护一个单调递增的栈,同时记录一个lef值表示:lef[i]表示当前栈内 ...
- Linux上的errno和strerror
部分内容参考:https://www.douban.com/note/165931644/ 在Linux的api中: errno 是记录系统的最后一次错误代码.代码是一个int型的值,在errno.h ...
- 最简实例演示asp.net5中用户认证和授权(4)
上篇: 最简实例演示asp.net5中用户认证和授权(3) 上面我们把自定义认证和授权的相关的最小基础类和要实现的接口都实现了,下面就是如何来进行认证和授权的配置. 首先我们要告诉系统,我们的用户和角 ...
- SpringMVC10 InitBinder 注册自定义编辑器
1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...
- POI 博客总结.....
主键类: HSSFRow row1= sheet.createRow(1); row.createCell(0).setCellValue("学生编号"); row.createC ...
- hibernate课程 初探单表映射3-1 hibernate单表操作简介
本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例
- 初始socket
一.客户端/服务器架构 1.C/S结构,即Client/Server(客户端/服务器)结构 2.我们在互联网中处处可见c/s架构比如说浏览器,qq,lol,视频软件... 3.我们学习socket就是 ...
- redis---安全设置
redis的安全性是通过设置口令来实现的. 首先打开redis的配置文件,我的是在/etc/redis/redis.conf,个人的路径可能会有不同,可以自行查找. 打开redis.conf文件以后, ...
- EF ObjectQuery查询及方法
string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID lim ...
- SharePoint Survey – Custom Action
<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://sc ...