问题
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。Input一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。Output左上角到右下角的最短路径,格式如样例所示。Sample Input0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0Sample Output(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)
题解
(这个题不一定要使用BFS,但是为了练习,还是使用了)
#include"iostream"
#include"vector"
#include"queue"
#include"stack"
using namespace std;
struct node
{
int x;
int y;
int pre;
};
int m, n;
int head = 0; int tail = 1;
node start, EN;
int dis[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
vector< int > print_path(int tail, node* que)
{
vector< int > path_track;
int t = tail;
while (que[t].pre != -1)
{
path_track.insert(path_track.begin(),t);
t = que[t].pre;
}
path_track.insert(path_track.begin(), 0);
return path_track;
}
int BFS(vector<vector<int>> map, node* que)
{
node point;
point = start;
que[head] = point;
vector<vector<int>> flag(m, vector<int>(n, 0));
flag[point.x][point.y] = 1;
while (head<tail)
{
for (int i = 0; i<4; i++)
{
int tx = que[head].x + dis[i][0];
int ty = que[head].y + dis[i][1];
//cout << flag[0].size() << flag.size();
//cout << map[tx][ty] << flag[tx][ty];
if (tx<m && tx >= 0 && ty<n && ty >= 0 && map[tx][ty] == 0)
if (flag[tx][ty] == 0)
{
que[tail].x = tx;
que[tail].y = ty;
que[tail].pre = head;
flag[tx][ty] = 1;
if (tx == EN.x && ty == EN.y)
{
//此时head为终点的前面一个步
return tail;
}
tail++;
}
}
head++;
}
return 0;
}
int main()
{
while (cin >> m >> n)
{
node *que = new node[n*m];
head = 0; tail = 1;
start.x = 0; start.y = 0; start.pre = -1;
EN.x = m - 1; EN.y = n - 1; EN.pre = -1;
vector<vector<int>> map(m, vector<int>(n, 0));
for (int i = 0; i<m; i++){
for (int j = 0; j<n; j++)
{
cin >> map[i][j];
}
}
int tail = BFS(map, que);
vector<int> path_track = print_path(tail, que);
int ii = path_track[0];
while (ii<path_track.size())
{
cout << "(" << que[path_track[ii]].x << "," << que[path_track[ii]].y << ")" << "\n";
ii++;
}
delete []que;
}
return 0;
}
错点
错点超级多
- 尽量少定义全局变量,全局变量智能定义成那种永远为常量的值,其他的不要定义,要使用参数传递
- 注意存在while循环来出来多个case
- 输入多个case时,特别注意每次计算前该初始化的参数是否初始化,全局变量中是否存在要初始化的(最好不要定义全局变量)
- 如何输入多个case: https://www.nowcoder.com/discuss/276
- C 64位输出请用printf("%lld")
int main() {
int a,b;
while(scanf("%d %d",&a, &b) != EOF)//注意while处理多个case
printf("%d\n",a+b);
return 0;
}
- C++ 64位输出请用printf("%lld")
#include <iostream>
using namespace std;
int main() {
int a,b;
while(cin >> a >> b)//注意while处理多个case
cout << a+b << endl;
}
-
定义的
new int[n*m]
一定要在n 和m有值存在,所以不要定义为全局变量 -
数组形参传递,只要传递数组变量就行
回溯打印
#include"iostream"
#include"vector"
#include"queue"
#include"stack"
using namespace std;
struct node
{
int x;
int y;
int pre;
};
int m, n;
int head = 0; int tail = 1;
node start, EN;
int dis[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
// 打印, 回溯法输出最终结果
void print_path(int tail, node* que)
{
if (que[tail].pre != -1)
print_path(que[tail].pre, que);
cout << "(" << que[tail].x << "," << que[tail].y << ")" << "\n";
return;
}
int BFS(vector<vector<int>> map, node* que)
{
node point;
point = start;
que[head] = point;
vector<vector<int>> flag(m, vector<int>(n, 0));
flag[point.x][point.y] = 1;
while (head<tail)
{
for (int i = 0; i<4; i++)
{
int tx = que[head].x + dis[i][0];
int ty = que[head].y + dis[i][1];
//cout << flag[0].size() << flag.size();
//cout << map[tx][ty] << flag[tx][ty];
if (tx<m && tx >= 0 && ty<n && ty >= 0 && map[tx][ty] == 0)
if (flag[tx][ty] == 0)
{
que[tail].x = tx;
que[tail].y = ty;
que[tail].pre = head;
flag[tx][ty] = 1;
if (tx == EN.x && ty == EN.y)
{
//此时head为终点的前面一个步
return tail;
}
tail++;
}
}
head++;
}
return 0;
}
int main()
{
while (cin >> m >> n)
{
node *que = new node[n*m];
head = 0; tail = 1;
start.x = 0; start.y = 0; start.pre = -1;
EN.x = m - 1; EN.y = n - 1; EN.pre = -1;
vector<vector<int>> map(m, vector<int>(n, 0));
for (int i = 0; i<m; i++){
for (int j = 0; j<n; j++)
{
cin >> map[i][j];
}
}
int tail = BFS(map, que);
print_path(tail, que);
delete []que;
}
return 0;
}
来源:https://blog.csdn.net/zseqsc_asd/article/details/100019713