小城天长网站建设,wordpress评论等级,如何建设网站建设,wordpress点击显示微信二维码输入#xff1a; 思路#xff1a;
1、注意输入用字符串。
2、采用广度搜素的方法来求解。
3、因为最后要求字典序最小且DLRU,所以在遍历四个方向的时候#xff0c; 先向下#xff0c;再向左、右#xff0c;最后向上。
#includeiostream
#include…
输入 思路
1、注意输入用字符串。
2、采用广度搜素的方法来求解。
3、因为最后要求字典序最小且DLRU,所以在遍历四个方向的时候 先向下再向左、右最后向上。
#includeiostream
#includequeue
using namespace std;
int n, m;
string Map[500];
const int dire[4][2] { {1,0},{0,-1},{0,1},{-1,0} };//下左右上
const string d DLRU;
struct node
{int i, j;string ans;
};
queuenode q;
void BFS()
{node s;s.i 0, s.j 0; s.ans ;Map[0][0] 1;q.push(s);while (!q.empty()){node a q.front(), b;int x a.i;//该点坐标int y a.j;q.pop();if (x n - 1 y m - 1)//走到终点{cout a.ans.length() endl;//步数cout a.ans endl;break;//最先达到的即为在步数最小的情况下字典序最小}for (int i 0; i 4; i)//遍历四个方向{int newx x dire[i][0];int newy y dire[i][1];if (newx 0 || newy 0 || newx n || newy m)//越界跳过continue;if (Map[newx][newy] 0)//通路{b.ans a.ans d[i];//加上该次步骤b.i newx, b.j newy;Map[newx][newy] 1;//标识已经走过q.push(b);//进入队列}}}
}
int main()
{cin n m;for (int i 0; i n; i){cin Map[i];}BFS();// cout DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR;return 0;
}