解题(MiGong--迷宫问题(深度搜索))
题目描述
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。
Input
一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
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
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
输入描述:
输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
输出描述:
左上角到右下角的最短路径,格式如样例所示。
输入
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
输出
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)
代码如下:
package com.yzh.hehe; import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class MiGong { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()) {
int x=scanner.nextInt();
int y=scanner.nextInt();
int[][] arr=new int[x][y];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
arr[i][j]=scanner.nextInt();
}
}
List<Point> list = miGong(arr);
for(Point temp:list){
System.out.println("("+temp.x+","+temp.y+")");
}
}
scanner.close();
}
/**
* 用一个list作为栈使用,每找到一个可通的点就加入到栈中,从栈顶取出一个点,依次从右下左上(对应1234)四个方向
* 判断下一个点是否可通(判断是否是1(墙)和考虑边界情况),可通就加入栈中,重复上面的操作,直到找到最右下角的点。否则的话换一个方向判断,
* 如果四个方向不通就从栈中删除这个点(回退一个点),再重复上面的操作。
*/
private static List<Point> miGong(int[][]arr) {
int xBound=arr.length-1;
int yBound=arr[0].length-1;
List<Point>list=new ArrayList<Point>();
list.add(new Point(0, 0, 1));
Point temppoPoint; while (true) {
temppoPoint=list.get(list.size()-1);
if (temppoPoint.direction==5) {
list.remove(list.size()-1);
continue;
} if (temppoPoint.direction==1){
//temppoPoint.y==yBound 放在前面能避免考虑边界上操作时数组越界
if (temppoPoint.y==yBound||arr[temppoPoint.x][temppoPoint.y+1]==1) {
temppoPoint.direction++;
continue;
} else {
list.add(new Point(temppoPoint.x, temppoPoint.y+1, 1));
}
}else if (temppoPoint.direction==2) {
if (temppoPoint.x==xBound||arr[temppoPoint.x+1][temppoPoint.y]==1) {
temppoPoint.direction++;
continue;
} else {
list.add(new Point(temppoPoint.x+1, temppoPoint.y, 1));
}
}else if (temppoPoint.direction==3) {
if (temppoPoint.y==0||arr[temppoPoint.x][temppoPoint.y-1]==1) {
temppoPoint.direction++;
continue;
} else {
list.add(new Point(temppoPoint.x, temppoPoint.y-1, 1));
}
}else{
if (temppoPoint.x==0||arr[temppoPoint.x-1][temppoPoint.y]==1) {
temppoPoint.direction++;
continue;
} else {
list.add(new Point(temppoPoint.x-1, temppoPoint.y, 1));
}
} if (list.get(list.size()-1).x==xBound&&list.get(list.size()-1).y==yBound) {
return list;
} }
} }
class Point{
int x;
int y;
int direction;
Point(int x,int y,int direction){
this.x=x;
this.y=y;
this.direction=direction;
}
}
解题(MiGong--迷宫问题(深度搜索))的更多相关文章
- 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)
走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...
- 洛谷P1605 迷宫 深度搜索 模板!
题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...
- F - 蜘蛛牌(深度搜索)
Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
- 【BZOJ2246】[SDOI2011]迷宫探险(搜索,动态规划)
[BZOJ2246][SDOI2011]迷宫探险(搜索,动态规划) 题面 BZOJ 洛谷 题解 乍一看似乎是可以求出每个东西是陷阱的概率,然而会发现前面走过的陷阱是不是陷阱实际上是会对当前状态产生影响 ...
- #C++初学记录(深度搜索#递归)
深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...
- 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)
#include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...
- [LeetCode] Populating Next Right Pointers in Each Node 深度搜索
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] Balanced Binary Tree 深度搜索
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- activiti explorer5.22.0源代码解读
请求通过ExplorerApplicationServlet(AbstractApplicationServlet.service()方法)进入web系统中. Activiti Explorer的应用 ...
- Xcode清除缓存,清除多余证书
http://blog.csdn.net/IDOshi201109/article/details/46634541
- Java执行js代码
在做项目中有时候需要用到Java调用js文件执行相应的方法 在JDK1.6添加了新的ScriptEngine类,允许用户直接执行js代码. import org.junit.Test; import ...
- 图解HTTP / HTTPS
http://kb.cnblogs.com/page/155287/ 我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议 ...
- centos官网下载旧版本办法
https://blog.csdn.net/yu0_zhang0/article/details/78503439 在 /etc/yum.conf 的 [main] 后面添加以下配置即可! exclu ...
- 启动标志 和launchMode
(1) FLAG_ACTIVITY_NEW_TASK: 操作: Activity A启动开僻Task堆栈(堆栈状态: A), 在Activity A中启动Activity B, 启动Activity ...
- 软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序
软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序 格式:wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数 ...
- 无法解析db.properties,spring报错:Caused by: java.sql.SQLException: unkow jdbc driver : ${url}
db.properties中配置了url等jdbc连接属性: driver=org.sqlite.JDBCurl=jdbc:sqlite:D:/xxx/data/sqliteDB/demo.dbuse ...
- using关键字在C#中的3种用法
using 关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型. (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. (一).作为指令 1. ...
- sourcetree 跳过注册
https://www.cnblogs.com/lucio110/p/8192792.html