解题(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 ...
随机推荐
- oracle insert 返回ID
create or replace procedure getid(v_id out number)as v_sql varchar2(500); begin v_sql:='insert into ...
- windows python读取grib2数据
一.环境准备 (1).python3环境 (2).wgirb工具(用于读取grib1文件),下载地址: ftp://ftp.cpc.ncep.noaa.gov/wd51we/wgrib (3).wg ...
- hive grouping sets 实现原理
先下结论: 看了hive 1.1.0 grouping sets 实现(从源码及执行计划都可以看出与kylin实现不一样),(前提是可累加,如sum函数)他并没有像kylin一样先按照group by ...
- spark 常用技巧总结
解析url scala> import java.net.URLimport java.net.URL scala> val urlstr="http://www.baidu.c ...
- Android Netty Server
项目源码在github上,请看这里-->Android Netty Server Android netty server Start a netty server on android Dow ...
- springBoot整合MongoDB(单机)
依赖: <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mon ...
- Swoole 内存操作(Table)
使用: //实例化表格,参数 int : 最大行数 $table = new swoole_table(1024); //设置表格字段 参数 (字段名:string , 字段类型:int.float. ...
- Mysql 获取表属性
获取表字段信息: select column_name from information_schema.COLUMNS where table_name='表名' nformation_schema. ...
- Linux 目录栈及目录切换
使用 cd - (中杠)会切换到上一次的目录 cd 命令会改变目录栈 1.dirs 1)功能显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录) 2)语法(1)格式:dirs ...
- 使用linux的shell脚本实现在当前行重复动态显示时间等字符串信息(不另起新行)
###本脚本在Suse11sp2当中验证正确 #!/bin/sh )) do echo -ne "\r$(date)" sleep 0.3 done ###关键在 echo 的 & ...