POJ 3984 迷宫问题
来源:互联网 发布:2017人工智能 编辑:程序博客网 时间:2024/05/16 09:41
题目大意:中文
题目链接
注释代码:
/* * Problem ID : POJ 3984 迷宫问题 * Author : Lirx.t.Una * Language : C * Run Time : 0 ms * Run Memory : 132 KB */ #include <stdio.h>typedefstruct { charx, y; } Pos;charmaze[5][5];//迷宫charvis[5][5];//BFS中检测一个点是否被访问过Posq[5 * 5 + 1];//BFS的队列charpre[5 * 5];//用来记录每个点的父结点,记录内容为q中结点的下标//表示向量(dx, dy),上下左右四种走法intdx[4] = { -1, 1, 0, 0 };intdy[4] = { 0, 0, -1, 1 };//由于每次直走一步,因此广度优先搜索下第一个解必定是最优解(因为步数最少)voidprint(int x) {//递归打印出完整的路线//即打印从头到尾每个点的坐标if ( !x ) {puts("(0, 0)");return ;}print(pre[x]);printf("(%d, %d)\n", q[x].x, q[x].y);}intmain() {inti, j;//计数变量inthead, tail;//队列的头尾intx, y;//移动前的坐标intxx, yy;//移动后的坐标for ( i = 0; i < 5; i++ )for ( j = 0; j < 5; j++ )scanf("%d", &maze[i][j]);//BFSvis[0][0] = 1;head = 0;tail = 1;while ( head < tail ) {x = q[head].x;y = q[head].y;for ( i = 0; i < 4; i++ ) {xx = x + dx[i];yy = y + dy[i];if ( xx >= 0 && xx < 5 && yy >= 0 && yy < 5 &&//没走出迷宫外面!maze[xx][yy] && !vis[xx][yy] ) {//不是障碍物且没有访问过//pushq[tail].x = xx;q[tail].y = yy;pre[tail] = head;//记录前驱if ( 4 == xx && 4 == y ) {//搜索到目标print(tail);return 0;}vis[xx][yy] = 1;tail++;}}head++;//pop}return 0;}无注释代码:
#include <stdio.h>typedefstruct { charx, y; } Pos;charmaze[5][5];charvis[5][5];Posq[5 * 5 + 1];charpre[5 * 5];intdx[4] = { -1, 1, 0, 0 };intdy[4] = { 0, 0, -1, 1 };voidprint(int x) {if ( !x ) {puts("(0, 0)");return ;}print(pre[x]);printf("(%d, %d)\n", q[x].x, q[x].y);}intmain() {inti, j;inthead, tail;intx, y;intxx, yy;for ( i = 0; i < 5; i++ )for ( j = 0; j < 5; j++ )scanf("%d", &maze[i][j]);vis[0][0] = 1;head = 0;tail = 1;while ( head < tail ) {x = q[head].x;y = q[head].y;for ( i = 0; i < 4; i++ ) {xx = x + dx[i];yy = y + dy[i];if ( xx >= 0 && xx < 5 && yy >= 0 && yy < 5 && !maze[xx][yy] && !vis[xx][yy] ) {q[tail].x = xx;q[tail].y = yy;pre[tail] = head;if ( 4 == xx && 4 == y ) {print(tail);return 0;}vis[xx][yy] = 1;tail++;}}head++;}return 0;}
0 0
- poj 3984 迷宫问题
- poj 3984 迷宫问题
- POJ-3984-迷宫问题
- Poj 3984 迷宫问题
- 迷宫问题 poj 3984
- poj 3984迷宫问题
- poj 3984 迷宫问题
- POJ-3984-迷宫问题
- poj 3984- 迷宫问题
- POJ 3984 迷宫问题
- poj 3984 迷宫问题
- poj 3984:迷宫问题
- POJ-3984-迷宫问题
- POJ 3984 迷宫问题
- poj 3984迷宫问题
- POJ 3984 迷宫问题
- POJ 3984 迷宫问题
- poj 3984 迷宫问题
- 堆排序
- 连接 FTP 服务器提示“500 OOPS: cannot change directory:/home/xxx”
- 自定义的线性表类
- ShortcutMapper 是应用程序的键盘快捷键
- MFC为子对话框添加OnInitDialog函数
- POJ 3984 迷宫问题
- hdu 4362 Dragon Ball(优先队列+dp)
- learn with python-第十一章:文件和异常
- the STL std::set insert --Operator< Overloaded
- OOP版电子词典
- 项目中发现的一些关于JavaScript中JSON的注意点
- GDI+屏幕截图内存BMP转JPG,JPG再转IStream流,IStream流再转BYTE;
- hadoop_day1
- 多线程