题目链接:P1605
简单的搜索题:给出起始点,障碍点,然后判断有多少种方法到达终点,直接搜索就好了
注意:判断到达终点的条件要写在判断为障碍点的后面,万一终点是障碍点,那不是傻逼了因为数据确实有个终点为障碍点我wa了一发
代码:
import java.util.*;
public class Main {
static int N,M,T,SX,SY,FX,FY;
static int key[][];
static boolean a[][];
static int count = 0;
static int xx[] = new int[]{0,0,-1,1};
static int yy[] = new int[]{-1,1,0,0};
static void dfs(int x,int y){
if(x < 1 || x > N || y < 1 || y > M)// 出界
return;
if(a[x][y]) // 已经走过
return;
for(int i = 0;i < T;i++)
if(x == key[i][0] && y == key[i][1]) // 为障碍点
return;
if(x == FX && y == FY){ // 到达终点
count++;
return;
}
for(int i = 0;i <= 3;i++){
a[x][y] = true;
dfs(x+xx[i],y+yy[i]);
a[x][y] = false;
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
a = new boolean[N+1][M+1];
for(int i = 1;i <= N;i++)
for(int j = 1;j <= M;j++)
a[i][j] = false;
T = sc.nextInt();
key = new int[T][2];
SX = sc.nextInt();
SY = sc.nextInt();
FX = sc.nextInt();
FY = sc.nextInt();
for(int i = 0;i < T;i++){
key[i][0] = sc.nextInt();
key[i][1] = sc.nextInt();
}
dfs(SX,SY);
System.out.println(count);
}
}