给定一个 n×n 的二维数组,如下所示:
- 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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
数据保证至少存在一条从左上角走到右下角的路径。
第一行包含整数 n(0≤n≤1000)。
接下来 n 行,每行包含 n 个整数 0 或 1,表示迷宫。
输出从左上角到右下角的最短路线,如果答案不唯一,输出任意一条路径均可。
按顺序,每行输出一个路径中经过的单元格的坐标,左上角坐标为 (0,0),右下角坐标为 (n−1,n−1)。
- 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
- 0 0
- 1 0
- 2 0
- 2 1
- 2 2
- 2 3
- 2 4
- 3 4
- 4 4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
一开始用递归写的广搜,测试点通不过,问老师说递归不需要广搜哈哈,就改成不用递归的广搜了哈哈。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- const int Max = 1010;
-
- using namespace std;
-
- //结点结构
- typedef struct Point
- {
- int x, y;
- }point;
-
- //地图
- int maze[Max][Max];
- point path[Max][Max]; //走过的路径,每个位置记录他的父位置
- int visitied[Max][Max] = {0}; //看结点是否经过
-
- int n;
-
-
-
- //上下左右移动的数组,上下左右的顺序
- int xMove[4] = {-1, 1, 0, 0};
- int yMove[4] = { 0, 0,-1, 1};
-
-
- point start; // 起点的位置
- point endp; //终点的位置
-
- void bfs();
- void show();
- void showPath();
-
- int main()
- {
- //迷宫大小为n
- cin >> n ;
-
- //初始化迷宫、起点和中点,包一圈围墙好判断
- memset(maze, 1, sizeof(maze));
- start.x = 1; start.y = 1;
- endp.x = n; endp.y = n;
-
- //输入地图
- for(int i = 1; i <= n; i++)
- for(int j = 1; j <= n; j++)
- cin >> maze[i][j];
-
-
- //bfs解决问题
-
- visitied[1][1] = 1; //起始点设为走过
- path[1][1].x = -1; path[1][1].y = -1; //设出发点的父结点为-1,-1,用来做输出的停止
- bfs();
- show();
- //showPath(); //测试路径数组
-
- return 0;
- }
-
- void bfs()
- {
- queue
q; //存放能走的路,遍历完四个方向后出队 - point next; //下一个结点
- q.push(start); //将第一个结点范进队列
-
- //遍历所有队列的元素
- while(!q.empty())
- {
- for(int i = 0; i < 4; i++)
- {
- //设置下一个结点
- //cout<< "当前判断的是" << q.front().x << ' ' << q.front().y << "位置" << endl;
- next.x = q.front().x + xMove[i];
- next.y = q.front().y + yMove[i];
-
-
- //如果是0,并且没走过,入队
- if(maze[next.x][next.y] == 0 && visitied[next.x][next.y] == 0)
- {
- //cout<< "结点:" << next.x << ' ' << next.y << "位置可以走" << endl;
- q.push(next);
-
- visitied[next.x][next.y] = 1;
- //在路径path中,记录该节点的父元素位置
- path[next.x][next.y] = q.front();
-
- }
-
-
- }
-
- q.pop();
-
- //cout << d.x << ' ' << d.y << ' ' << d.t << ' ' << d.time << endl;
-
- }
-
- }
-
- void show()
- {
- //从迷宫终点往回找,并存进栈中
- point now, next;
- now.x = now.y = n;
- stack
s; - s.push(now);
-
- //now为起点时结束
- while(path[now.x][now.y].x != -1 && path[now.x][now.y].y != -1)
- {
- next = path[now.x][now.y]; //在path中找下一个位置的坐标next
- now = next; //入栈
- s.push(now);
- }
-
- //输出
- while(!s.empty())
- {
- now = s.top();
-
- cout << now.x-1 << ' ' << now.y-1 << endl;
- s.pop();
- }
- }
-
- //测试路径
- void showPath()
- {
- for(int i = 1 ;i <= n; i++)
- {
- for(int j = 1; j <= n; j++)
- {
- cout << '(' << path[i][j].x << ' ' << path[i][j].y << ')' << ' ';
- }
- cout << endl;
- }
- }
- #include
- #include
- #include
- #include
- #include
-
- /*思路:从起点出发,每次判断上下左右的是否为0(通路),是的话将该条通路放进队列,
- 将所有通路放进队列后,再依次判断每个通路的上下左右是否为0。
- */
-
-
-
- using namespace std;
-
- //每个位置的坐标和它的父结点在队列中的位置
- struct Point
- {
- int x, y;
- int f;
- };
- void bfs(Point s); //广度优先遍历
- void show(Point q); //按照要求输出函数
- void show2();
-
- int n; //迷宫的行列
- int maze[1010][1010];//存放迷宫的二维数组
- int path = 0;
-
- Point que[1000010]; //队列,存放可以走的通路值
- int head = 0; //头指针
- int tail = 0; //尾指针
-
- Point resort[1000010]; //答案存放在这
-
- int main()
- {
- //按照要求输入
- cin >> n;
- if(n == 0)
- {
- cout << 0 << ' ' << 0 << endl;
- return 0;
- }
- for(int i = 0; i <= n+1; i++)
- {
- for(int j = 0; j <= n+1; j++)
- {
- maze[i][j] = 1;
- }
- }
-
- for(int i = 1; i <= n; i++)
- {
- for(int j = 1; j <= n; j++)
- {
- cin >> maze[i][j];
- }
- }
-
- //定义一个位置,用来当起点
- Point s;
- s.f = -1; s.x = 1; s.y = 1;
- //把起点先放进队列
- que[tail++] = s;
- //用起点来调用bfs算法
- bfs(s);
- //输出
- show(que[tail-1]);
- for(int i = path-1; i >= 0; i--)
- cout << resort[i].x-1 << ' ' << resort[i].y-1 << endl;
-
- //测试用
- /* show2();
- for(int i = 0; i <= n+1; i++)
- {
- for(int j = 0; j <= n+1; j++)
- {
- cout << maze[i][j] << ' ';
- }
- cout << endl;
- }*/
- }
-
- void bfs(Point s)
- {
- Point q; //用来存放每个改变的位置
- q.f = head; //当前判断的数是头结点
- maze[s.x][s.y] = 2; //该点设为走过
- int count = 0;
- //如果该点是终点,返回
- if(s.x == n && s.y == n)
- {
- return;
- }
-
- //如果上下左右的节点是0,放进队列,并且置该点为2
- //上
- q.x = s.x-1; q.y = s.y;
- if(maze[q.x][q.y] == 0)
- {
- //cout << "上" << endl;
- que[tail++] = q;
- count++;
- }
- //下
- q.x = s.x+1; q.y = s.y;
- if(maze[q.x][q.y] == 0)
- {
- //cout << "下" << endl;
- que[tail++] = q;
- count++;
- }
- //左
- q.x = s.x; q.y = s.y-1;
- if(maze[q.x][q.y] == 0)
- {
- //cout << "左" << endl;
- que[tail++] = q;
- count++;
- }
- //右
- q.x = s.x; q.y = s.y+1;
- if(maze[q.x][q.y] == 0)
- {
- //cout << "右" << endl;
- que[tail++] = q;
- count++;
- }
- //cout << "----------------------------------" <
- //再依次调用bfs
- head++;
- bfs(que[head]);
-
- }
-
- void show(Point q)
- {
- while(q.f != -1)
- {
- //cout << q.x-1 << ' ' << q.y-1 << ' ' << endl;
- resort[path++] = q;
- q = que[q.f];
-
- }
- if(q.f == -1)
- {
- //cout << q.x-1 << ' ' << q.y-1 << ' ' << endl;
- resort[path++] = q;
- }
-
- }
-
- void show2()
- {
- for(int i = 0; i < tail; i++)
- {
- cout << i << ": " << que[i].x-1 << ' ' << que[i].y-1 << ' ' << que[i].f << endl;
- }
- }
佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?

已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
- 4 4 1
- #@##
- **##
- ###+
- ****
6
- 4 4 2
- #@##
- **##
- ###+
- ****
4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
其实我是先做的这道题,然后用这道题的代码改成上一题的,话说鸣人的查克拉难道不是用不完的吗哈哈。
- #include
- #include
- #include
- #include
- #include
- #include
-
- const int Max = 210;
-
- using namespace std;
-
- //结点结构
- typedef struct Point
- {
- int x, y;
- int t; //查克拉
- int time; //花的时间
- }point;
-
- //地图
- char maze[Max][Max];
- point path[Max][Max]; //走过的路径,每个位置记录他的父位置
- int visitied[Max][Max] = {0}; //看结点是否经过
-
- int m, n, t;
-
-
-
- //上下左右移动的数组,上下左右的顺序
- int xMove[4] = {-1, 1, 0, 0};
- int yMove[4] = { 0, 0,-1, 1};
-
-
- point mingRen; // 名人的位置
- point zuoZhu; //佐助的位置
-
- int bfs();
-
- int main()
- {
- //行m,列n,名人查克拉t
- cin >> m >> n >> t;
-
- //初始化迷宫,包一圈围墙好判断
- memset(maze, 0, sizeof(maze));
- //初始化path,让没走过的为no
-
-
- //其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下
- for(int i = 1; i <= m; i++)
- for(int j = 1; j <= n; j++)
- {
- cin >> maze[i][j];
- //找到名人和佐助的位置
- if(maze[i][j] == '@')
- {
- mingRen.x = i;
- mingRen.y = j;
- }
- if(maze[i][j] == '+')
- {
- zuoZhu.x = i;
- zuoZhu.y = j;
- }
- }
-
- //bfs解决问题
- mingRen.t = t; //名人的查克拉
- mingRen.time = 0;//一开始的时间为0
- visitied[mingRen.x][mingRen.y] = 1; //起始点设为走过
- int resort = bfs();
- cout << resort;
- return 0;
- }
-
- int bfs()
- {
- queue
q; //存放能走的路,遍历完四个方向后出队 - point next; //下一个结点
- q.push(mingRen); //将第一个结点范进队列
-
- //遍历所有队列的元素
- while(!q.empty())
- {
- for(int i = 0; i < 4; i++)
- {
- //设置下一个结点
- next.x = q.front().x + xMove[i];
- next.y = q.front().y + yMove[i];
- next.t = q.front().t;
- next.time = q.front().time;
-
- //如果是*,并且没走过,则不需要小号查克拉,直接入队
- if(maze[next.x][next.y] == '*' && visitied[next.x][next.y] == 0)
- {
- next.time++;
- q.push(next);
- //cout << next.x << ' ' << next.y << ' ' << next.t << ' ' << next.time << endl;
- visitied[next.x][next.y] = 1;
- //在路径path中,记录该节点的父元素位置
- path[next.x][next.y] = mingRen;
-
- }
- //如果是#,大蛇丸手下,判断查克拉
- else if(maze[next.x][next.y] == '#' && visitied[next.x][next.y] == 0)
- {
- if(next.t > 0)
- {
- next.time++;
- //cout << " haha" << endl;
- next.t--; //消耗一点查克拉
- q.push(next);
- //cout << next.x << ' ' << next.y << ' ' << next.t << ' ' << next.time << endl;
- visitied[next.x][next.y] = 1;
- //在路径path中,记录该节点的父元素位置
- path[next.x][next.y] = mingRen;
- }
-
- }
- //如果是+,就是佐助,存入路径,结束调用
- else if(maze[next.x][next.y] == '+')
- {
- next.time++;
- q.push(next);
- path[next.x][next.y] = mingRen;
- return next.time;
- }
- }
- point d = q.front();
- q.pop();
-
- //cout << d.x << ' ' << d.y << ' ' << d.t << ' ' << d.time << endl;
-
- }
- //到这说明到不了佐助
- return -1;
-
-
- }
若对您有帮助麻烦点个赞哦~