美文网首页
【洛谷】P1605 - 迷宫

【洛谷】P1605 - 迷宫

作者: 莫wen | 来源:发表于2020-11-12 22:44 被阅读0次
public class Main {
    static int N ;
    static int M ;
    static int T ;
    static int[][] area ;
    static int SX ;
    static int SY ;
    static int EX ;
    static int EY ;
    
    static int[] ZhX ;
    static int[] ZhY ;
    
    static int count ;
    
    static int[] px = {-1, 1 , 0 , 0};
    static int[] py = {0 , 0 , -1, 1};
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        N = sc.nextInt();
        M = sc.nextInt();
        T = sc.nextInt();
        
        SX = sc.nextInt();
        SY = sc.nextInt();
        EX = sc.nextInt();
        EY = sc.nextInt();
        
        area = new int[N][M];
        ZhX = new int[T];
        ZhY = new int[T];
        
        count = 0;
        
        for (int i = 0; i < T; i++) {
            ZhX[i] = sc.nextInt();
            ZhY[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                area[i][j] = 0;
            }
        }
        
        for (int i = 0; i < T; i++) {
            int r = ZhX[i]-1;
            int c = ZhY[i]-1;
            area[r][c] = 2; // 障碍物处 
        }
        
        area[SX-1][SY-1] = 1;
        DFS(SX-1 , SY-1);
        
        System.out.println(count);
        
        
    }

    public static void DFS(int x, int y) {
        if (x == (EX-1) && y == (EY-1)) {
            count += 1;
            return;
        }
        
        if (area[x][y] == 2 ) {
            return;
        }
        
        for (int i = 0; i != 4; i++) {
            int newx = x + px[i];
            int newy = y + py[i];
            if (newx >= 0 && newx < N && newy >= 0 && newy < N && area[newx][newy] == 0) {
                area[newx][newy] = 1;
                DFS(newx,newy);
                area[newx][newy] = 0;
            }
            
        }
        
        
    }
}

相关文章

  • 【洛谷 P1605】迷宫

    迷宫(题目链接) 思路 简单的深搜问题 代码

  • 【洛谷】P1605 - 迷宫

  • 洛谷P1141 01迷宫 题解

    一条搜索水题,竟然交了10次才a...还是太菜了,怒献一篇题解,思路是记忆化dfs+剪枝。先看一下题目:(截图拼接...

  • 2020-03-10(洛谷P1141 01迷宫)

    写到吐血的一道题,其实就是再dfs上加了一个优化,变成了联通快,也可以叫做记忆化搜索;1. 如果当前的点不属于任何...

  • 洛谷计划

    洛谷是IT生认可度较高的一个网站,有各种题目以及专业术语,是刷题的一个好地方,但是对基础要求还算挺高,因此需要在...

  • 几个高精度模板

    模板来自洛谷及Acwing:Acwing洛谷 后续增加注释以及相关代码改进 高精度加法 高精度减法 高精度乘法 高...

  • 洛谷新手题

    今天只是做了一个简单的顺序与分支题,知识点也很常见,只截图题目和代码了~

  • P1000 超级玛丽游戏

    【题目背景】 本题是洛谷的试机题目,可以帮助了解洛谷的使用。 建议完成本题目后继续尝试P1001、P1008。 【...

  • 洛谷P1219八皇后-dfs

    题目传送:洛谷P1219八皇后 dfs

  • 数据结构(十九) -- 图

    一,概述 弥诺陶洛斯(Minotaur)是希腊神话中半人半牛的怪物,它藏身于一个精心设计的迷宫之中。这个迷宫的结构...

网友评论

      本文标题:【洛谷】P1605 - 迷宫

      本文链接:https://www.haomeiwen.com/subject/duembktx.html