前言


蓝桥杯全国软件和信息技术专业人才大赛是由工业和信息化部人才交流中心举办的全国性IT学科赛事。 蓝桥杯在多数高校校被评为二类竞赛,级别相当高,对于保研评优找工作都起着相当大的作用。 本博客用于及时复习蓝桥杯知识点,将本人所学分享给大家,由于博主水平有限,如有不足,敬请谅解。


文章目录





常用技巧

日期问题

具体到星期几【可使用windows计算机器】

package ch01;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000
Scanner cin = new Scanner(System.in);
Calendar c = Calendar.getInstance();
c.set(2020, 10,9);//月份从0开始
System.out.println(c.get(Calendar.DAY_OF_WEEK) - 1);
}

}

【Windows计算器实现日期计算】【日期之差】【日期对应星期几】

2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_java
2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_职场和发展_02

… 日期格式化 - parse

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000
Scanner cin = new Scanner(System.in);
String a[] = cin.next().split("/");
String t[] = new String[3];
t[0] = ((Integer.valueOf(a[0]) >= 60) ? "19" + a[0] : "20" + a[0]) + "-" + a[1] + "-" + a[2];
t[1] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[0] + "-" + a[1];
t[2] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[1] + "-" + a[0];
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
sf.setLenient(false);
Set<String> s = new TreeSet<String>();
for (String T : t) {
try {
sf.parse(T);
} catch (ParseException e) {
continue;
}
s.add(T);
}
for(String T:s)
System.out.println(T);
}
}

Java排序

Java优先队列重载

PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>() {

@Override
public int compare(Node o1, Node o2) {
// TODO Auto-generated method stub
return o1.costT - o2.costT;
}

});

交叉排序【内部类实现排序】【熟记】

​交叉排序​​​2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_职场和发展_03

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Integer a[];
Integer n = cin.nextInt();
a = new Integer[n + 5];
int l1 = cin.nextInt();
int r1 = cin.nextInt();
int l2 = cin.nextInt();
int r2 = cin.nextInt();
for(int i = 1;i <= n;i++)
a[i] = cin.nextInt();
Arrays.sort(a, l1, r1 + 1);
Arrays.sort(a, l2, r2 + 1, new Comparator<Integer>(){

@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method
return o2 - o1;
}

});
for(int i = 1;i <= n;i++)
if(i == 1)
System.out.print(a[i]);
else System.out.print(" " + a[i]);
System.out.println();
}
}

内部类排序【动态数组,排序自定义,foreach输出】

2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_职场和发展_04

​成绩排序​

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
static class node{
int c; int m; int e; int s;
int sum;
String name;
node(String name,int c, int m, int e, int s){
this.name = name;
this.c = c;
this.m = m;
this.e = e;
this.s = s;
this.sum = c + m + e + s;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
ArrayList<node>ar = new ArrayList<node>();
int n = cin.nextInt();
for(int i = 1;i <= n;i++)
{
String name = cin.next();
int c = cin.nextInt();
int m = cin.nextInt();
int e = cin.nextInt();
int s = cin.nextInt();
node nd = new node(name, c, m, e, s);
ar.add(nd);
}
ar.sort(new Comparator<node>() {

@Override
public int compare(node o1, node o2) {
// TODO Auto-generated method stub
if(o1.c == o2.c)return o1.name.compareTo(o2.name);
else return o2.c - o1.c;
}

});
int cnt = 0;
for(node T:ar) {
cnt++;
if(cnt == 1)
System.out.print(T.name);
else System.out.print(" " + T.name);
if(cnt == 4)break;
}

System.out.println();
ar.sort(new Comparator<node>() {

@Override
public int compare(node o1, node o2) {
// TODO Auto-generated method stub
if(o1.m == o2.m)return o1.name.compareTo(o2.name);
else return o2.m - o1.m;
}

});
cnt = 0;
for(node T:ar) {
cnt++;
if(cnt == 1)
System.out.print(T.name);
else System.out.print(" " + T.name);
if(cnt == 4)break;
}

System.out.println();
ar.sort(new Comparator<node>() {

@Override
public int compare(node o1, node o2) {
// TODO Auto-generated method stub
if(o1.e == o2.e)return o1.name.compareTo(o2.name);
else return o2.e - o1.e;
}

});
cnt = 0;
for(node T:ar) {
cnt++;
if(cnt == 1)
System.out.print(T.name);
else System.out.print(" " + T.name);
if(cnt == 4)break;
}

System.out.println();
ar.sort(new Comparator<node>() {

@Override
public int compare(node o1, node o2) {
// TODO Auto-generated method stub
if(o1.s == o2.s)return o1.name.compareTo(o2.name);
else return o2.s - o1.s;
}

});
cnt = 0;
for(node T:ar) {
cnt++;
if(cnt == 1)
System.out.print(T.name);
else System.out.print(" " + T.name);
if(cnt == 4)break;
}

System.out.println();
ar.sort(new Comparator<node>() {

@Override
public int compare(node o1, node o2) {
// TODO Auto-generated method stub
if(o1.sum == o2.sum)return o1.name.compareTo(o2.name);
else return o2.sum - o1.sum;
}

});
cnt = 0;
for(node T:ar) {
cnt++;
if(cnt == 1)
System.out.print(T.name);
else System.out.print(" " + T.name);
if(cnt == 4)break;
}

System.out.println();
}
}

正则表达式(用于处理分割字符串)

​详解链接​​​​题目链接​

class Solution {
public int numDifferentIntegers(String word) {
if(word.length() == 0) return word.length();
String newStr = word.replaceAll("[a-z]+", " ");//代替a到z的全部元素
String[] s = newStr.split(" ");
HashMap<String, Integer> map = new HashMap<>();
for (String s1 : s) {
if(s1.equals("")){
continue;
}
String s2 = s1.replaceAll("^(0+)", "");//匹配从0开头所有元素0
map.put(s2, map.getOrDefault(s2, 0)+1);//如果map种没有s2这个key,则用0作为它的value
}
return map.size();
}
}

图论

最小生成树

import java.util.*;

public class Main {
static class Node{
int u, v, w;
Node(int u, int v, int w){
this.u = u;
this.v = v;
this.w = w;
}
}
static Node nd[];
static Vector<Integer> ar[];
static int f[];
static int find(int x){
if(x == f[x])
return x;
return f[x] = find(f[x]);
}
static void setUnion(int x, int y){
x = find(x);
y = find(y);
f[x] = y;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
n = n * (n - 1) / 2;
f = new int[n + 5];
nd = new Node[n + 5];
for(int i = 0;i < n;i++){
int u = cin.nextInt();
int v = cin.nextInt();
int w = cin.nextInt();
int f = cin.nextInt();
if(f == 1)w = 0;
nd[i] = new Node(u, v, w);
}
Arrays.sort(nd, 0, n, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.w - o2.w;
}
});
int cost = 0;
for(int i = 1;i <= n;i++)
f[i] = i;
for(int i = 0;i < n;i++){
int x = find(nd[i].u);
int y = find(nd[i].v);

if(x == y)continue;//都在一棵树上,pass
cost += nd[i].w;
f[x] = y;
}

System.out.println(cost);

}
}

最短路

floyd模板

算法分析



f[i, j, k]表示从i走到j的路径上除i和j点外只经过1到k的点的所有路径的最短距离。那么f[i, j, k] = min(f[i, j, k - 1), f[i, k, k - 1] + f[k, j, k - 1]。 因此在计算第k层的f[i, j]的时候必须先将第k - 1层的所有状态计算出来,所以需要把k放在最外层。

读入邻接矩阵,将次通过动态规划装换成从i到j的最短距离矩阵

在下面代码中,判断从a到b是否是无穷大距离时,需要进行if(t > INF/2)判断,而并非是if(t == INF)判断,原因是INF是一个确定的值,并非真正的无穷大,会随着其他数值而受到影响,t大于某个与INF相同数量级的数即可。


代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);

int n = cin.nextInt();
int m = cin.nextInt();
int x = cin.nextInt();
int dis[][] = new int[n + 5][n + 5];
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i == j)dis[i][j] = 0;
else
dis[i][j] = 99999999;



for(int i = 1;i <= m;i++) {
int u = cin.nextInt();
int v = cin.nextInt();
int w = cin.nextInt();
dis[u][v] = Math.min(w, dis[u][v]);
}

for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]);
while(x--!=0) {
int u = cin.nextInt();
int v = cin.nextInt();
if(dis[u][v] >= 99999999 / 2)System.out.println("impossible");
else System.out.println(dis[u][v]);
}
}
}

牛的旅行 floyd(多源最短路)

​题目链接​

import java.util.Scanner;


public class Main {
static double INF = 0x3f3f3f3f;
static double len(double x1, double y1, double x2, double y2){
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
double zb[][] = new double[n + 5][2];
char map[][] = new char[n + 5][n + 5];
double dis[][] = new double[n + 5][n + 5];
double maxDis[] = new double[n + 5];
for(int i = 1;i <= n;i++){
zb[i][0] = cin.nextInt();
zb[i][1] = cin.nextInt();
}

for(int i = 1;i <= n;i++){
String s = cin.next();
for(int j = 1;j <= s.length();j++)
map[i][j] = s.charAt(j - 1);
}

for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i == j)dis[i][j] = 0;
else dis[i][j] = INF;

for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(map[i][j] == '1')
dis[i][j] = len(zb[i][0], zb[i][1], zb[j][0], zb[j][1]);

for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
dis[i][j] = Math.min(dis[i][j], dis[i][k] + dis[k][j]);
double ans1 = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(dis[i][j] != INF){
maxDis[i] = Math.max(maxDis[i], dis[i][j]);
ans1 = Math.max(ans1, maxDis[i]);
}
double ans = INF;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(dis[i][j] > INF / 2){
ans = Math.min(ans, maxDis[i] + len(zb[i][0], zb[i][1], zb[j][0], zb[j][1]) + maxDis[j]);
}
System.out.printf("%.6f",Math.max(ans, ans1));
}
}

数论

欧拉筛法【O(nlogn)级别快速求质数】

​链接​​ 长度为11的等差素数列,其公差最小值是多少?

package ch01;

public class isPrime {

static int []prime = new int[100005];
static boolean []vis = new boolean[100005];
static int cnt = 0, count = 0;
static void sieve(int n)
{
cnt = 0;
for(int i = 2; i <= n; i++)
{
if(!vis[i]) //不是目前找到的素数的倍数 是素数
prime[cnt++] = i; //找到素数
for(int j = 0; j < cnt && i * prime[j] <= n; j++)
{
vis[i * prime[j]] = true; //找到的素数的倍数不访问
if(i % prime[j] == 0) break; //关键!!!!
}
}
}
public static void main(String[] args) {
sieve(100000);
for(int i = 1;i <= 1000;i++) {
for(int j = 0;j < cnt;j++) {
int sum = prime[j];int k = 1;
while(true) {

count++;
if(sum > 100000 || vis[sum] || k == 11) {//不是素数break
break;
}
sum += i;
k++;
}
if(k == 11) {
System.out.println(i);
System.out.println(count);
System.exit(0);
}
}
}
}
}

辗转相除法求最大公约数【熟记】

static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

动态规划

二维动态规划

​题目链接​​​2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_职场和发展_05

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;

class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int sum = cin.nextInt();
int dp[][] = new int[n + 15][sum + 15];
dp[1][1] = 1;
dp[1][2] = 1;
dp[1][3] = 1;
dp[1][4] = 1;
dp[1][5] = 1;
dp[1][6] = 1;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= sum;j++)
for(int k = 1;k <= 6 && j > k;k++)
dp[i][j] = (dp[i][j] + dp[i - 1][j - k])%998244353;
System.out.println(dp[n][sum]);
}
}

数据结构

HashMap 嵌套 Set

​题目链接​

class Solution {
public int[] findingUsersActiveMinutes(int[][] logs, int k) {
int ans[] = new int[k];
HashMap<Integer, Integer>ansp = new HashMap<>();
HashSet<Integer>sst = new HashSet<>();
HashMap<Integer, HashSet<Integer>>mp = new HashMap<>();
for(int i = 0;i < logs.length;i++){
int id = logs[i][0];//id
int t = logs[i][1];//时间
sst.add(id);
if(mp.containsKey(id)){
if(!mp.get(id).contains(t)){
mp.get(id).add(t);
// mp.put(id, st);
ansp.put(id, ansp.get(id) + 1);
// System.out.println(ansp.get(id));
}
}else{
HashSet<Integer>st = new HashSet<>();
st.add(t);
mp.put(id, st);
ansp.put(id,1);
}
// System.out.println(ansp.get(id));
}

for(Integer t:sst)
ans[ansp.get(t) - 1]++;
return ans;
}
}

队列

并查集

使用并查集判断连通块数量【模板】

​题目连接​

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

class Main {
static int n, m;
static int fa[] = new int[20005];

static void makeSet(int size) {
for (int i = 1; i <= size; i++)
fa[i] = i; // i就在它本身的集合里
return;
}

static int find(int x) {
if (x != fa[x]) // x不是自身的父亲,即x不是该集合的代表
fa[x] = find(fa[x]); // 查找x的祖先直到找到代表,于是顺手路径压缩
return fa[x];
}

static void unionSet(int x, int y) {
// x 与 y 所在家族合并
x = find(x);
y = find(y);
fa[x] = y; // 把 x 的祖先变成 y 的祖先的儿子
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
m = cin.nextInt();
makeSet(n);
for (int i = 1; i <= m; i++) {
int u = cin.nextInt();
int v = cin.nextInt();
unionSet(u, v);
}
int cnt = 0;
for(int i = 1;i <= n;i++)
if(fa[i] == i)cnt++;
System.out.println(cnt);
}
}
import java.util.*;

public class Main {
static class Node{
String name;
int num;
Node(String name, int num){
this.name = name;
this.num = num;
}
}
static Vector<Integer> ar[];
static int f[];
static int find(int x){
if(x == f[x])
return x;
return f[x] = find(f[x]);
}
static void setUnion(int x, int y){
x = find(x);
y = find(y);
f[x] = y;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
ar = new Vector[n + 5];
f = new int[n + 5];
int flag = 0;
for(int i = 0;i < n + 5;i++)
ar[i] = new Vector<>();
for(int i = 1;i <=n;i++ )
f[i] = i;
for(int i = 0;i < m;i++){
int u = cin.nextInt();
int v = cin.nextInt();
setUnion(u,v);
ar[u].add(v);
ar[v].add(u);
}
for(int i = 1;i <= n;i++){
if(ar[i].size() % 2 == 1)flag = 1;
}
int cnt = 0;
for(int i = 1;i <= n;i++)
if(i == f[i])cnt++;
if(cnt == 1 && flag == 0) System.out.println("1");
else System.out.println("0");
}
}

优先队列

标题:第几个幸运数

到x星球旅行的游客都被发给一个整数,作为游客编号。
x星的国王有个怪癖,他只喜欢数字3,5和7。
国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。

我们来看前10个幸运数字是:
3 5 7 9 15 21 25 27 35 45
因而第11个幸运数字是:49

小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。

请你帮小明计算一下,59084709587505是第几个幸运数字。

需要提交的是一个整数,请不要填写任何多余内容。
import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.TreeSet;

public class Test2 {
public static void main(String[] args) {
PriorityQueue<Long> q = new PriorityQueue<Long>();
TreeSet<Long> st = new TreeSet<Long>();
q.add((long) 1);
int cnt = 0;
while (!q.isEmpty()) {
long x = q.poll();
System.out.println(x);
if(("" + x).equals("59084709587505"))break;
if (!st.contains(x * 3)) {
q.add(x * 3);
st.add(x * 3);

cnt++;
}
if (!st.contains(x * 5)) {
q.add(x * 5);
st.add(x * 5);
cnt++;
}
if (!st.contains(x * 7)) {
q.add(x * 7);
st.add(x * 7);
cnt++;
}

}
cnt = 0;
for(Long T:st) {
cnt++;
if((""+T).equals("59084709587505")) {
System.out.println(cnt);
break;
}
}
}
}

二分

寻找第一个大于目标数的数

package ch01;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {// 年/月/日的,有采用月/日/年的,还有采用日/月/年的000
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
long vis[] = new long[100005];
int a[][] = new int[3][n + 5];
for (int i = 0; i < n; i++) {
a[0][i] = cin.nextInt();
}
for (int i = 0; i < n; i++) {
a[1][i] = cin.nextInt();
}
for (int i = 0; i < n; i++) {
a[2][i] = cin.nextInt();
}
Arrays.sort(a[0], 0, n);
Arrays.sort(a[1], 0, n);
Arrays.sort(a[2], 0, n);
long res = 0;
int ff = 0;
long max = 0;
for (int i = 0; i < n; i++) {

int l = 0, r = n;
while (l <= r) {
int mid = (l + r) >>> 1;
if (a[0][i] < a[1][mid]) {
r = mid - 1;
} else
l = mid + 1;
}
int pos = a[1][l];
// System.out.println(pos);
if (l >= n)
break;
if (l != 0) {
if (vis[a[1][l]] != 0)
res += max - vis[a[1][l - 1]];
} else
res += max;
// System.out.println(res + " " + a[1][l] + " " + vis[a[1][l - 1]]);
// System.out.println(pos);
// System.out.println(vis[a[1][l - 1]]);
int flag = l;
if (ff == 0) {
while (true) {
l = 0;
r = n;
ff = 1;
while (l <= r) {
int mid = (l + r) >>> 1;
if (a[1][flag] < a[2][mid]) {
r = mid - 1;
} else
l = mid + 1;
}
// System.out.println(a[2][l] + " sd");
if (l >= n)
break;
res += (long) (n - l);
if (flag == 0)
vis[a[1][flag]] = n - l;
else
vis[a[1][flag]] = res;
flag++;
max = res;
if (flag == n)
break;
}
}

}
System.out.println(res);
}

}

搜索

dfs

2020年JavaB组国赛 E:玩具蛇

2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_蓝桥杯_06
思路: 经典dfs,搜出每一种情况。

public class Main {
static int map[][] = new int[20][20];
static int cnt;
static void dfs(int x, int y, int num) {
if(x < 1 || y < 1 || x > 4 || y > 4)return ;
if(map[x][y] != 0)return ;
map[x][y] = num;
if(num == 16) {
cnt++;

for(int i = 1;i <= 4;i++) {
for(int j = 1;j <= 4;j++)
System.out.print(map[i][j] + " ");
System.out.println();
}
System.out.println();
map[x][y] = 0;
return ;
}
dfs(x + 1, y, num + 1);
dfs(x - 1, y, num + 1);
dfs(x, y + 1, num + 1);
dfs(x, y - 1, num + 1);
map[x][y] = 0;
}
public static void main(String[] args) {
for(int i = 1;i <= 4;i++)
for(int j = 1;j <= 4;j++)
dfs(i, j, 1);
System.out.println(cnt);
}
}

判断是否能表示成3的幂数和

​题目链接​

class Solution {
static int f;
void dfs(long sum, int pos, int index){
// System.out.println(sum + " " + pos);
if(f == 1 || sum > pos)return ;
if(sum == pos){
f = 1;
return ;
}
for(int i = index;i <= 15;i++){
dfs(sum + (int)Math.pow(3, i), pos, i + 1);
}
}
public boolean checkPowersOfThree(int n) {
f = 0;
dfs(0, n, 0);
if(f == 1)return true;
return false;
}
}

搜索枚举各类情况

​题目链接​

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;


class Main {
static String s;
static int k;
static int min;

static void dfs(String s, int z, int res) {
if(res < 0)return ;
//System.out.println(res);
if(res == 0) {
System.out.println(k + "=" +s.substring(0, s.length() - 1));
return;
}
for(int i = z;i < k;i++)
dfs(s + i + '+', i, res - i);
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
k = cin.nextInt();
dfs("", 1, k);
}
}

​题目链接​

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;


class Main {
static String s;
static int k;
static int min;

static void dfs(int l, int r, int cs, int res) {

if (cs == k - 1) {
// System.out.println("-->" + (res + Integer.valueOf(s.substring(r, s.length()))));
min = Math.min(res + Integer.valueOf(s.substring(r, s.length())), min);
return;
}
for (int i = r + 1; i < s.length(); i++) {
// System.out.println(s.substring(r, i));
dfs(r, i, cs + 1, res + Integer.valueOf(s.substring(r, i)));
}
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
s = cin.next();
k = cin.nextInt();
min = 9999999;
dfs(0, 0, 0, 0);
System.out.println(min);
}
}

类似八皇后

​题目连接​

import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

class Main {
static int n, m;
static int vis[] = new int[10 + 5];
static char a[][] = new char[10 + 5][10 + 5];
;
static int cnt;
static void dfs(int r, int sp) {
if (m == sp) {
cnt++;
return;
}
if(r == n + 1)return ;
for (int i = 0; i < n; i++) {
if (a[r][i] == '#' && vis[i] == 0) {
vis[i] = 1;
dfs(r + 1, sp + 1);
vis[i] = 0;
}
}
dfs(r + 1, sp);
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(true) {
n = cin.nextInt();
m = cin.nextInt();
if(n == -1 && m == -1)break;
for (int i = 0; i < n; i++) {
String s = cin.next();
for (int j = 0; j < n; j++) {
a[i][j] = s.charAt(j);
// System.out.println(s);
}
}
cnt = 0;
dfs(0, 0);
System.out.println(cnt);
}
}
}

判断联通块数量

​题目连接​

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

class Main {
static int n, m;
static ArrayList<Integer> ar[] = new ArrayList[20005];
static int vis[] = new int[20005];
static void dfs(int u) {
for (int i = 0; i < ar[u].size(); i++) {
if (vis[ar[u].get(i)] == 0) {
vis[ar[u].get(i)] = 1;
dfs(ar[u].get(i));
}
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
m = cin.nextInt();
for (int i = 0; i < 20005; i++)
ar[i] = new ArrayList<>();
for (int i = 0; i < m; i++) {
int u = cin.nextInt();
int v = cin.nextInt();
ar[u].add(v);
ar[v].add(u);
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (vis[i] != 1) {
cnt++;
vis[i] = 1;
dfs(i);
}
System.out.println(cnt);
}
}

bfs

迷宫【bfs模板】

2021蓝桥杯Java复习【史上最详细攻略】【持续更新】_ide_07

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {// date1 = "2019-06-29", date2 = "2019-06-30"
static class Node{
int x;
int y;
int step;
Node(int x, int y, int step){
this.x = x;
this.y = y;
this.step = step;
}
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int dir[][] = {{1, 0},{-1, 0},{0, 1},{0, -1}};
int vis[][];
char a[][];
int tran[][][];
int n = cin.nextInt();
int m = cin.nextInt();
a = new char[n + 5][m + 5];
vis = new int[n + 5][m + 5];
tran = new int[n + 5][m + 5][2];
for (int i = 1; i <= n; i++) {
String s = cin.next();
for (int j = 1; j <= m; j++) {
a[i][j] = s.charAt(j - 1);
}
}

int qx = cin.nextInt();
for(int i = 0;i < qx;i++) {
int x = cin.nextInt();
int y = cin.nextInt();
tran[x][y][0] = cin.nextInt();
tran[x][y][1] = cin.nextInt();
}
int tx = cin.nextInt();
int ty = cin.nextInt();
Queue<Node> q = new LinkedList<Node>();
q.add(new Node(1, 1, 0));

while(!q.isEmpty()) {
Node node = q.poll();
if(node.x == tx && node.y == ty) {
System.out.println(node.step);
System.exit(0);
}
if(tran[node.x][node.y][0] != 0) {
int dx = tran[node.x][node.y][0];
int dy = tran[node.x][node.y][1];
if(dx == tx && dy == ty) {
System.out.println(node.step);
System.exit(0);
}
if(a[dx][dy] == '*') {
System.out.println("No solution");
System.exit(0);
}else {
if(vis[dx][dy] == 1)continue;
vis[dx][dy] = 1;
q.add(new Node(dx, dy, node.step));
// continue;
}
continue;
}
for(int i = 0; i < 4;i++) {
int nx = node.x + dir[i][0];
int ny = node.y + dir[i][1];
if(nx <= 0 || nx > n || ny <= 0 || ny > m || vis[nx][ny] == 1 || a[nx][ny] == '*')continue;
vis[nx][ny] = 1;
q.add(new Node(nx, ny, node.step + 1));
}

}
System.out.println("No solution");
}
}

背包

01背包

​01背包问题​

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;

class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int N = cin.nextInt();
int V = cin.nextInt();
int v[] = new int[1005];
int w[] = new int[1005];
int dp[] = new int[1005];
for(int i = 1;i <= N;i++) {
v[i] = cin.nextInt();
w[i] = cin.nextInt();
}
for(int i = 1;i <= N;i++)
for(int j = V;j >= v[i];j--)
dp[j] = Math.max(dp[j], dp[j - v[i]] + w[i]);
System.out.println(dp[V]);
}
}