当前位置:网站首页>DFS question list and template summary
DFS question list and template summary
2022-07-30 22:55:00 【Conan II】
This article is to document my own learningDFSAlgorithms and records were writtenDFSSummary of questions,持续补充
迷宫
题目描述
给定一个 N × M N \times M N×M 方格的迷宫,迷宫里有 T T T 处障碍,障碍处不可通过.
在迷宫中移动有上下左右四种方式,每次只能移动一个方格.数据保证起点上没有障碍.
给定起点坐标和终点坐标,每个方格最多经过一次,问有多少种从起点坐标到终点坐标的方案.
输入格式
第一行为三个正整数 N , M , T N,M,T N,M,T,分别表示迷宫的长宽和障碍总数.
第二行为四个正整数 S X , S Y , F X , F Y SX,SY,FX,FY SX,SY,FX,FY, S X , S Y SX,SY SX,SY 代表起点坐标, F X , F Y FX,FY FX,FY 代表终点坐标.
接下来 T T T 行,每行两个正整数,表示障碍点的坐标.
输出格式
输出从起点坐标到终点坐标的方案总数.
样例 #1
样例输入 #1
2 2 1
1 1 2 2
1 2
样例输出 #1
1
提示
对于 100 % 100\% 100% 的数据, 1 ≤ N , M ≤ 5 1 \le N,M \le 5 1≤N,M≤5, 1 ≤ T ≤ 10 1 \le T \le 10 1≤T≤10, 1 ≤ S X , F X ≤ n 1 \le SX,FX \le n 1≤SX,FX≤n, 1 ≤ S Y , F Y ≤ m 1 \le SY,FY \le m 1≤SY,FY≤m.
AC代码:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
static int n;
static int m;
static int N = 10;
static int T;
static int[][] arr = new int[N][N];
static boolean[][] vis = new boolean[N][N];
static int[][] dir = {
{
1,0}, {
-1,0}, {
0,1}, {
0,-1}};
static int res = 0;
static int x0;
static int y0;
static int x;
static int y;
public static void main(String[] args) throws IOException {
Read read = new Read();
n = read.nextInt();
m = read.nextInt();
T = read.nextInt();
// (x0,y0) -> 起始点坐标
// (x,y) -> 终点坐标
x0 = read.nextInt();
y0 = read.nextInt();
x = read.nextInt();
y = read.nextInt();
for (int i = 1 ; i <= n; i++) {
for (int j = 1; j <= m; j++) {
// 赋予默认值1
arr[i][j] = 1;
}
}
for (int k = 1 ; k <= T; k++) {
// 标记障碍物为0
int a1 = read.nextInt();
int a2 = read.nextInt();
arr[a1][a2] = 0;
}
dfs(x0,y0);
System.out.println(res);
}
public static void dfs(int i,int j) {
// Exit the loop if the end point is reached
if (i == x && j == y) {
res++;
return;
} else {
// 针对(i,j)The coordinates are explored up, down, left and right each time
for (int k = 0 ; k < 4; k++) {
int dx = i + dir[k][0];
int dy = j + dir[k][1];
// 如果当前坐标(dx,dy)没有被访问过,且(dx,dy)is a non-obstruction point
if (dx >= 1 && dx <= n && dy >= 1 && dy <= m && arr[dx][dy] == 1 && !vis[dx][dy]) {
// 标记(i,j)已经遍历过
vis[i][j] = true;
dfs(dx, dy);
// 还原(i,j)
vis[i][j] = false;
}
}
}
}
}
class Read {
StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
public int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
public double nextDouble() throws IOException {
st.nextToken();
return st.nval;
}
public String nextString() throws IOException {
st.nextToken();
return st.sval;
}
}
边栏推荐
猜你喜欢

Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)

IDEA usage skills

IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields

QT 在父类中添加子类的流程,object tree,

Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation

反转链表-头插反转法

Navicat cannot connect to mysql super detailed processing method

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

Go1.18升级功能 - 泛型 从零开始Go语言

Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
随机推荐
Successfully solved ImportError: always import the name '_validate_lengths'
Gxlcms有声小说系统/小说听书系统源码
grub learning
Installation and use of cnpm
VS2017 compile Tars test project
Apache Doris系列之:深入认识实时分析型数据库Apache Doris
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
一文详解:SRv6 Policy模型、算路及引流
Go的Gin框架学习
grub 学习
Learning about XML (1)
QT开发简介、命名规范、signal&slot信号槽
Jetson AGX Orin 平台关于c240000 I2C总线和GMSL ses地址冲突问题
2022/07/30 学习笔记 (day20) 面试题积累
Abstract classes and interfaces (study notes)
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
【Untitled】
MySQL索引常见面试题(2022版)