提交 02d08292 编写于 作者: qq_36480062's avatar qq_36480062

commit

上级 1395091e
......@@ -6,15 +6,17 @@ import java.util.Scanner;
public class DFS {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 15, 23, 13, 12};
// buFenHe(arr, 46, 0, new ArrayList<>());
ShuiWa(10,12);
int k = 11;
System.out.println(ABC(arr, arr.length, k, 0, 0));
buFenHe(arr, k, 0, new ArrayList<Integer>());
// int[] data = new int[6];
// data[0] = 1;
// suShuHuan(6,data,1);
// ShuiWa(N, M);
NhuangHou(0);
System.out.println(cnt);
// NhuangHou(0);
// System.out.println(cnt);
}
/**
......@@ -53,11 +55,33 @@ public class DFS {
intS.remove(intS.size() - 1);//回溯
}
/**
* 部分数之和简化版,返回存在与否
*
* @param arr 源数组
* @param n 数组长度
* @param k 需要求出的和
* @param i 第i个元素
* @param sum 当前凑到的部分和
* @return 返回存在与否
* 每个状态按顺序决定每个数,加或者不加,
* 在全部n个数分决定后再判断它们的和是否为k即可
* 状态数是2^n+1所以复杂度是O(2^n)
* 枚举所有的状态
*/
public static boolean ABC(int[] arr, int n, int k, int i, int sum) {
if (i == n) return sum == k;
if (ABC(arr, n, k, i + 1, sum)) return true;
if (ABC(arr, n, k, i + 1, sum + arr[i])) return true;
return false;
}
public static int N = 10;
public static int M = 12;
/**
* POJ.2386
* 3.水洼数目
* 有一个大小为 N*M 的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出
* 园子里总共有多少水洼?(八连通指的是下图中相对 W 的*的部分)
......@@ -134,7 +158,7 @@ public class DFS {
for (int l = -1; l < 2; l++) {
if (k == 0 && l == 0)//不判断自身,因为开头已经把当前水抽干
continue;
if (i + k >= 0 && i + k <= N - 1 && j + l >= 0 && j + l <= M - 1) {
if (i + k >= 0 && i + k <= N - 1 && j + l >= 0 && j + l <= M - 1) {//边界条件
if (data[i + k][j + l] == 'W')
Shuiwa(data, i + k, j + l);
}
......
package com.shiyu;
import java.util.ArrayDeque;
public class Tesdtt {
public static void main(String[] args) {
ArrayDeque<Integer> arr = new ArrayDeque<Integer>();
arr.offer(12);
arr.offer(2123);
arr.offer(22);
System.out.println(arr.poll());
}
}
package com.shiyu;
import java.util.Arrays;
public class tsdt {
public static void main(String[] args) {
// System.out.println(isPalindrome(121));
......@@ -9,8 +11,10 @@ public class tsdt {
// a.add(5);
//
// System.out.println(a.poll());
int d=2&3;
int d = 2 & 3;
System.out.println(d);
int[] arr = {3, 4, 6, 87, 478};
System.out.println(Arrays.binarySearch(arr, 0, 2, 4));
}
......
package BFS;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;
/**
* POJ 3984
* https://blog.csdn.net/Jason_Ranger/article/details/50768661
* https://blog.csdn.net/a739260008/article/details/89166428
* 给定一个大小为N*M的迷宫,迷宫由通道和墙壁组成
* 每一步可以向左右上下四个方向走一步,
* 请计算从出发点到终点最少需要多少步,
* 若不能到达终点则输出0
* N,M<=100
* * n=10 m=10
* <p>
* #S######.#
* ......#..#
* .#.##.##.#
* .#........
* ##.##.####
* ....#....#
* .#######.#
* ....#.....
* .####.###.
* ....#...G#
*
* BFS其实与与DFS一样,都会生成所有能遍历到的状态,
* 递归函数可以代码简短,状态的管理更简单,求取最短路的时候使用BFS,
* 因为求取最短路时,DFS需要反复经过同样的状态
*
* BFS会把状态逐个加入队列,因此通常需要与状态数成正比的内存空间,
* 反之,DFS是与最大递归深度成正比的,一般与状态数相比,递归的深度并不会太大
* 一般来说DFS比BFS更节省内存,
*
*/
public class 迷宫最短路径BFS实现 {
public static class Node {
public int x;
public int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int sx = 0;
int sy = 0;
data = new char[n][m];
sc.nextLine();//换行,再输入迷宫数据
for (int i = 0; i < n; i++) {
data[i] = sc.nextLine().toCharArray();
System.out.println(Arrays.toString(data[i]));
}
int res = BFS(n, m, 0, 1, 9, 8);
System.out.println(res);
}
public static char[][] data;
public static int[] dx = {1, 0, -1, 0};
public static int[] dy = {0, 1, 0, -1};
/**
* @param N 长度
* @param M 宽度
* @param sx 起点横坐标
* @param sy 起点纵坐标
*/
public static int BFS(int N, int M, int sx, int sy, int gx, int gy) {
int[][] d = new int[N][M];//到各个位置的最短距离的数组
for (int i = 0; i < N; i++) {
Arrays.fill(d[i], -1);
}
Queue<Node> queue = new ArrayDeque<Node>();
Node first = new Node(sx, sy);
queue.offer(first);//初始化起点入队
d[sx][sy] = 0;//初始化起点的,最短距离数组为0
while (!queue.isEmpty()) {
Node cur = queue.poll();
int cx = cur.x;
int cy = cur.y;
if (cx == gx && cy == gy) {
return d[gx][gy];//如果取到了终点,直接返回结束方法
}
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];//取出偏移量
int ny = cy + dy[i];//取出偏移量
// 判断是否访问过,越界,可行
if ((0 <= nx && nx < N)//边界控制
&& (0 <= ny && ny < M)//边界控制
&& (d[nx][ny] == -1)//判断这个位置是否遍历过
&& (data[nx][ny] != '#')) {//判断这个位置是否是墙壁,能不能走
Node nextNode = new Node(nx, ny);
queue.add(nextNode);//将经过筛选可以遍历到的位置,加入队列
d[nx][ny] = d[cx][cy] + 1;//维护,加入队列的那个节点的距离起点的距离
}
}
}
return d[gx][gy];
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册