SDNUOJ 1654.Treasure House (bfs+最短路径+记录路径)

Time Limit: 1000 MS Memory Limit: 32768 KB

Description
Occasionally, WWY finds a treasure map with size of .
This map consists of only two characters: ‘KaTeX parse error: Expected 'EOF', got '#' at position 8: ' and ‘#̲’. '’: An empty cells(WWY can walk on it).
‘#’: A cell full of rock(WWY cannot walk on it).
WWY now is in coordinates (top left corner) and the treasure house is in coordinates (lower right corner).
Every second, WWY can chose one direction from and fly one step towards it.
avatar
Can you help WWY find the path to the treasure house with the minimum seconds?

Input
The first line of input is an integer and an integer .
lines follwed, each line has characters .

Output
The output has two lines.
The first line prints the minimum seconds to arrive the treasure house.
The second line prints several characters for representing . If there are many paths with the same minimum seconds, please print the path with maximum dictionary.
It is guarantees that there must have a path for arriving the treasure house.

Sample Input
3 4
KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲$ #$
##$$

Sample Output
5
ESESE

Hint
Arriving from to , the minimum seconds is .
The paths with the minimum seconds are and , and is the maximum dictionary path with the minimum seconds.

总之这种问题bfs好写,关键是记录路径,可以开一个等大的结构体数组用来记录当前坐标的前一个坐标的数据以及从上一个坐标到这一个坐标所作的操作,以及到此坐标所走的步数,最后dfs一下输出路径

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int next[4][2]={{0,-1},{1,0},{-1,0},{0,1}}; //方向数组 
struct loc{
	int x;
	int y;
	int step; 
};    //记录现在的坐标,往队列里塞为做bfs做准备 
struct father{
	int x;
	int y;
	char cz;
	int step;
}lj[15][15]; //这个结构体用来记录当前坐标的前一个坐标的数据以及从上一个坐标到这一个坐标所作的操作,以及到此坐标所走的步数 
queue<loc> qu; //用来做bfs的队列 
stack<char> st; //用来输出路径的栈 
loc locc;
char map[15][15]; //地图 
bool vis[15][15]; //判断是否走过 
void dfs(int x,int y)
{
	if(x==0&&y==0)
	return;
	st.push(lj[x][y].cz);
	dfs(lj[x][y].x,lj[x][y].y);
}       //从终点dfs回去,用来输出路径 
int main()
{
	int xx,yy,n,m;
	bool flag=0;
	cin>>n>>m;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			cin>>map[i][j];
		}
	}
	if(n==1&&m==1)
	{
		cout<<0<<endl;
		flag=1;
	}  //一个特判 
	vis[1][1]=1;
	lj[1][1].x=0;
	lj[1][1].y=0;
	lj[1][1].step=0;
	locc.x=1;
	locc.y=1;
	locc.step=0;
	qu.push(locc);//各种初始化 
	while(!qu.empty())
	{
		for(int i=0;i<4;++i)
		{
			xx=qu.front().x+next[i][0];
			yy=qu.front().y+next[i][1];
			if(xx<1||xx>n||yy<1||yy>m)//判断是否越界 
			continue;
			if(xx==n&&yy==m)//到终点了 
			{
				lj[xx][yy].x=qu.front().x;
				lj[xx][yy].y=qu.front().y;
				lj[xx][yy].step=lj[qu.front().x][qu.front().y].step+1;
				if(i==0) lj[xx][yy].cz='W';
				else if(i==1) lj[xx][yy].cz='S';
				else if(i==2) lj[xx][yy].cz='N';
				else lj[xx][yy].cz='E'; //记录必要数据 
				dfs(n,m);
				cout<<lj[xx][yy].step<<endl; //输出最短路程的值 
				st.pop();
				while(!st.empty())
				{
					cout<<st.top();
					st.pop(); //输出路径 
				}
				cout<<endl;
				flag=1;
				break;
			}
			else if(!vis[xx][yy]&&map[xx][yy]=='$')
			{
				locc.x=xx;
				locc.y=yy;
				locc.step=qu.front().step+1;
				qu.push(locc);
				lj[xx][yy].x=qu.front().x;
				lj[xx][yy].y=qu.front().y;
				lj[xx][yy].step=lj[qu.front().x][qu.front().y].step+1;
				if(i==0) lj[xx][yy].cz='W';
				else if(i==1) lj[xx][yy].cz='S';
				else if(i==2) lj[xx][yy].cz='N';
				else lj[xx][yy].cz='E';//记录必要数据 
				vis[xx][yy]=1;//走过了 
			}
		}
		if(flag) break;
		qu.pop();//出队,下一个 
	}
	return 0;
}
/*
4
9
$$$$$$$#$
$$$$#$$$$
$$$$$$#$$
$#$###$$$
9
6
$$$$$$
$$#$$#
##$$$$
$$$$$$
$##$$$
$$$$$$
##$$$$
$$$$$$
$$$$$$
*/

路径长度用坐标结构体求好像有问题,所以还是用路径结构体求吧= =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值