(Java实现) 洛谷 P1605 迷宫

题目背景
迷宫 【问题描述】

给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和

终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫

中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。

输入样例 输出样例

【数据规模】

1≤N,M≤5

题目描述
输入输出格式
输入格式:
【输入】

第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX,SY,终点

坐标FX,FY。接下来T行,每行为障碍点的坐标。

输出格式:
【输出】

给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方

案总数。

输入输出样例
输入样例#1:
2 2 1
1 1 2 2
1 2
输出样例#1:
1

import java.util.Scanner;


public class migong {
	static int n,m,t,sx,sy,fx,fy,count=0;
	static int  [] [] map ;
	static int []  a = {0,1,0,-1}; 
	static int []  b = {-1,0,1,0}; 
	static boolean  [] [] bool ;
	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();
		 fx = sc.nextInt();
		 fy = sc.nextInt();
		map = new int[n+1][m+1];
		 bool = new boolean[n+1][m+1];
		for (int i = 0; i < t; i++) {
			int c = sc.nextInt();
			int d = sc.nextInt();
			bool[c][d]=true;
		}
		dfs(fx,fy);
		System.out.println(count);
	}
	public static void dfs(int x,int y){
		if(x==fx && y==fy){
			count++;
			return;
		}
		for (int i = 0; i < a.length; i++) {
			if(x+a[i]>0 && x+a[i]<=n && y+b[i]>0 && y+b[i]<=m && 
					!bool[x+a[i]][y+b[i]]){
				bool[x+a[i]][y+b[i]]=true;
				dfs(x+a[i],y+b[i]);
				bool[x+a[i]][y+b[i]]=false;
			}
		}
	}

}

原文地址:https://www.cnblogs.com/a1439775520/p/13079196.html