POJ-3984迷宫问题 (BFS,水题)
来源:互联网 发布:百川环评工程师软件 编辑:程序博客网 时间:2024/06/10 22:36
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24199 Accepted: 14125
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)
Source
Hint:水题。。可以用BFS
#include <stdio.h>#include <queue>#include <iostream>#include <string.h>using namespace std;int Map[11][11],v[11][11],d[4][2]={0,1,0,-1,1,0,-1,0};int flag=0;int judge(int x,int y){ if(x<0||y<0||x>4||y>4) return 0; if(Map[x][y]==1) return 0; if(v[x][y]) return 0; else return 1;}void print(int x,int y){ if(flag==0) printf("(%d, %d)\n",x,y);}void DFS(int x,int y){ if(x==4&&y==4) { flag=1; return; } for(int i=0;i<4;i++) { if(judge(x+d[i][0],y+d[i][1])) { v[x+d[i][0]][y+d[i][1]]=1; print(x+d[i][0],y+d[i][1]); DFS(x+d[i][0],y+d[i][1]); } }}int main(){ memset(v,0,sizeof(v)); for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { scanf("%d",&Map[i][j]); if(j==4) getchar(); } } printf("(0, 0)\n"); DFS(0,0); return 0;}
[Submit] [Go Back] [Status] [Discuss]
阅读全文
1 0
- POJ 3984 迷宫问题 (BFS,水题)
- POJ-3984迷宫问题 (BFS,水题)
- POJ 3984-迷宫问题 BFS
- poj 3984 迷宫问题(bfs)
- POJ 3984 迷宫问题 BFS
- poj 3984 迷宫问题 bfs
- POJ 3984 || 迷宫问题 (bfs
- POJ 3984 迷宫问题 BFS
- POJ 3984 迷宫问题(bfs)
- BFS-POJ-3984-迷宫问题
- 迷宫问题 POJ 3984 【BFS】
- POJ 3984 迷宫问题 (BFS)
- POJ 3984:迷宫问题【BFS】
- POJ 3984迷宫问题(BFS)
- poj 3984 迷宫问题 bfs
- POJ 3984:迷宫问题【BFS】
- POJ 3984 迷宫问题 BFS
- poj 3984 迷宫问题(bfs)
- Uncaught TypeError: $(...).customFileInput is not a function
- nodejs的helloworld
- iOS_LocalPush_本地推送
- GG and MM
- RecyclerView点击事件
- POJ-3984迷宫问题 (BFS,水题)
- POJ2251 BFS
- 刷题打卡2 UVa1590,UVa508, UVa509
- 二叉排序树--cf675d Tree construction
- P3741 honoka的键盘
- Python字典对象实现原理
- 8.10关于二维数组
- 8.10
- 操作数据库(增删改查)