Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a898a5e

Browse files
update
1 parent ab735ed commit a898a5e

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

‎src/其他/abwl/q1/Main.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package 其他.abwl.q1;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Scanner in = new Scanner(System.in);
9+
String[] s = in.nextLine().split(" ");
10+
if (s.length < 1) {
11+
return;
12+
}
13+
int k = Integer.parseInt(s[0]);
14+
if (k == 0) {
15+
return;
16+
}
17+
List<Integer> nums = new ArrayList<>();
18+
for (int i = 1; i < s.length; i++) {
19+
nums.add(Integer.parseInt(s[i]));
20+
}
21+
in.close();
22+
23+
int sp = nums.size() / k;
24+
25+
Map<Integer, Integer> map = new HashMap<>();
26+
for (int i = 0; i < nums.size(); i++) {
27+
if (map.containsKey(nums.get(i))) {
28+
int total = map.get(nums.get(i)) + 1;
29+
if (total > sp) {
30+
System.out.print(nums.get(i));
31+
return;
32+
}
33+
map.put(nums.get(i), total);
34+
} else {
35+
map.put(nums.get(i), 1);
36+
}
37+
}
38+
}
39+
}

‎src/其他/abwl/q2/Main.java‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package 其他.abwl.q2;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
private static int MAX = 256;
7+
public static int[][] weight;
8+
9+
public static int getVal(int[][] graph, int index) {
10+
return graph[index / graph.length][index % graph.length];
11+
}
12+
13+
public static int getX(int[][] graph, int index) {
14+
return index / graph.length;
15+
}
16+
17+
public static int getY(int[][] graph, int index) {
18+
return index % graph.length;
19+
}
20+
21+
public static void main(String[] args) {
22+
Scanner in = new Scanner(System.in);
23+
int width = in.nextInt();
24+
int height = in.nextInt();
25+
int startX = in.nextInt();
26+
int startY = in.nextInt();
27+
int[][] graph = new int[height][width];
28+
int total = height * width;
29+
weight = new int[total][total];
30+
for (int i = 0; i < height; i++) {
31+
for (int j = 0; j < width; j++) {
32+
graph[i][j] = in.nextInt();
33+
}
34+
}
35+
for (int i = 0; i < total; i++) {
36+
for (int j = 0; j < total; j++) {
37+
weight[i][j] = MAX;
38+
}
39+
}
40+
List<Integer> list = new ArrayList<>();
41+
for (int i = 0; i < weight.length; i++) {
42+
weight[i][i] = 0;
43+
int val = getVal(graph, i);
44+
int x = getX(graph, i);
45+
int y = getY(graph, i);
46+
if (x == 0 || x == height - 1) {
47+
list.add(i);
48+
}
49+
if (y == 0 || y == width - 1) {
50+
list.add(i);
51+
}
52+
if (y < (width - 1) && i + 1 < total && getVal(graph, i + 1) <= val) {
53+
weight[i][i + 1] = 1;
54+
}
55+
if (y != 0 && i - 1 >= 0 && getVal(graph, i - 1) <= val) {
56+
weight[i][i - 1] = 1;
57+
}
58+
if (i + width < total && getVal(graph, i + width) <= val) {
59+
weight[i][i + width] = 1;
60+
}
61+
if (i - width >= 0 && getVal(graph, i - width) <= val) {
62+
weight[i][i - width] = 1;
63+
}
64+
}
65+
66+
int start = startX * width + startY;
67+
int resultIndex = -1;
68+
int resultLen = 256;
69+
for (int i = 0; i < list.size(); i++) {
70+
if (getVal(graph, list.get(i)) <= start) {
71+
int len = resolve(start, list.get(i));
72+
if (len < resultLen) {
73+
resultLen = len;
74+
resultIndex = list.get(i);
75+
}
76+
}
77+
}
78+
if (resultLen >= 256) {
79+
System.out.print("-1 -1 -1");
80+
return;
81+
}
82+
System.out.print(resultIndex / graph.length + " " + resultIndex % graph.length + " " + resultLen);
83+
}
84+
85+
public static int resolve(int start, int end) {
86+
87+
if (start < 0 || end < 0 || start >= weight.length || end >= weight.length) {
88+
return MAX;
89+
}
90+
91+
boolean[] isVisited = new boolean[weight.length];
92+
int[] d = new int[weight.length];
93+
94+
for (int i = 0; i < weight.length; i++) {
95+
isVisited[i] = false;
96+
d[i] = MAX;
97+
}
98+
d[start] = 0;
99+
isVisited[start] = true;
100+
int unVisitedNum = weight.length;
101+
int index = start;
102+
103+
while (unVisitedNum > 0 && index != end) {
104+
int min = MAX;
105+
for (int i = 0; i < weight.length; i++) {
106+
if (min > d[i] && !isVisited[i]) {
107+
min = d[i];
108+
index = i;
109+
}
110+
}
111+
for (int i = 0; i < weight.length; i++) {
112+
if (d[index] + weight[index][i] < d[i]) {
113+
d[i] = d[index] + weight[index][i];
114+
}
115+
}
116+
unVisitedNum--;
117+
isVisited[index] = true;
118+
}
119+
120+
return d[end];
121+
}
122+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /