#include
#include
#include
using namespace std;
int least_step(char (*p)[21], int n, int m);//深搜寻找最短路径
int main() {
	int n, m;
	char maze[21][21];
	for (int i = 0; i < 21; i++) {
		for (int j = 0; j < 21; j++) {
			maze[i][j] = ‘#‘;
		}
	}//初始化迷宫格子
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> maze[i][j];
		}
	}
	cout << least_step(maze, n, m) << endl;
	return 0;
}
int least_step(char (*p)[21], int n, int m) {
	static int len = 0;
	static priority_queue, greater > len_s;//存储路径.
	int i, j;
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			if (p[i][j]== ‘S‘)
				break;
		}
		if (p[i][j] == ‘S‘)
			break;
	}
	if (p[i + 1][j] == ‘E‘ && i + 1 < n) {
		len++; len_s.push(len); len--; return 1;
	}
	else if (p[i][j + 1] == ‘E‘ && j + 1 < m) {
		len++; len_s.push(len); len--; return 1;
	}
	else if (p[i][j - 1] == ‘E‘ && j - 1 > 0) {
		len++; len_s.push(len); len--; return 1;
	}
	else if (p[i - 1][j] == ‘E‘ && i - 1 > 0) {
		len++; len_s.push(len); len--; return 1;
	}//如果找到了终点就返回
	if (p[i + 1][j] == ‘.‘ && i + 1 < n) {
		p[i][j] = ‘#‘;
		p[i + 1][j] = ‘S‘;
		len++;
		least_step(p, n, m);
		p[i][j] = ‘S‘;
		p[i + 1][j] = ‘.‘;
		len--;
	}
	if (p[i][j + 1] == ‘.‘ && j + 1 < m) {
		p[i][j] = ‘#‘;
		p[i][j + 1] = ‘S‘;
		len++;
		least_step(p, n, m);
		p[i][j] = ‘S‘;
		p[i][j + 1] = ‘.‘;
		len--;
	}
	if (p[i][j - 1] == ‘.‘ && j - 1 > 0) {
		p[i][j] = ‘#‘;
		p[i][j - 1] = ‘S‘;
		len++;
		least_step(p, n, m);
		p[i][j] = ‘S‘;
		p[i][j - 1] = ‘.‘;
		len--;
	}
	if (p[i - 1][j] == ‘.‘ && i - 1 > 0) {
		p[i][j] = ‘#‘;
		p[i - 1][j] = ‘S‘;
		len++;
		least_step(p, n, m);
		p[i][j] = ‘S‘;
		p[i - 1][j] = ‘.‘;
		len--;
	}//按4个方向进行深搜
	if (len_s.size() != 0)
	return len_s.top();//最后找到最小的路径
	else return -1;
}
#include
#include
using namespace std;
int least_step(char (*p)[21], int n, int m);
struct path {
	path(int a, int b, int c) : x(a), y(b), floor(c) {}
	int x, y;
	int floor;//记录当前节点的层数
};
int main() {
	int n, m;
	char maze[21][21];
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> maze[i][j];
		}
	}
	cout << least_step(maze, n, m) << endl;
	return 0;
}
int least_step(char (*p)[21], int n, int m) {
	int step = 0, i = 0, j = 0, flag = 1;
	queue road;
	for (i = 0; i < n; i++) {
		for (j = 0; j < m; j++) {
			if (p[i][j] == ‘S‘) {
				flag = 0;//便于退出2重循环
				break;
			}
		}
		if (!flag) break;
	}
	road.push(path(i, j, 0));
	p[road.front().x][road.front().y] = ‘#‘;
	while (1) {
		if (p[road.front().x][road.front().y + 1] == ‘E‘ &&
			road.front().y + 1 < m) {
			step++;
			return step;
		}
		if (p[road.front().x + 1][road.front().y] == ‘E‘ &&
			road.front().x + 1 < n) {
			step++;
			return step;
		}
		if (p[road.front().x - 1][road.front().y] == ‘E‘ &&
			road.front().x - 1 > 0) {
			step++;
			return step;
		}
		if (p[road.front().x][road.front().y - 1] == ‘E‘ &&
			road.front().y - 1 > 0) {
			step++;
			return step;
		}//若搜索到终点,则返回步数
		if (p[road.front().x][road.front().y + 1] == ‘.‘ &&
			road.front().y + 1 < m) {
				p[road.front().x][road.front().y + 1] = ‘#‘;
				if (road.front().floor == step) {
					step++;
				}
			road.push(path(road.front().x, road.front().y + 1, step));
		}
		if (p[road.front().x + 1][road.front().y] == ‘.‘ &&
			road.front().x + 1 < n) {
			p[road.front().x + 1][road.front().y] = ‘#‘;
			if (road.front().floor == step) {
				step++;
			}
			road.push(path(road.front().x + 1, road.front().y, step));
		}
		if (p[road.front().x - 1][road.front().y] == ‘.‘ &&
			road.front().x - 1 > 0) {
			p[road.front().x - 1][road.front().y] = ‘#‘;
			if (road.front().floor == step) {
				step++;
			}
			road.push(path(road.front().x - 1, road.front().y, step));
		}
		if (p[road.front().x][road.front().y - 1] == ‘.‘ &&
			road.front().y - 1 > 0) {
			p[road.front().x][road.front().y - 1] = ‘#‘;
			if (road.front().floor == step) {
				step++;
			}
			road.push(path(road.front().x, road.front().y - 1, step));
		}//4个方向去按层寻找,之后同一层的节点不重复加,把找过的节点标志为墙,把 //合适的点压紧队列。
		road.pop();//搜过一个队头后弹出队列
		if (road.size() == 0) return -1;//发现队列中没有节点,说明每条路都走到了 //绝境,不能走下去了
	}
}
#include
#include
#include
#include
using namespace std;
struct Node {
int x;
int y;
int step;
Node(int x1, int y1, int step1) : x(x1), y(y1), step(step1) {}
Node() {}
} s, e;
int main() {
int n, m, sx, sy, ex, ey;
cin >> n;
cin >> m;
int i, j;
int xx, yy;
bool flag = -1;
Node node(0, 0, 0);
char c;
queue maza;
int point[100][100];
bool visit[100][100];
int d[4][2] = {
{0, 1} , {1, 0}, {0, -1}, {-1, 0}
};
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> c;
visit[i][j] = 0;
if (c == ‘S‘) {
sx = i;
sy = j;
point[i][j] = 1;
} else if (c == ‘E‘) {
ex = i;
ey = j;
point[i][j] = 1;
} else if(c == ‘!‘) {
point[i][j] = 0;
} else if (c == ‘#‘) {
point[i][j] = 0;
}
else point[i][j] = 1;
}
}
Node s(sx, sy, 0), e(ex, ey, 0);
maza.push(s);
while (!maza.empty()) {
node = maza.front();
maza.pop();
if (node.x == e.x && node.y == e.y) {
cout << node.step << endl;
flag = 0;
break;
}
visit[node.x][node.y] = 1;
for (i = 0; i < 4; i++) {
int x = node.x + d[i][0];
int y = node.y + d[i][1];
if (x >= 0 && y >= 0
&& visit[x][y] != 1 && point[x][y] != 0) {
Node next(x, y, node.step + 1);
visit[x][y] = 1;
maza.push(next);
}
}
}
if (flag != 0)
cout << -1 << endl;
}
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::queue;
int n, m;
class state {
private:
int s_x, s_y;
int e_x, e_y;
int step;
public:
state() {step = 0;}
state(int a, int b, int c, int d) {
s_x = a;
s_y = b;
e_x = c;
e_y = d;
step = 0;
}
bool ismeet();
bool isvaild();
friend void judge();
};
bool state::ismeet() {
if (s_x == e_x && s_y == e_y)
return true;
else
return false;
}
bool state::isvaild() {
if (s_x > 0 && s_x <= n && s_y > 0 && s_y <= m)
return true;
else
return false;
}
int s_x, s_y, e_x, e_y;
int mov[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char aa[21][21];
bool isvisit[21][21];
void judge();
int main() {
cin >> n >> m;
memset(isvisit, false, sizeof(isvisit));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> aa[i][j];
if (aa[i][j] == ‘S‘) {
s_x = i;
s_y = j;
aa[i][j] = ‘.‘;
}
if (aa[i][j] == ‘E‘) {
e_x = i;
e_y = j;
aa[i][j] = ‘.‘;
}
}
}
judge();
return 0;
}
void judge() {
queue vv;
vv.push(state(s_x, s_y, e_x, e_y));
state temp, hold;
while (!vv.empty()) {
temp = vv.front();
vv.pop();
for (inti = 0; i < 4; i++) {
hold.s_x = temp.s_x + mov[i][0];
hold.s_y = temp.s_y + mov[i][1];
hold.step = temp.step + 1;
hold.e_x = temp.e_x;
hold.e_y = temp.e_y;
if (hold.isvaild() && aa[hold.s_x][hold.s_y] == ‘.‘) {
if (!isvisit[hold.s_x][hold.s_y]) {
isvisit[hold.s_x][hold.s_y] = true;
if (hold.ismeet()) {
cout << hold.step << endl;
return;
} else {
vv.push(hold);
}
}
}
}
}
cout <<‘-1‘ <<endl;
}
 
原文:http://www.cnblogs.com/eggplant-is-me/p/6720058.html