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
$$$$$$
$$#$$#
##$$$$
$$$$$$
$##$$$
$$$$$$
##$$$$
$$$$$$
$$$$$$
*/
路径长度用坐标结构体求好像有问题,所以还是用路径结构体求吧= =