解题(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 ...
随机推荐
- J2SE 5.0-memory management whitepaper--delete
1.垃圾回收器期职责 开辟空间 任何引用可达的对象都在内存内 回收不再使用的内存 3.垃圾回收器概念 3.1.垃圾回收器期望的性能 垃圾回收器必须安全,存活的对象不应该被释放,应该释放的对象存活的时间 ...
- py库: scrapy (深坑未填)
scrapy 一个快速高级的屏幕爬取及网页采集框架 http://scrapy.org/ 官网 https://docs.scrapy.org/en/latest/ Scrapy1.4文档 http: ...
- [多线程]线程基础(对象锁、class锁、同步、异步)
synchronized.volatile.ReentrantLock.concurrent 线程安全:当多个线程访问某一个类(对象或方法)时,这个类始终都能表现出正确的行为,那么这个类(对象或方法) ...
- 电脑IP设置
方法一: echo offecho 修改[本地连接]IP......netsh interface IP set address "本地连接" static 138.8.8.111 ...
- PHP实现防sql注入
在查询数据库时需要防止sql注入 实现的方法: PHP自带了方法可以将sql语句转义,在数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(').双引号(").反斜线(\)与 ...
- Executors.newSingleThreadScheduledExecutor();线程池中放入多个线程问题
转自:https://blog.csdn.net/u012062455/article/details/78247234/
- Oracle的基本数据类型(常用)
转自:https://www.2cto.com/database/201810/783959.html 1.字符型 Char 固定长度字符串 占2000个字节 Varchar2 可变长度字符串 占40 ...
- ubuntu防火墙命令初探
1.防火墙的状态与开关 1)$sudo ufw status //查看防火墙的状态及当前的设置规则 2)$sudo ufw enable //开启防火墙 3)$sudo ufw disable // ...
- js函数的创建
1.js 函数的创建有几种方式: 1.1 直接声明 1.2 创建匿名函数,然后赋值 1.3 声明函数,然后赋值给变量 1.4 使用1.3 得到的变量再赋值给变量 1.5 使用函数对象创建函数 < ...
- 尚硅谷redis学习2-redis的安装和HelloWorld
Reids: remote dictionary server redis特点:支持持久化,支持复杂数据类型,支持备份 下载: 解压: 执行make, make install 可能会需要安装make ...