poj3984 迷宫问题
来源:互联网 发布:swatch知乎 编辑:程序博客网 时间:2024/06/07 02:38
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20123 Accepted: 11797
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
自己利用队列思想完成一个BFS,因为系统提供的方法没有保存功能。创建一个自己想要的类存放结点信息,我自己创建的类里结点为当前位置坐标和这个结点的前一个结点索引下标,然后还是利用队列完成广度搜索。
import java.util.*;public class MazeProblem {static char[][] maze = new char[10][10];static boolean[][] visited = new boolean[6][6];static myqueue[] m = new myqueue[30];static int dirx[] = {-1,1,0,0};static int diry[] = {0,0,1,-1};static void print(int i){ //利用递归函数完成输出 if(m[i].front!=-1){ print(m[i].front); System.out.println("("+m[i].row+", "+m[i].col+")"); } }public static void main(String[] args) {Scanner input = new Scanner(System.in);String s;for( int i = 0 ; i < 5 ; i++){s = input.nextLine();s = s.replaceAll(" ", "");//去掉没用的空格maze[i] = s.toCharArray();}for( int i = 0 ; i < 6 ; i++){Arrays.fill(visited[i], false);}m[0] = new myqueue();m[0].row = 0 ; m[0].col = 0 ;m[0].front = -1;for( int i = 1 ; i < m.length ; i++)m[i] = new myqueue();visited[0][0] = true;int front = 0 , rear = 1;while(front < rear){if( m[front].row == 4 && m[front].col == 4 ){System.out.println("(0, 0)");//需要自己输出开始结点print(front);break;}else{for( int i = 0 ; i < 4 ; i++){int row = m[front].row + dirx[i];int col = m[front].col + diry[i];if( row < 0 || row > 4 || col < 0 || col > 4)continue;if( !visited[row][col] && maze[row][col] == '0'){visited[row][col] = true;m[rear].row = row;m[rear].col = col;m[rear].front = front;rear++;}}}front++;}}}class myqueue{int row,col;int front = -2;}
当然我自己也用c++写了利用DFS完成这个最优解的输出问题,当然因为这里数据量小DFS才不会超时,若数据量过大DFS超时的可能性就会提高。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;bool visited[5][5];char a[6][6];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int minLen = 1 << 30;int totalLen = 0;int flag[6][6];bool finds = false;void DFS(int x , int y){ if( x == 4 && y == 4 ) { minLen = min(minLen,totalLen); return; } for( int i = 0 ; i < 4 ; i++) { int row = x + dir[i][0]; int col = y + dir[i][1]; if( row < 0 || row > 4 || y < 0 || y > 4 ) continue; if( !visited[row][col] && a[row][col] == '0' ) { visited[row][col] = true; totalLen += 1; DFS(row,col); visited[row][col] = false; totalLen -= 1; } }}void findWay(int x,int y){ if( x == 4 && y == 4 ) { if( minLen == totalLen ) { int t = 0; for( int k = 0 ; k < minLen ; k++) { for( int i = 0 ; i < 5 ; i++ ) for( int j = 0 ; j < 5 ; j++) { if( flag[i][j] == t ) { cout << "(" << i << ", " << j << ")" << endl; t++; } } if( t == minLen) break; } finds = true; } return; } for( int i = 0 ; i < 4 ; i++) { int row = x + dir[i][0]; int col = y + dir[i][1]; if( row < 0 || row > 4 || y < 0 || y > 4 ) continue; if( !visited[row][col] && a[row][col] == '0' ) { visited[row][col] = true; totalLen += 1; flag[row][col] = totalLen; findWay(row,col); visited[row][col] = false; flag[row][col] = -1; totalLen -= 1; } if( finds == true ) break; }}int main( ){ int i,j; for( i = 0 ; i < 5 ; i++) for( j = 0 ; j < 5 ; j++) cin >> a[i][j] ; memset(visited,false,sizeof(visited)); visited[0][0] = true; DFS(0,0); memset(visited,false,sizeof(visited)); memset(flag,-1,sizeof(flag)); totalLen = 0; flag[0][0] = 0; //cout << minLen << endl; visited[0][0] = true; findWay(0,0); return 0;}
0 0
- POJ3984 迷宫问题 BFS
- POJ3984 迷宫问题
- HUD--POJ3984 迷宫问题
- POJ3984 迷宫问题 BFS
- poj3984 迷宫问题
- poj3984(迷宫问题)
- poj3984 迷宫问题
- [POJ3984]迷宫问题
- poj3984 迷宫问题(BFS)
- POJ3984 迷宫问题
- POJ3984 迷宫问题【BFS】
- poj3984 迷宫问题
- Poj3984- dfs -迷宫问题
- poj3984迷宫问题(bfs)
- poj3984迷宫问题
- poj3984 迷宫问题
- poj3984迷宫问题
- POJ3984 迷宫问题【BFS】
- freeradius
- C中可变参数函数及log函数运用
- 343. Integer Break
- 为什么需要字节对齐?
- C语言链表创建的电子通讯录V1.0
- poj3984 迷宫问题
- SAP录屏BDC使用实例
- Android开发中向SQlite表自动插入当前时间
- HJT212协议
- matlab实现PS算法之亮度调整
- JAVA字符串格式化-String.format()的使用
- pdf文件如何编辑背景颜色
- 防止按钮多次快速点击,防误触
- sqlite3