输入样例:10 10
#S######.#
…#…#
.#.##.##.#
.#…
##.##.####
…#…#
.#######.#
…#…
.####.###.
…#…G#
思路:bfs按照距开始状态由近及远的顺序搜索,适合求最短路路径、最少操作问题;这个问题中,状态只是位置坐标,可构造pair表达,当状态更复杂时,需要封装成结构体。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e2;
const int inf=1e8;//一般将inf设为放大2~4倍不会溢出的大小
typedef pair<int,int>P;//使用pair表示状态时,使用typedef更方便
char maze[maxn][maxn+1];
int N,M,d[maxn][maxn];//d为到各个位置最短距离的数组
int sx,sy,gx,gy;//起点坐标和终点坐标
int dx[4]