Maze(for lab)
来源:http://eden.sysu.edu.cn/m/ass/6275/
You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And you can only walk in four directions:up, down,left, right.
There will only be 5 kinds of characters in the maze.The meaning of each is as followed.
“#” is for rock, which you can not walk through.
“S” is for start.
“E” is for end.
“.” is for road.
“!” is for magma(岩浆),which you can not walk through.
Input
n,m represent the maze is a nxm maze.(n rows, m columnsm,0
#####
#S..#
#.!.#
#.#E#
#####
Output
You need to give the least steps to walk from start to the end.If it doesn’t exist, then output -1.
e.g.(for the example in input)
4
第一次的提交,用map来储存深度
#include <iostream>
#include <queue>
#include <map>
struct point {
int x;
int y;
point(int _x = 0, int _y = 0): x(_x), y(_y) {}
bool operator< (point another) {
return x <= another.x;
}
void operator= (point another) {
x = another.x;
y = another.y;
}
};
struct classcomp {
bool operator() (point a, point b) {
return a.x <= b.x;
}
};
using namespace std;
int not_get = -1;
void step(int x, int y);
char _map[22][22] = {{0}};
int main() {
int n_row, m_col = 0;
int target_x, target_y = 0;
queue<point> que;
map<point, int, classcomp> dist;
cin >> n_row >> m_col;
for (int i = 0; i < n_row; i++) {
for (int j = 0; j < m_col; j++) {
cin >> _map[i][j];
if ('E' == _map[i][j]) {
target_x = i;
target_y = j;
}
if ('!' == _map[i][j]) {
_map[i][j] = '#';
}
}
}
que.push(point(target_x, target_y));
dist[point(target_x, target_y)] = 0;
point temp;
point temp2;
while (!que.empty()) {
temp = que.front();
que.pop();
if ('#' != _map[temp.x][temp.y + 1]) {
temp2 = point(temp.x, temp.y+1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << dist[temp] + 1 << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x][temp.y - 1]) {
temp2 = point(temp.x, temp.y-1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << dist[temp] + 1 << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x + 1][temp.y]) {
temp2 = point(temp.x+1, temp.y);
if (_map[temp2.x][temp2.y] == 'S') {
cout << dist[temp] + 1 << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x - 1][temp.y]) {
temp2 = point(temp.x-1, temp.y);
if (_map[temp2.x][temp2.y] == 'S') {
cout << dist[temp] + 1 << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
}
cout << not_get << endl;
return 0;
}
第一次提交用map来储存深度,超时了。
第二次改把深度放到结构体中一起储存,就是把
struct point {
int x;
int y;
point(int _x = 0, int _y = 0): x(_x), y(_y) {}
// bool operator< (point another) {
// return x <= another.x;
}
void operator= (point another) {
x = another.x;
y = another.y;
}
};
改为了
struct point {
int x;
int y;
int dep;
point(int _x = 0, int _y = 0, int _dep = 0): x(_x), y(_y), dep(_dep) {}
// bool operator< (point another) {
// return x <= another.x;
// }
void operator= (point another) {
x = another.x;
y = another.y;
dep = another.dep;
}
};
整体的代码变为:
#include <iostream>
#include <queue>
#include <map>
struct point {
int x;
int y;
int dep;
point(int _x = 0, int _y = 0, int _dep = 0): x(_x), y(_y), dep(_dep) {}
// bool operator< (point another) {
// return x <= another.x;
// }
void operator= (point another) {
x = another.x;
y = another.y;
dep = another.dep;
}
};
using namespace std;
int not_get = -1;
void step(int x, int y);
char _map[22][22] = {{0}};
int main() {
int n_row, m_col = 0;
int target_x, target_y = 0;
queue<point> que;
cin >> n_row >> m_col;
for (int i = 0; i < n_row; i++) {
for (int j = 0; j < m_col; j++) {
cin >> _map[i][j];
if ('E' == _map[i][j]) {
target_x = i;
target_y = j;
}
if ('!' == _map[i][j]) {
_map[i][j] = '#';
}
}
}
que.push(point(target_x, target_y, 0));
_map[target_x][target_y] = '#';
point temp;
point temp2;
while (!que.empty()) {
temp = que.front();
que.pop();
if ('#' != _map[temp.x][temp.y + 1]) {
temp2 = point(temp.x, temp.y+1, temp.dep + 1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << temp2.dep << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x][temp.y - 1]) {
temp2 = point(temp.x, temp.y-1, temp.dep + 1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << temp2.dep << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x + 1][temp.y]) {
temp2 = point(temp.x+1, temp.y, temp.dep + 1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << temp2.dep << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
if ('#' != _map[temp.x - 1][temp.y]) {
temp2 = point(temp.x-1, temp.y, temp.dep + 1);
if (_map[temp2.x][temp2.y] == 'S') {
cout << temp2.dep << endl;
return 0;
} else {
que.push(temp2);
_map[temp2.x][temp2.y] = '#';
}
}
}
cout << not_get << endl;
return 0;
}
完美地通过了所有测例。