K星人 2021-05-16 22:38 采纳率: 0%
浏览 47
已结题

dfs的一个问题,求最小步数

package com.algorithm.algorithm01server.demo;

import java.util.*;

public class Demo5 {
    //图论 F 迷宫问题
    static int wide, high, step, Row, Col;
    static int[] dx = {-1, 0, 1, 0};
    static int[] dy = {0, 1, 0, -1};
    static int[][] map;
    static int target = 11;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        List list = new ArrayList();
        while (true) {
            wide = scanner.nextInt();
            high = scanner.nextInt();
            if (high == 0 || wide == 0) {
                break;
            }
            //二维数组存储邻接矩阵
            map = new int[high][wide];
            for (int i = 0; i < high; i++) {
                for (int j = 0; j < wide; j++) {
                    map[i][j] = scanner.nextInt();


                    if (map[i][j] == 2) {
                        Row = i;
                        Col = j;
                    }
                }
            }
            //最开始的地方走了一步
            step = 1;
            int dfs = dfs(Row, Col, step);
            list.add(dfs);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private static int dfs(int Row, int Col, int step) {
        if (step > 10) {
            return -1;
        }

        //找到返回步数
        for (int i = 0; i < 4; i++) {
            //走的方向
            int nx = Row + dx[i];
            int ny = Col + dy[i];

            //如果没有碰到墙继续向前走
            while (nx >= 0 && nx < high && ny >= 0 && ny < wide && map[nx][ny] != 1) {
                if (map[nx][ny] == 3) {
                    return Math.min(step, target);
                }
                nx += dx[i];
                ny += dy[i];
            }
            if (nx >= 0 && nx < high && ny >= 0 && ny < wide && map[nx][ny] == 1) {

                map[nx][ny] = 0;//碰到石块,坐标的位置标记为0
                dfs(nx - dx[i], ny - dy[i], step + 1);//找到石块坐标的前一个位置,然后步数+1
                map[nx][ny] = 1;
            }
        }

        //没有找到返回-1
        return -1;
    }


}

dfs的一个问题,题目大概是:如果遇到的格子是 0空置广场  1是堵塞   2是开始位置    3目标位置,求最小步数问题。

然后我每次输入测试用例的时候都返回的是-1,找了很久没发现问题,请求一下,问问是哪里写错了?

我的测试用例

6 1
1 1 2 1 1 3
0 0
  • 写回答

2条回答 默认 最新

  • 小P聊技术 2021-05-17 09:58
    关注

    5*5迷宫初始化如下

    0 0 1 0 1

    0 0 0 0 1

    0 0 1 0 0

    0 1 0 0 1

    0 0 0 0 1

    初始位置在(0,0)处,欲到达(3,2)处最少需要走多少步

    代码如下:

     1 package DFSearch;
     2 
     3 import java.util.Scanner;
     4 
     5 public class FindPath {
     6     //人质所在迷宫的位置
     7     static int fx,fy;
     8     //迷宫为5*5
     9     static int n=5;
    10     //上下左右移动
    11     static int[][] temp ={{0,1},{1,0},{0,-1},{-1,0}};
    12     //迷宫数组
    13     static int [][] squera = new int [n][n];
    14     //标记数组,走过就标记为1
    15     static int [][] book = new int [n][n];
    16     //最短步数
    17     static int min = 9999999;
    18     public static void main(String[] args){
    19         @SuppressWarnings("resource")
    20         Scanner scan  = new Scanner(System.in);
    21         
    22         System.out.println("请输入迷宫5*5:");
    23         for(int i=0;i<n;i++){
    24             for(int j=0;j<n;j++){
    25                 squera[i][j] = scan.nextInt();
    26             }
    27         }
    28         System.out.println("请输入人质所在位置:");
    29         fx = scan.nextInt();
    30         fy = scan.nextInt();
    31         book[0][0] = 1;
    32         dfs(0,0,0);
    33         System.out.println(min);
    34         /*for(int i=0;i<5;i++){
    35             for(int j=0;j<5;j++){
    36                 if(book[i][j]==1){
    37                     System.out.println("<"+i+","+j+">->");
    38                 }
    39             }
    40         }*/
    41     }
    42     public static void dfs(int x,int y,int step){
    43         //如果到达地点,结束
    44         if(x==fx&&y==fy){
    45             if(step<min){
    46                 min = step;
    47             }
    48             return;
    49         }
    50         //循环移动到四个方向
    51         for(int i=0;i<4;i++){
    52             int tx = temp[i][0];
    53             int ty = temp[i][1];
    54             //如果该方向越界了,改变到另一个方向
    55             if(x+tx<0||x+tx>=n)
    56                 continue;
    57             if(y+ty<0||y+ty>=n)
    58                 continue;
    59             //如果该位置没有障碍物并且也没有走过,走
    60             if(squera[x+tx][y+ty]==0 && book[x+tx][y+ty]==0){
    61                     //标记为走过
    62                     book[x+tx][y+ty] = 1;
    63                     //搜索过程
    64                     //System.out.println(""+(x+tx)+","+(y+ty)+"->");
    65                     //往下一层递归
    66                     dfs(x+tx,y+ty,step+1);
    67                     //取消标记,回到上一层
    68                     book[x+tx][y+ty] = 0;
    69                 }
    70             }
    71         return;
    72     }
    73 }

    执行结果:

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月13日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,