算法提高 学霸的迷宫
来源:互联网 发布:詹姆斯2011总决赛数据 编辑:程序博客网 时间:2024/05/23 12:01
问题描述
学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
输入格式
第一行两个整数n, m,为迷宫的长宽。
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
第一行一个数为需要的最少步数K。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
样例输入
Input Sample 1:
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
样例输出
Output Sample 1:
4
RDRD
Output Sample 2:
4
DDRR
4
RDRD
Output Sample 2:
4
DDRR
数据规模和约定
有20%的数据满足:1<=n,m<=10
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
经典bfs
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>#include <ctype.h>#include <algorithm>#include <map>#include <math.h>#include <stack>#include <queue>#define Max 510#define inf 100000000using namespace std;int vis[Max][Max],r,l;int Mov[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};char str[5] = "UDLR",m[Max][Max],step[Max];struct Node{ int x; int y; int s; char sp;}path[Max][Max];int BFS(){ Node now; memset(vis, 0, sizeof(vis)); now.x = now.y =0; path[now.x][now.y].s = 0; path[now.x][now.y].sp = 0; queue<Node>q; q.push(now); while (q.size()>0) { now = q.front(); if (now.x == r-1 && now.y == l-1) { break; } q.pop(); Node next = now; //printf("now:x %d y %d step %d straight %c\n",now.x,now.y,path[now.x][now.y].s,path[now.x][now.y].sp); for (int i=0; i<4; i++) { next.x = now.x+Mov[i][0]; next.y = now.y+Mov[i][1]; if (!vis[next.x][next.y]&&(next.x>=0&&next.x<r&&next.y>=0&&next.y<l)&& m[next.x][next.y] != '1' ) { vis[next.x][next.y] = 1; path[next.x][next.y] = now; path[next.x][next.y].sp = str[i]; path[next.x][next.y].s = path[now.x][now.y].s+1; q.push(next); // printf("next:x %d y %d step %d straight %c\n",next.x,next.y,path[next.x][next.y].s,path[next.x][next.y].sp); } } } //printf("%d\n",path[r-1][l-1].s); return path[r-1][l-1].s;}int main(){ while (scanf("%d%d",&r,&l)!=EOF) { for (int i=0; i<r; i++) { scanf("%s",m[i]); } printf("%d\n",BFS()); int len = 0; int nx = r-1,ny = l-1; int nextx = nx,nexty = ny; while (1) { nx = nextx; ny = nexty; if (nx + ny == 0) { break; } step[len++] = path[nx][ny].sp; nextx = path[nx][ny].x; nexty = path[nx][ny].y; } for (int i=len-1; i>=0; i--) { printf("%c",step[i]); } printf("\n"); } return 0;}
0 0
- 蓝桥杯 算法提高 学霸的迷宫
- 蓝桥杯算法提高 -- 学霸的迷宫
- 算法提高 学霸的迷宫
- 蓝桥网算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫 蓝桥杯
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法提高 学霸的迷宫
- 算法导论14.1-8
- 告警构件
- 搜索引擎的原理务必学习掌握
- Android Studio安装配置
- Hibernate N+1 问题及解决办法
- 算法提高 学霸的迷宫
- 【Lua学习笔记】 --> 《字符串string与table的函数操作》
- 角色权限管理
- Android Material Design II-自定义动画
- 百思学习笔记05-自定义tabBar
- 我的2015
- 在ubuntu上安装zeppelin
- Tomcat 7.0安装与配置
- HDU2005