poj3984 迷宫问题 bfs
来源:互联网 发布:珠海联邦制药知乎 编辑:程序博客网 时间:2024/06/14 01:50
链接:http://poj.org/problem?id=3984
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16884 Accepted: 10073
Description
定义一个二维数组:
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0
Sample Output
(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)
比赛题,但是曹喆学长暑假已经讲过一次了,而且以为内上课也没有比赛,暂且把之前敲得贴上来吧
bfs,要求打印路径,其实用一个数组存就好
代码:
#define _CRT_SBCURE_MO_DEPRECATE#include<iostream>#include<stdlib.h>#include<stdio.h>#include<cmath>#include<algorithm>#include<string>#include<string.h>#include<set>#include<queue>#include<stack>#include<functional> using namespace std;const int MAXN = 100000 + 10;int n, sx, sy, ex, ey;int vis[305][305];int dirt[8][2] = { { 1,0 },{ 0, 1 },{ -1,0 },{ 0, -1 } };int x[10][10];//迷宫struct node {int x;int y;int j;int step[105][3];};int judge(int x, int y) {if (x < 0 || x>5 || y < 0 || y>5) return 1;else return vis[x][y];}void bfs() {sx = 0; sy = 0;ex = 4; ey = 4;memset(vis, 0, sizeof(vis));queue<node>q;node a, b, next;a.x = sx;a.y = sy;a.j = 0;a.step[0][0] = a.x;a.step[0][1] = a.y;//cout << sx << sy;q.push(a);vis[sx][sy] = 1;while (!q.empty()) {b = q.front();q.pop();if (b.x == ex&&b.y == ey) {for (int k = 0; k < b.j; k++)cout << "(" << b.step[k][0] << ", " << b.step[k][1] << ")" << endl;cout << "(4, 4)" << endl;}for (int i = 0; i < 4; i++) {next = b;next.x = b.x + dirt[i][0];next.y = b.y + dirt[i][1];if (judge(next.x, next.y) == 1)continue;if (x[next.x][next.y] == 1)continue;//p判断不是墙(next.j)++;next.step[next.j][0] = next.x;next.step[next.j][1] = next.y;q.push(next);vis[next.x][next.y] = 1;}}}int main(){for (int i = 0; i<5; i++) {scanf("%d %d %d %d %d", &x[i][0], &x[i][1], &x[i][2], &x[i][3], &x[i][4]);}bfs();//system("pause");return 0;}
0 0
- POJ3984 迷宫问题 BFS
- POJ3984 迷宫问题 BFS
- poj3984 迷宫问题(BFS)
- POJ3984 迷宫问题【BFS】
- poj3984迷宫问题(bfs)
- POJ3984 迷宫问题【BFS】
- poj3984 迷宫问题 bfs
- POJ3984 迷宫问题【BFS】
- POJ3984 迷宫问题(BFS)
- poj3984迷宫问题 bfs
- poj3984 迷宫问题(BFS)
- POJ3984 迷宫问题【水BFS】
- POJ3984 迷宫问题(BFS)
- poj3984-迷宫问题(BFS)
- POJ3984 迷宫问题(BFS)
- poj3984(bfs迷宫问题)
- poj3984 迷宫问题 bfs水题
- poj3984 迷宫问题(图-BFS)
- py-faster-rcnn配置运行faster_rcnn_end2end—VGG_CNN_M_1024(Ubuntu14.04)
- [REVIEW] NOIP 2015 Day2 解题报告
- 透镜畸变及校正模型
- BZOJ 4569: [Scoi2016]萌萌哒
- poj 1014
- poj3984 迷宫问题 bfs
- Linux 系统命令及其使用详解(上)
- Maven系列(五)CentOS7搭建最新GitLab
- SQL
- 几条好玩的linux命令
- debug caffe
- delphi 数据库操作
- linux驱动开发:input子系统三
- 机器学习 算法 工作(考虑点)