首页 >> 大全

第十届蓝桥杯 2019年国赛真题 (Java 大学C组)

2023-10-20 大全 33 作者:考证青年

蓝桥杯 2019年国赛真题(Java 大学 C 组 )

给大专生出流问题,真牛啊

#A 奇数倍数

本题总分:5 分

问题描述

请你找到最小的整数 X 同时满足:

X 是 2019 的整倍数

X 的每一位数字都是奇数

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

public class Test {public static void main(String[] args) {agent: for (int i = 2019, n = 2019; true; n = i += 2019) {do if ((n & 1) == 0) continue agent;while ((n /= 10) > 0);System.out.println(i);break;}}
}

#B 递增序列

本题总分:5 分

问题描述

对于一个字母矩阵,我们称矩阵中的一个递增序列是指在矩阵中找到两个字母,它们在同一行,同一列,或者在同一 45 度的斜线上,这两个字母从左向右看、或者从上向下看是递增的。

例如,如下矩阵中

LANN

QIAO

有LN、LN、AN、AN、IO、AO、LQ、AI、NO、NO、AQ、IN、AN 等 13 个递增序列。注意当两个字母是从左下到右上排列时,从左向右看和从上向下看是不同的顺序。

对于下面的 30 行 50 列的矩阵,请问总共有多少个递增序列?(如果你把以下文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 inc.txt,内容与下面的文本相同)

VLPWJVVNNZSWFGHSFRBCOIJTPYNEURPIGKQGPSXUGNELGRVZAG
SDLLOVGRTWEYZKKXNKIRWGZWXWRHKXFASATDWZAPZRNHTNNGQF
ZGUGXVQDQAEAHOQEADMWWXFBXECKAVIGPTKTTQFWSWPKRPSMGA
BDGMGYHAOPPRRHKYZCMFZEDELCALTBSWNTAODXYVHQNDASUFRL
YVYWQZUTEPFSFXLTZBMBQETXGXFUEBHGMJKBPNIHMYOELYZIKH
ZYZHSLTCGNANNXTUJGBYKUOJMGOGRDPKEUGVHNZJZHDUNRERBU
XFPTZKTPVQPJEMBHNTUBSMIYEGXNWQSBZMHMDRZZMJPZQTCWLR
ZNXOKBITTPSHEXWHZXFLWEMPZTBVNKNYSHCIQRIKQHFRAYWOPG
MHJKFYYBQSDPOVJICWWGGCOZSBGLSOXOFDAADZYEOBKDDTMQPA
VIDPIGELBYMEVQLASLQRUKMXSEWGHRSFVXOMHSJWWXHIBCGVIF
GWRFRFLHAMYWYZOIQODBIHHRIIMWJWJGYPFAHZZWJKRGOISUJC
EKQKKPNEYCBWOQHTYFHHQZRLFNDOVXTWASSQWXKBIVTKTUIASK
PEKNJFIVBKOZUEPPHIWLUBFUDWPIDRJKAZVJKPBRHCRMGNMFWW
CGZAXHXPDELTACGUWBXWNNZNDQYYCIQRJCULIEBQBLLMJEUSZP
RWHHQMBIJWTQPUFNAESPZHAQARNIDUCRYQAZMNVRVZUJOZUDGS
PFGAYBDEECHUXFUZIKAXYDFWJNSAOPJYWUIEJSCORRBVQHCHMR
JNVIPVEMQSHCCAXMWEFSYIGFPIXNIDXOTXTNBCHSHUZGKXFECL
YZBAIIOTWLREPZISBGJLQDALKZUKEQMKLDIPXJEPENEIPWFDLP
HBQKWJFLSEXVILKYPNSWUZLDCRTAYUUPEITQJEITZRQMMAQNLN
DQDJGOWMBFKAIGWEAJOISPFPLULIWVVALLIIHBGEZLGRHRCKGF
LXYPCVPNUKSWCCGXEYTEBAWRLWDWNHHNNNWQNIIBUCGUJYMRYW
CZDKISKUSBPFHVGSAVJBDMNPSDKFRXVVPLVAQUGVUJEXSZFGFQ
IYIJGISUANRAXTGQLAVFMQTICKQAHLEBGHAVOVVPEXIMLFWIYI
ZIIFSOPCMAWCBPKWZBUQPQLGSNIBFADUUJJHPAIUVVNWNWKDZB
HGTEEIISFGIUEUOWXVTPJDVACYQYFQUCXOXOSSMXLZDQESHXKP
FEBZHJAGIFGXSMRDKGONGELOALLSYDVILRWAPXXBPOOSWZNEAS
VJGMAOFLGYIFLJTEKDNIWHJAABCASFMAKIENSYIZZSLRSUIPCJ
BMQGMPDRCPGWKTPLOTAINXZAAJWCPUJHPOUYWNWHZAKCDMZDSR
RRARTVHZYYCEDXJQNQAINQVDJCZCZLCQWQQIKUYMYMOVMNCBVY
ABTCRRUXVGYLZILFLOFYVWFFBZNFWDZOADRDCLIRFKBFBHMAXX

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

52800

import java.io.*;public class Test {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("inc.txt")));char[][] map = new char[30][];for (int i = 0; i < 30; i++)map[i] = in.readLine().toCharArray();int cnt = 0;int[] offsetN = { 0, 0, -1, 1, -1, -1, 1, 1 };int[] offsetM = { -1, 1, 0, 0, -1, 1, 1, -1 };for (int k = 0; k < 30; k++) {for (int g = 0; g < 50; g++) {char start = map[k][g];for (int z = 0; z < 8; z++) {for (int i = k + offsetN[z], j = g + offsetM[z]; i >= 0 && i < 30 && j >= 0 && j < 50; i += offsetN[z], j += offsetM[z])if (map[i][j] > start && (k < i || g < j)) cnt++;}}}System.out.println(cnt);}
}

没有具体读懂题目意思,但可以把它描述的限制全部加上,首先是 它们在同一行,同一列,或者在同一 45 度的斜线上 就是八个方位全覆盖到,再者 这两个字母从左向右看、或者从上向下看是递增的

完事

#C 平方拆分

本题总分:10 分

问题描述

将 2019 拆分为若干个两两不同的完全平方数之和,一共有多少种不同的方法?

注意交换顺序视为同一种方法,例如 132 + 252 + 352 = 2019 与 132 + 352 +252 = 2019 视为同一种方法。

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

52574

public class Test {static int cnt;public static void main(String[] args) {dfs(2019, -1);System.out.println(cnt);}static void dfs(int num, int start) {if (num < 0) return;if (num == 0) cnt++;else for (int i = start + 1, high = (int)Math.sqrt(num); i <= high; i++)dfs(num - i * i, i);}
}

如果保证拆分出的元素是递增的,那么拆分的方法是不会重复的

如果保证拆分出的元素是严格递增的,那么拆分的元素和方法都是不会重复的

这个道理在绝大多数情况下都适用

#D 切割

本题总分:10 分

问题描述

在 4 × 4 的方格矩阵中画一条直线。则直线穿过的方格集合有多少种不同的可能?这个里直线穿过一个方格当且仅当直线将该方格分割成面积都大于 0 的两部分。

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

不会

import java.util.*;public class Test {static final int size = 4;static final double precision = 0.01;static int[] HASH_TABLE = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };public static void main(String[] args) {Set<Integer> set = new HashSet();List<Line> grid = new ArrayList();List<Line.Point> top = new ArrayList(),left = new ArrayList(),right = new ArrayList(),bottom = new ArrayList();for (int i = 0; i <= size; i++) {grid.add(new Line(i, 0, i, size));grid.add(new Line(0, i, size, i));}for (double i = 0; i <= size; i += precision) {top.add(new Line.Point(i, size));left.add(new Line.Point(0, i));right.add(new Line.Point(size, i));bottom.add(new Line.Point(i, 0));}}static void calc(List<Line.Point> side, List<Line.Point> other, List<Line> grid, Set set) {int[][] cnt = new int[size + 2][size + 2];for (Line.Point A: side)for (Line.Point B: other) {clear(cnt);for (Line line: grid) {Line.Point point = Line.intersection(line, new Line(A, B));if (point == null) continue;// 不会}}}static void clear(int[][] a) {for (int i = a.length - 1; i >= 0; i--)Arrays.fill(a, 0);}static class Line {private Point A, B;public Line(Point A, Point B) {this.A = A;this.B = B;}public Line(double x1, double y1, double x2, double y2) {this.A = new Point(x1, y1);this.B = new Point(x2, y2);}static public Point intersection(Line line1, Line line2) {double A1 = line1.A.y - line1.B.y;double B1 = line1.B.x - line1.A.x;double A2 = line2.A.y - line2.B.y;double B2 = line2.B.x - line2.A.x;double det = A1 * B2 - A2 * B1;if (Math.abs(det) <= 1E-5) return null;double C1 = A1 * line1.A.x + B1 * line1.A.y;double C2 = A2 * line2.A.x + B2 * line2.A.y;double a = B2 / det;double b = -B1 / det;double c = -A2 / det;double d = A1 / det;return new Point(a * C1 + b * C2, c * C1 + d * C2);}public static class Point {double x, y;Point(double x, double y) {this.x = x;this.y = y;}public String toString() {return "Point{ x: " + x + ", y:" + y + " }";}}}
}

人工枚举一个 char 的可能性,也不太现实

或者人工枚举一边连接其他三边的状态,然后旋转判重

public class Test {static int[] HASH_TABLE = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };static int[][] matrix = {{0,  1,  2,  3 },{4,  5,  6,  7 },{8,  9,  10, 11},{12, 13, 14, 15}};public static void main(String[] args) {Set<Long> set = new HashSet();for (int k = 0; k < 4; k++) {set.add(calc(matrix[0][0]));// 这边请matrix = rightRotate(matrix);}System.out.println(set.size());}static int[][] rightRotate(int[][] a) {int[][] res = new int[a[0].length][a.length];for (int i = 0, l = a.length - 1; i < a.length; i++, l--)for (int j = 0; j < a[i].length; j++)res[j][l] = a[i][j];return res;}static long calc(int... visit) {long res = 1;for (int i = 0; i < visit.length; i++)res *= HASH_TABLE[visit[i]];return res;}
}

这是给大专生做的题?也太瞧得起人了吧

#E 序列求和

本题总分:15 分

问题描述

学习了约数后,小明对于约数很好奇,他发现,给定一个正整数 t,总是可以找到含有 t 个约数的整数。小明对于含有 t 个约数的最小数非常感兴趣,并把它定义为 St 。

例如 S1 = 1, S2 = 2, S3 = 4, S4 = 6,· · · 。

现在小明想知道,前 60 个 Si 的和是多少?即 S1 + S2 + · · · + S60 是多少?

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

||

public class Test {public static void main(String[] args) {int res = 0;int[] cnt = new int[62];for (int i = 1; true; i++) {int tmp = factors(i);if (tmp >= 60) {cnt[60] = cnt[61] = i;break;}if (cnt[tmp] == 0) cnt[tmp] = i;}for (int i = 60; i > 0; i--) {if (cnt[i] == 0 || cnt[i] > cnt[i + 1]) cnt[i] = cnt[i + 1];res += cnt[i];}System.out.print(res);}static int factors(long n) {int res = 1, now;for (int i = 2; i * i <= n; i++) {now = 0;while (n % i == 0) {now++;n /= i;}if (now > 0) {res *= now + 1;}}return n <= 1? res: (res << 1);}
}

一直在被人杠,就把网上的另一种解法写出来吧

先打个质数表

public class Test {public static void main(String[] args) {int[] prime = new int[100];prime[0] = 2; prime[1] = 3;agent: for (int i = 5, cur = 2; cur < 100; i += 2) {for (int r = (int)Math.sqrt(i), k = 0; prime[k] <= r; k++)if (i % prime[k] == 0) continue agent;prime[cur++] = i;}for (int i = 0; i < 100; i++)System.out.printf("%d, ", prime[i]);}
}

import java.util.HashMap;
import java.util.Map;public class Test {static final int[] prime = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541 };public static void main(String[] args) {Map<Integer, Double> map = new HashMap();long res = 0;map.put(1, 1.0);for (int i = 2; i <= 60; i++)resolve(i, 2, 0, i, map, new int[1024]);for (int i = 1; i <= 60; i++)res += map.get(i);System.out.println(res);}static void resolve(int n, int start, int cur, int val, Map map, int[] buff) {if (val == 1) calc(n, cur, map, buff);else while (start <= val) {if (val % start == 0) {buff[cur] = start;resolve(n, start, cur + 1, val / start, map, buff);}start++;}}static void calc(int n, int cur, Map<Integer, Double> map, int[] buff) {double total = 1;int i = 0;while (cur-- > 0)total *= Math.pow(prime[i++], buff[cur] - 1);if (map.get(n) == null || map.get(n) > total)map.put(n, total);}
}

#F 最长子序列

时间限制: 1.0s 内存限制: 512.0MB 本题总分:15 分

问题描述

我们称一个字符串 S 包含字符串 T 是指 T 是 S 的一个子序列,即可以从字符串 S 中抽出若干个字符,它们按原来的顺序组合成一个新的字符串与 T 完全一样。

给定两个字符串 S 和 T,请问 T 中从第一个字符开始最长连续多少个字符被 S 包含?

输入格式

输入两行,每行一个字符串。第一行的字符串为 S,第二行的字符串为 T。

两个字符串均非空而且只包含大写英文字母。

输出格式

输出一个整数,表示答案。

测试样例1

Input:
ABCDEABCD
AABZOutput:
3

评测用例规模与约定

对于 20% 的评测用例,1 ≤ |T| ≤ |S | ≤ 20;

对于 40% 的评测用例,1 ≤ |T| ≤ |S | ≤ 100;

对于所有评测用例,1 ≤ |T| ≤ |S | ≤ 1000。

code:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String S = in.readLine();String T = in.readLine();int j = 0;for (int i = 0, ih = S.length(), jh = T.length(); i < ih && j < jh; i++)if (S.charAt(i) == T.charAt(j)) j++;System.out.println(j);}
}

经典第一题友好

#G 数正方形

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

问题描述

在一个 N × N 的点阵上,取其中 4 个点恰好组成一个正方形的 4 个顶点,一共有多少种不同的取法?

由于结果可能非常大,你只需要输出模 109 + 7 的余数。

如上图所示的正方形都是合法的。

输入格式

输入包含一个整数 N。

输出格式

输出一个整数代表答案。

测试样例1

Input:
4Output:
20

评测用例规模与约定

对于所有评测用例,2 ≤ N ≤ 。

规律显然易见

code:

import java.io.IOException;
import java.io.InputStream;public class Main {static final int mod = 1000000007;public static void main(String[] args) throws IOException {long n = nextInt(System.in);long res = ((n - 1)* (n - 1)) % mod;for (int i = 2; i < n; i++)res = (res + (((n - i) * (n - i)) % mod) * i) % mod;System.out.println(res);}static int nextInt(InputStream in) throws IOException {int n = 0, c = in.read();while (c < '0' || c > '9') c = in.read();while (c >='0' && c <='9') {n = n * 10 + (c & 0xf);c = in.read();}return n;}
}

#H 矩阵计数

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

问题描述

一个 N × M 的方格矩阵,每一个方格中包含一个字符 O 或者字符 X。

要求矩阵中不存在连续一行 3 个 X 或者连续一列 3 个 X。

问这样的矩阵一共有多少种?

_2021年蓝桥杯国赛答案_蓝桥杯国一含金量

输入格式

输入一行包含两个整数 N 和 M。

输出格式

输出一个整数代表答案。

测试样例1

Input:
2 3Output:
49

评测用例规模与约定

对于所有评测用例,1 ≤ N, M ≤ 5。

code:

import java.io.IOException;
import java.io.InputStream;public class Main {static boolean[] map;static int n, m, end, cnt;public static void main(String[] args) throws IOException {n = nextInt(System.in);m = nextInt(System.in);end = n * m;map = new boolean[end];dfs(0);System.out.println(cnt);}static void dfs(int depth) {if (depth == end)  cnt++;else {dfs(depth + 1);if (check(depth)) return;map[depth] = true;dfs(depth + 1);map[depth] = false;}}static boolean check(int index) {int i = index / n, offset = i * n, j = index - offset, cnt = 1;for (int k = j - 1; k >= 0; k--) {if (map[offset + k]) cnt++;else break;}if (cnt >= 3) return true;cnt = 1;while (i-- > 0) {if (map[i * n + j]) cnt++;else break;}if (cnt >= 3) return true;return false;}static int nextInt(InputStream in) throws IOException {int n = 0, c = in.read();while (c < '0' || c > '9') c = in.read();while (c >='0' && c <='9') {n = n * 10 + (c & 0xf);c = in.read();}return n;}
}

因为数据规模很小,因此暴搜枝剪后也能在约定时间内拿到结果

还可以写一种实现简单的方法效验一下

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;public class Main {static int n, m, cnt;public static void main(String[] args) {InputReader in = new InputReader(System.in);List<boolean[]> rows = new ArrayList();n = in.nextInt();m = in.nextInt();buildRow(0, new boolean[m], rows);dfs(0, rows, new boolean[n][]);System.out.println(cnt);}static void dfs(int depth, List<boolean[]> rows, boolean[][] map) {if (depth == n) {for (int i = 0, cnt = 0; i < n; cnt = 0, i++)for (int j = 0; j < m; j++) {if (map[i][j]) cnt++;else cnt = 0;if (cnt == 3) return;}cnt++;} elsefor (boolean[] row: rows) {map[depth] = row;dfs(depth + 1, rows, map);}}static void buildRow(int depth, boolean[] row, List<boolean[]> rows) {if (depth == m) {for (int i = 0, cnt = 0; i < m; i++) {if (row[i]) cnt++;else cnt = 0;if (cnt == 3) return;}rows.add(Arrays.copyOf(row, m));} else {buildRow(depth + 1, row, rows);row[depth] = true;buildRow(depth + 1, row, rows);row[depth] = false;}}static class InputReader {BufferedReader read;StringTokenizer tok;String delimiters;InputReader(InputStream in) { this(in, " \n\t\r\f"); }InputReader(InputStream in, String delimiters) {this.read = new BufferedReader(new InputStreamReader(in));this.tok = new StringTokenizer("", "");this.delimiters = delimiters;}String next() {while (!tok.hasMoreTokens())try {tok = new StringTokenizer(read.readLine(), delimiters);} catch (IOException e) {e.fillInStackTrace();}return tok.nextToken();}int nextInt() { return Integer.parseInt(next()); }}
}

#I 大胖子走迷宫

时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分

问题描述

小明是个大胖子,或者说是个大大胖子,如果说正常人占用 1 × 1 的面积,小明要占用 5 × 5 的面积。由于小明太胖了,所以他行动起来很不方便。当玩一些游戏时,小明相比小伙伴就吃亏很多。

小明的朋友们制定了一个计划,帮助小明减肥。计划的主要内容是带小明玩一些游戏,让小明在游戏中运动消耗脂肪。走迷宫是计划中的重要环节。

朋友们设计了一个迷宫,迷宫可以看成是一个由 n × n 个方阵组成的方阵,正常人每次占用方阵中 1 × 1 的区域,而小明要占用 5 × 5 的区域。小明的位置定义为小明最正中的一个方格。迷宫四周都有障碍物。

为了方便小明,朋友们把迷宫的起点设置在了第 3 行第 3 列,终点设置在了第 n-2 行第 n-2 列。

小明在时刻 0 出发,每单位时间可以向当前位置的上、下、左、右移动单位 1 的距离,也可以停留在原地不动。小明走迷宫走得很辛苦,如果他在迷宫里面待的时间很长,则由于消耗了很多脂肪,他会在时刻 k 变成一个胖子,只占用 3 × 3 的区域。如果待的时间更长,他会在时刻 2k 变成一个正常人,只占用 1 × 1 的区域。注意,当小明变瘦时迷宫的起点和终点不变。

请问,小明最少多长时间能走到迷宫的终点。注意,小明走到终点时可能变瘦了也可能没有变瘦。

输入格式

输入的第一行包含两个整数 n, k。

接下来 n 行,每行一个由 n 个字符组成的字符串,字符为 + 表示为空地,

字符为 * 表示为阻碍物。

输出格式

输出一个整数,表示答案。

测试样例1

Input:
9 5
+++++++++
+++++++++
+++++++++
+++++++++
+++++++++
***+*****
+++++++++
+++++++++
+++++++++Output:
16

评测用例规模与约定

对于 30% 的评测用例,1 ≤ n ≤ 50。

对于 60% 的评测用例,1 ≤ n ≤ 100。

对于所有评测用例,1 ≤ n ≤ 300,1 ≤ k ≤ 1000。

code:

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;public class Test {public static void main(String[] args) {InputReader in = new InputReader(System.in);int n = in.nextInt(), k = in.nextInt();if (n <= 5) System.out.print('0');else {int map[][] = new int[n][n], INF = 0x3F3F3F3F;boolean[][] visit = new boolean[n][n];for (int i = 0, hi = n - 1; i <= hi; i++) {if (i < hi)map[1][i] = map[hi - 1][i] = map[i][1] = map[i][hi - 1] = 1;map[0][i] = map[hi][i] = map[i][0] = map[i][hi] = 2;}int[] os2i = { -1, -1, 0, 1, 1, 1, 0, -1 };int[] os2j = { 0, 1, 1, 1, 0, -1, -1, -1 };int[] os1i = { -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2 };int[] os1j = { 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1 };for (int i = 0, x, y; i < n; i++)for (int j = 0; j < n; j++) {if (in.split() == '*') {map[i][j] = INF;for (int l = 0; l < 8; l++) {x = i + os2i[l];y = j + os2j[l];if (x < 0 || x >= n || y < 0 || y >= n || map[x][y] > 2) continue;map[x][y] = 2;}for (int l = 0; l < 16; l++) {x = i + os1i[l];y = j + os1j[l];if (x < 0 || x >= n || y < 0 || y >= n || map[x][y] > 1) continue;map[x][y] = 1;}}}class Step {int x, y, time;Step (int x, int y, int time) {this.x = x;this.y = y;this.time = time;}Step relax() {this.time++;return this;}}Queue<Step> queue = new LinkedList();int[] offsetX = { -1, 0, 1, 0 };int[] offsetY = { 0, 1, 0, -1 };int endX = n - 3, endY = n - 3;queue.offer(new Step(2, 2, 0));while (queue.size() > 0) {Step now = queue.poll();if (now.x == endX && now.y == endY) {System.out.print(now.time);break;}for (int i = 0, x, y, s; i < 4; i++) {x = now.x + offsetX[i];y = now.y + offsetY[i];s = now.time / k;if (x < 0 || x >= n || y < 0 || y >= n || visit[x][y] || map[x][y] > s) continue;visit[x][y] = true;queue.offer(new Step(x, y, now.time + 1));}queue.offer(now.relax());}}}static class InputReader {InputStream in;int next, len;byte[] buff;InputReader(InputStream in) { this(in, 8192); }InputReader(InputStream in, int buffSize) {this.buff = new byte[buffSize];this.next = this.len = 0;this.in = in;}int getByte() {if (next >= len)try {next = 0;len = in.read(buff);if (len == -1) return -1;} catch (IOException e) {e.fillInStackTrace();}return buff[next++];}int split() {int c = getByte();while (c <= 32 || c == 127) c = getByte();return c;}int nextInt() {int n = 0, c = split();boolean flag = true;if (c == '-') {c = getByte();flag = false;}while (c >= '0' && c <= '9') {n = n * 10 + (c & 0xf);c = getByte();}return flag? n: -n;}}
}

广搜单源最短路

题目不难,优化麻烦

这里地图数字的意思是在 map[i][j] * k 的时间可以达到此地

其实可以把边界也看做是障碍物,这样用 O(n) 的空间就可以换取每一步减少 4个越界判断,但想到的时候已经快写完了,就算了,不会对其性质造成影响

#J 估计人数

时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分

问题描述

给定一个 N × M 的方格矩阵,矩阵中每个方格标记 0 或者 1 代表这个方格是不是有人踩过。

已知一个人可能从任意方格开始,之后每一步只能向右或者向下走一格。

走了若干步之后,这个人可以离开矩阵。这个人经过的方格都会被标记为 1,包括开始和结束的方格。注意开始和结束的方格不需要一定在矩阵边缘。

请你计算至少有多少人在矩阵上走过。

输入格式

输入第一行包含两个整数 N、M。

以下 N 行每行包含 M 个整数 (0/1),代表方格矩阵。

输出格式

输出一个整数代表答案。

测试样例1

Input:
5 5
00100
11111
00100
11111
00100Output:
3

评测用例规模与约定

对于所有评测用例,1 ≤ N, M ≤ 20,标记为 1 的方格不超过 200 个。

code:

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;public class Main {static int V = 1;static int source[];static boolean graph[][], marked[];public static void main(String[] args) {InputReader in = new InputReader(System.in);int n = in.nextInt(), m = in.nextInt();int idx[][] = new int[n + 1][m + 1];for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (in.split() == '1')idx[i][j] = V++;graph = new boolean[V][V];marked = new boolean[V];source = new int[V];for (int i = 0, v; i < n; i++)for (int j = 0; j < m; j++)if (idx[i][j] > 0) {v = idx[i][j];if (idx[i + 1][j] > 0)graph[v][idx[i + 1][j]] = true;if (idx[i][j + 1] > 0)graph[v][idx[i][j + 1]] = true;}for (int k = 1; k < V; k++)for (int i = 1; i < V; i++)for (int j = 1; j < V; j++)graph[i][j] |= graph[i][k] & graph[k][j];int cnt = 0;for (int i = 1; i < V; i++) {Arrays.fill(marked, false);cnt += dfs(i)? 1: 0;}System.out.print(V - cnt - 1);}static boolean dfs(int v) {for (int i = 1; i < V; i++) {if (graph[v][i]) {if (marked[i]) continue;marked[i] = true;if (source[i] == 0 || dfs(source[i])) {source[i] = v;return true;}}}return false;}static class InputReader {InputStream in;int next, len;byte[] buff;InputReader(InputStream in) { this(in, 8192); }InputReader(InputStream in, int buffSize) {this.buff = new byte[buffSize];this.next = this.len = 0;this.in = in;}int getByte() {if (next >= len)try {next = 0;len = in.read(buff);if (len == -1) return -1;} catch (IOException e) {e.fillInStackTrace();}return buff[next++];}int split() {int c = getByte();while (c <= 32 || c == 127) c = getByte();return c;}int nextInt() {int n = 0, c = split();boolean flag = true;if (c == '-') {c = getByte();flag = false;}while (c >= '0' && c <= '9') {n = n * 10 + (c & 0xf);c = getByte();}return flag? n: -n;}}
}

匈牙利算法,难点在于构图,影响中蓝桥练习系统里面有道简单题练的就是这种构图,懒得去找了

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了