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

c

上级 35ce1371
package DFS.剪枝;
public class 奇偶走迷宫 {
public static void main(String[] args) {
}
}
package DFS.剪枝;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
* https://blog.csdn.net/qq_30277239/article/details/104813141
*/
public class 运输小猫 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 0; i < n; i++) {
w[i] = sc.nextInt();
}
Arrays.sort(w, 0, n, new Comparator<Integer>() {
@Override
public int compare(Integer t2, Integer t1) {
return t1 - t2;
}
});
System.out.println(Arrays.toString(w));
dfs(0, 0);
System.out.println(ans);
}
/**
* @param u 走到第几个小猫,下标
* @param k 当前是第几辆车
*/
static void dfs(int u, int k) {
if (k >= ans) return;
if (u == n) {
ans = k;
return;
}
for (int i = 0; i < k; i++) {
if (sum[i] + w[u] <= m) {
sum[i] += w[u];
dfs(u + 1, k);
sum[i] -= w[u];
}
}
sum[k] = w[u];
dfs(u + 1, k + 1);
sum[k] = 0;
}
static Integer[] w = new Integer[20];//记录每只小猫的重量
static int[] sum = new int[20];//每辆车的重量
static int n, m, ans = 20;
}
\ No newline at end of file
......@@ -62,11 +62,11 @@ public class 电路维修 {
private static int bfs(int x, int y) {
q.clear();
int t = Integer.MAX_VALUE / 2;
for (int i = 0; i < dist.length; i++) {
Arrays.fill(dist[i], t);
for (int[] ints : dist) {
Arrays.fill(ints, t);
}
for (int i = 0; i < vis.length; i++) {
Arrays.fill(vis[i], false);
for (boolean[] vi : vis) {
Arrays.fill(vi, false);
}
q.add(new node(x, y));
dist[x][y] = 0;
......
import java.io.*;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Random;
import java.util.StringTokenizer;
import static java.lang.System.in;
......@@ -31,7 +32,8 @@ public class IO加速 {
}
public static void main(String[] args) throws IOException {
f();
ff();
// f();
// tokenizer = new StringTokenizer("123123 15412 4312412");
// System.out.println(tokenizer.nextToken());
// System.out.println(tokenizer.nextToken());
......@@ -47,6 +49,29 @@ public class IO加速 {
// bw.flush();
}
static void ff() {
int a = 5, b = 5;
Random r = new Random();
int[] t = new int[3];
while (a > 0 && b > 0) {
for (int i = 0; i < 3; i++) {
int e=r.nextInt(13923);
System.out.println(e);
t[i] =e & 1;
}
if (t[0] == t[1] && t[1] == t[2]) {
a++;
b--;
} else {
b++;
a--;
}
System.out.println(a + " " + b);
}
System.out.println(a);
System.out.println(b);
}
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static BufferedReader reader = new BufferedReader(new InputStreamReader(in));
static StringTokenizer tokenizer = new StringTokenizer("");
......
......@@ -15,7 +15,7 @@ public class 新年好 {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
source[0] = 1;
source[0] = 1;//起点
for (int i = 1; i <= 5; i++) {
source[i] = sc.nextInt();//起点
}
......@@ -33,6 +33,12 @@ public class 新年好 {
System.out.println(dfs(1, 0, 0));
}
/**
* @param u 当前枚举到第几个
* @param start 当前起点
* @param dist 当前距离
* @return
*/
static int dfs(int u, int start, int dist) {
if (u == 6) return dist;
int res = Integer.MAX_VALUE / 2;
......@@ -51,7 +57,7 @@ public class 新年好 {
private static void spfa(int s, int[] dis) {
q.add(s);
Arrays.fill(dis, Integer.MAX_VALUE / 2);
Arrays.fill(vis,false);
Arrays.fill(vis, false);
dis[s] = 0;
while (!q.isEmpty()) {
int t = q.poll();
......
package graph.复合单源最短路;
/**
* 如果边权非负使用Dijkstra算法
*
*/
public class 道路与航路 {
}
package 递归;
import java.util.Arrays;
/**
* 给定n个数,取出m个数使得和为s,求方案数
* 给定n个数,取出k个数使得和为sum,求方案数
*/
public class m任取n满足 {
public static void main(String[] args) {
f(0, 0, 7);
for (int i = 0; i < 30; i++) {
arr[i] = i;
}
System.out.println(Arrays.toString(arr));
// dfs(0, 0, 7);
long a = System.nanoTime();
f(0, 0, 0);
System.out.println(ans);
long e = System.nanoTime();
System.out.println((e - a) / 1e8);
ans = 0;
a = System.nanoTime();
dfs(0, 0, 0);
System.out.println(ans);
e = System.nanoTime();
System.out.println((e - a) / 1e8);
}
static int ans, n = 6, m = 2;
static int[] arr = {2, 3, 5, 6, 1, 7};
static int ans, n = 30, k = 8, suma = 200;
static int[] arr = new int[30];
//u是当前第几位,sum是当前选了几个,s是当前和
/**
* 枚举每一位选或者不选
*
* @param u 是当前第几位
* @param sum 是当前选了几个
* @param s 当前和
*/
static void f(int u, int sum, int s) {
if (sum + n - u < m) return;//n-u是剩余可选的数,
if (u == 6) {
if (sum == 2 && s == 0)
if (sum + n - u < k) return;//n-u是剩余可选的数,
if (sum > k) return;
if (u == n) {
if (sum == k && s == suma)
ans++;
return;
}
if (u > n) return;
f(u + 1, sum, s);
f(u + 1, sum + 1, s - arr[u]);
f(u + 1, sum + 1, s + arr[u]);
}
static boolean[] x = new boolean[6];
//搜索策略
static void dfs(int u, int cnt) {
static boolean[] x = new boolean[30];
for (int i = 0; i < n; i++) {
/**
* 枚举
*
* @param u 当前选了多少个
* @param sum 当前和
* @param pos 下一次从哪一个索引开始
*/
static void dfs(int u, int sum, int pos) {
if (sum > suma || u > k) return;
if (sum == suma && u == k) {
ans++;
return;
}
for (int i = pos; i < n; i++) {
if (!x[u]) {
x[u] = true;
dfs(u + 1, cnt);
dfs(u + 1, sum + arr[i], i + 1);
x[u] = false;
}
}
}
......
......@@ -6,8 +6,10 @@ import static java.lang.System.in;
/**
* 狗可以感觉到地下沉。 他意识到骨头是一个陷阱,他拼命地走出了这个迷宫。
* <p>
* 迷宫是M的大小为N的矩形。迷宫里有一扇门。 一开始,门关闭,它将在第T秒打开一段时间(不到1秒)。 因此,小狗不得不在第T秒钟到达门口。 在每秒钟,他可以将一个块移动到上,下,左和右相邻块之一。 一旦他进入一个街区,这个街区的地面将在下一秒开始下沉并消失。 他不能停留在一个街区超过一秒钟,也不能进入一个被访问的块。 可怜的小狗可以生存吗? 请帮助他
* 迷宫是M的大小为N的矩形。迷宫里有一扇门。 一开始,门关闭,它将在第T秒打开一段时间(不到1秒)。
* 因此,小狗不得不在第T秒钟到达门口。 在每秒钟,他可以将一个块移动到上,下,左和右相邻块之一。
* 一旦他进入一个街区,这个街区的地面将在下一秒开始下沉并消失。
* 他不能停留在一个街区超过一秒钟,也不能进入一个被访问的块。 可怜的小狗可以生存吗? 请帮助他
* 'X': 不能进入
* 'S': 起点
* 'D': 出口
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册