解题(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 ...
随机推荐
- 使用SQL SERVER PIVOT实现行列转置
一般我们在使用SQL语句实现行列转置时候,最常用的方法无外乎就是 case语句来实现,但是如果需要需要转置的列太多,那么case起来语句就无限庞大,十分不方便,sql server中的PIVOT就可以 ...
- pychram 2018-01 安装pyQT5报错
pychram 2018-01 安装pyQT5报错
- 关于时间:UTC/GMT/xST/ xDT
UTC ,Coordinated Universal Time GMT ,Greenwich Mean Time GMT和UTC 一般来说是意义几近相同的, 不过 UTC Coordinated Un ...
- 混合式应用真机调试(Android Studio + Chrome)
如何在chrome上调试混合式APP https://developers.google.com/web/tools/chrome-devtools/remote-debugging/ Get Sta ...
- oracle查询重复的数据
在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是oracle中的哪一个数据文件.块.行上.在重复的记录中,可能所有列的内容都相同,但rowi ...
- ASPxCallback组件(珍藏版)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...
- DataSnap Server 客户端调用 异常
No peer with the interface with guid {9BB0BE5C-9D9E-485E-803D-999645CE1B8F} has been registered.
- centos服务器如何用命令查看哪个程序内存占用情况,硬盘空间占用
1.free -m只能查看内存总量情况 2.top M ( 注意M是大写) 3.ps aux|head -1; ps aux | sort -k4nr | head -10 ------------- ...
- java通过过滤器 设置跨域允许
前人种树:https://www.cnblogs.com/CBDoctor/p/4235082.htmlhttps://blog.csdn.net/yuting0787/article/details ...
- Weed-FS 接口 master、volume 服务接口(转)
目录结构 weed-fs master 服务接口,分配文件 id,查找 volume,volume 服务接口,在指定的 volume 服务创建指定的 volume,检查 volume 服务的状态. ...