美文网首页
【洛谷】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 - 迷宫

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