题目描述

定义一个二维数组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表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

输出描述:

左上角到右下角的最短路径,格式如样例所示。

示例1

输入

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--迷宫问题(深度搜索))的更多相关文章

  1. 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)

    走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...

  2. 洛谷P1605 迷宫 深度搜索 模板!

    题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...

  3. F - 蜘蛛牌(深度搜索)

    Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...

  4. 题目--oil Deposits(油田) 基础DFS(深度搜索)

    上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...

  5. 【BZOJ2246】[SDOI2011]迷宫探险(搜索,动态规划)

    [BZOJ2246][SDOI2011]迷宫探险(搜索,动态规划) 题面 BZOJ 洛谷 题解 乍一看似乎是可以求出每个东西是陷阱的概率,然而会发现前面走过的陷阱是不是陷阱实际上是会对当前状态产生影响 ...

  6. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  7. 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)

    #include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...

  8. [LeetCode] Populating Next Right Pointers in Each Node 深度搜索

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  10. [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 ...

随机推荐

  1. system.data oracleClient 需要Oracle客户端8.1.7或high

  2. putty使用秘钥对登录百度云系统全过程

    使用秘钥对登录百度云系统全过程 1在百度云生成秘钥对 并且绑定要登录的IP 1)创建 2) 创建成功后,会自动下载私钥到本地 3)绑定 4)绑定完毕 2把下载到本地的私钥使用putty key gen ...

  3. beginner_json_setting

  4. 4. mysql 查看数据库中所有表的记录数

    use information_schema; select table_name,table_rows from tables where TABLE_SCHEMA = 'testdb'  orde ...

  5. JS 对象(对象遍历,拷贝)

     定义属性 直接 obj.对象 的方法 Object.defineProperty(obj, prop, descriptor) ,这种方法可以设置 或者修改对象属性的访问权限 数据描述符和存取描述符 ...

  6. linux驱动开发( 五) 字符设备驱动框架的填充file_operations结构体中的操作函数(read write llseek unlocked_ioctl)

    例子就直接使用宋宝华的书上例子. /* * a simple char device driver: globalmem without mutex * * Copyright (C) 2014 Ba ...

  7. 深度学习原理与框架-Tensorflow卷积神经网络-神经网络mnist分类

    使用tensorflow构造神经网络用来进行mnist数据集的分类 相比与上一节讲到的逻辑回归,神经网络比逻辑回归多了隐藏层,同时在每一个线性变化后添加了relu作为激活函数, 神经网络使用的损失值为 ...

  8. java web项目使用IDEA打成war包

    步骤: 1.点击 File -->Project Structure...如下图: 2.出现如下界面后点击 Artifacts--> 绿色加号-->Web Application:A ...

  9. Java学习路线(转)

    原文:http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http ...

  10. DATASNAP远程方法返回TSTREAM正解(转咏南兄)

    DATASNAP远程方法返回TSTREAM正解 DATASNAP远程方法返回TSTREAM,如果数据大小超过32K是会报错的.许多DELPHIER栽在这个上头,甚至开始怀疑TSTREAM返回数据的可行 ...