学霸的迷宫BFs

算法提高 学霸的迷宫  

 

问题描述

  学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。

输入格式

  第一行两个整数n, m,为迷宫的长宽。
  接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。

输出格式

  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。

样例输入

Input Sample 1:
3 3
001
100
110

Input Sample 2:
3 3
000
000
000

样例输出

Output Sample 1:
4
RDRD

Output Sample 2:
4
DDRR

数据规模和约定

  有20%的数据满足:1<=n,m<=10
  有50%的数据满足:1<=n,m<=50
  有100%的数据满足:1<=n,m<=500。

 

#include<cstdio>
#include<iostream>
#include<cstring> 
#include <queue>
using namespace std;
#define M 550
int n,m,ans; 
char mp[M][M];
int vis[M][M];
int dir[4][2] = {1,0,0,-1,0,1,-1,0};
char dirc[4] = {'D','L','R','U'}; //下 左右上 

struct node{
	int x;
	int y;	
	int step;
	string s;
	node(int xx,int yy,int stt,string ss){
		x = xx;
		y = yy;
		step = stt;
		s = ss;
	 } 
};

bool check(int x, int y){
	if(x < 1 || x > n || y< 1 || y > m || mp[x][y] == '1' || vis[x][y])
	return false;
	return true;	
}
 
queue<node> q;
 
void bfs(int x,int y)
{
	q.push(node(x,y,0,""));
	while(!q.empty()){
		node now = q.front();
		q.pop();
		if(now.x == n && now.y == m){
			cout<<now.step<<endl;
			
			cout<<now.s;
			return;		
		}
		for(int i = 0;i<4;++i){
			int nx = now.x + dir[i][0];
			int ny = now.y + dir[i][1];
			if(check(nx,ny)){
				vis[nx][ny] = 1;
				//printf("$#%d %d\n",nx,ny);
				q.push(node(nx,ny,now.step+1,now.s+dirc[i]));
			}
		}
		
	}
} 
 
int main ()
{
	memset(vis,0,sizeof(vis));
	cin>>n>>m;
	for(int i = 1;i<= n;++i){
		for(int j = 1;j <= m;++j){
			cin>>mp[i][j];
		}
	}
	vis[1][1] = 1;
	bfs(1,1);
	
	
    return 0;
}

 

Guess you like

Origin blog.csdn.net/intmain_S/article/details/90301247