5793. 【NOIP2008模拟】小S练跑步 
(File IO): input:run.in output:run.out

Time Limits: 2000 ms  Memory Limits: 524288 KB  Detailed Limits  

Goto ProblemSet

Description

       小S是一个爱锻炼的孩子,他在放假期间坚持在A公园练习跑步。
      但不久后,他就开始为在重复的地点练习感到厌烦了,他就打算去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

      第1行两个整数n和m,它们的定义请看【题目描述】。
      第2~n+1行,每行有m个字符,表示小S的移动方向。

Output

      如果小S从第1行第1列出发无论如何都到达不了第n行第m列,输出“No Solution”,否则输出小S途中最少要改变方向的次数。
 
 

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%数据是随机构造的。
 
做法:纯粹的暴力╮(╯_╰)╭,考验代码能力。。。
 
这里给出一个比较丑的打法,跑了1500ms,看到有人跑了50ms。。
代码如下:
 #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练跑步的更多相关文章

  1. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  2. JZOJ 5776. 【NOIP2008模拟】小x游世界树

    5776. [NOIP2008模拟]小x游世界树 (File IO): input:yggdrasil.in output:yggdrasil.out Time Limits: 1500 ms  Me ...

  3. JZOJ 5775. 【NOIP2008模拟】农夫约的假期

    5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms  Memory Lim ...

  4. JZOJ 5773. 【NOIP2008模拟】简单数学题

    5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms  Memory Limits ...

  5. JZOJ5776. 【NOIP2008模拟】小x游世界树

    题目:[NOIP2008模拟]小x游世界树: 题目的附加题解给的很清楚,这里只给一个代码: #include<iostream> #include<cstdio> #inclu ...

  6. JZOJ 5809. 【NOIP2008模拟】数羊

    5809. [NOIP2008模拟]数羊 (File IO): input:sheep.in output:sheep.out Time Limits: 1000 ms  Memory Limits: ...

  7. JZOJ 5791. 【NOIP2008模拟】阶乘

    5791. [NOIP2008模拟]阶乘 (File IO): input:factorial.in output:factorial.out Time Limits: 1000 ms  Memory ...

  8. JZOJ 5771. 【NOIP2008模拟】遨游

    5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms  Memory Limits: 2 ...

  9. 「SCOI2015」小凸想跑步 解题报告

    「SCOI2015」小凸想跑步 最开始以为和多边形的重心有关,后来发现多边形的重心没啥好玩的性质 实际上你把面积小于的不等式列出来,发现是一次的,那么就可以半平面交了 Code: #include & ...

随机推荐

  1. 四,JVM 自带工具之jvisualvm

    http://www.ibm.com/developerworks/cn/java/j-lo-visualvm/index.html?ca=drs- https://visualvm.java.net ...

  2. js电话号码校验

    var contact_phone = $("#contact_phone").val();        if(contact_phone && /^1[3|4| ...

  3. java Smaphore 控制并发线程数

    概念: Semaphore(信号量)是用来控制同事访问特定资源的线程数量,它通过协调各个线程,已保证合理的使用公共资源. 应用场景: Semaphore 可以用于做流量控制,特别是共用资源有限的应用场 ...

  4. asp.net MVC 4.0 Model元数据回顾——HtmlHelper的ModelMetadata

    模板方法包括Display/DisplayFor.Editor/EditorFor.DisplayForModel/EditForModel提供辅助生成Html的模型元数据信息 public stat ...

  5. Oracle同义词。。。

    同义词 --私有同义词--私有同义词权限grant create synonym to scott;--创建私有同义词create synonym dp for scott.dept;--将查询dep ...

  6. Hibernate基础案例1

    使用到的是MySQL数据库 1.在项目中先引入jar包,并添加引用 <dependencies> <dependency> <groupId>junit</g ...

  7. BZOJ3624: [Apio2008]免费道路(最小生成树)

    题意 题目链接 Sol 首先答案一定是一棵树 这棵树上有一些0边是必须要选的,我们先把他们找出来,如果数量$\geqslant k$显然无解 再考虑继续往里面加0的边,判断能否加到k条即可 具体做法是 ...

  8. css钻石旋转实现

    css钻石旋转实现: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  9. Retrofit2与RxJava用法大全

    Retrofit2是square公司出品的一个网络请求库,网上有很多相关的介绍.我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解Retrofit2与RxJava的 ...

  10. logback的configuration

    logback的<configuration>只有三个属性: 1.scan[boolean]:当scan被设置为true时,当配置文件发生改变,将会被重新加载.默认值为true. 2.sc ...