题
1.迷宫 - 蓝桥云课
2.外卖店优先级 - 蓝桥云课
3.后缀表达式 - 蓝桥云课
题
1.迷宫 - 蓝桥云课
import java.util.*;
public class Main {
static class Node {
int x;
int y;
String str;
public Node(int x, int y, String str) {
this.x = x;
this.y = y;
this.str = str;
}
}
static char[][] chs = new char[30][50];
static boolean[][] vis = new boolean[30][50];
static void bfs() {
Queue<Node> q = new LinkedList<>();
int[] dx = {1, 0, 0, -1};
int[] dy = {0, -1, 1, 0};
char[] dir = {'D', 'L', 'R', 'U'};
q.offer(new Node(0,0,""));
vis[0][0] = true;
String res = "";
while(!q.isEmpty()) {
Node t = q.poll();
int x = t.x;
int y = t.y;
String str = t.str;
if(x==29 && y== 49) {
res = str;
break;
}
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
String nStr = str+dir[i];
if(nx>=0 && nx<30 && ny>=0 && ny<50 && !vis[nx][ny] && chs[nx][ny] =='0') {
q.offer(new Node(nx, ny, nStr));
vis[nx][ny] = true;
}
}
}
System.out.print(res);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
for(int i = 0; i < 30; i++)
chs[i] = scan.nextLine().toCharArray();
bfs();
scan.close();
}
}
走迷宫一看就是前几天还没记下来的bfs。。。这里主要是要记录路径。 那就在每个节点上加上从起点到这个点的路径一路记下来,这样就不需要前驱数组记录节点然后最后反向构建路径了。
2.外卖店优先级 - 蓝桥云课
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int t = scan.nextInt();
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for(int i = 0; i < m; i++) {
int ts = scan.nextInt();
int id = scan.nextInt();
if(map.containsKey(ts)) {
map.get(ts).add(id);
}
else {
map.put(ts, new ArrayList<>());
map.get(ts).add(id);
}
}
boolean[] inCache = new boolean[n+1];
int[] pri = new int[n+1];
// 按每一个时刻来遍历
for(int ts = 1; ts <= t; ts++) {
HashSet<Integer> processed = new HashSet<>();
if(map.containsKey(ts)) {
for(int store : map.get(ts)) {
pri[store] += 2;
processed.add(store);
}
}
for(int store = 1; store <= n; store++) {
if(!processed.contains(store)) {
pri[store] = pri[store]-1 >= 0 ? pri[store]-1 : 0;
}
if(pri[store] > 5)
inCache[store] = true;
else if(pri[store] <= 3)
inCache[store] = false;
}
}
int res = 0;
for(boolean b : inCache)
res = b ? res+1 : res;
System.out.print(res);
scan.close();
}
}
这个题目吧,,就是模拟,考验对代码和数据结构的掌握熟练度。
用一个HashMap来存储每个时间点有订单的外卖店。用一个布尔数组,每个时间点遍历所有的外卖店,如果满足要求就标记为true,反则反之。我那60%错在没有正确处理每个时间点没有订单的外卖点和记录结果的数组。怎么样实现这个呢?就是用一个set集合,有订单处理过的就加进去相当于做了个标记,妙哉妙哉。。。
3.后缀表达式 - 蓝桥云课
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int t = n+m+1;
int[] nums = new int[t];
for(int i = 0; i < t; i++) {
nums[i] = scan.nextInt();
}
if(m == 0) {
int res = 0;
for(int num : nums)
res += num;
System.out.print(res);
return;
}
Arrays.sort(nums);
int res = 0;
res += nums[t-1];
res -= nums[0];
for(int i = 1; i < t-1; i++) {
res += Math.abs(nums[i]);
}
System.out.print(res);
scan.close();
}
}
这个题目很有意思的点就是后缀表达式可以通过括号隐含该改变运算顺序。那么这样就可以将多个减号转换为加法。
- 最大数一定用加号
如果最大数是正数那肯定毋庸置疑用加号嘛,可以最大程度的使结果最大
如果最大数是负数,也用加号,因为这个时候这个最大数的绝对值是最小的,用加号就相当于减去一个最小的数字。 - 最小数一定用减号
如果最小数是正数,那没话说。
如果最小数是负数,用减号。就加上了一个很大的绝对值 - 中间数字都取绝对值相加