题目描述
假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径。迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路。迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与之匹配的钥匙,只有先拿到钥匙才能打开门。请设计一个算法,帮助探险家找到脱困的最短路径。如前所述,迷宫是通过一个二维矩阵表示的,每个元素的值的含义如下 0-墙,1-路,2-探险家的起始位置,3-迷宫的出口,大写字母-门,小写字母-对应大写字母所代表的门的钥匙
输入描述:
迷宫的地图,用二维矩阵表示。第一行是表示矩阵的行数和列数M和N
后面的M行是矩阵的数据,每一行对应与矩阵的一行(中间没有空格)。M和N都不超过100, 门不超过10扇。
输出描述:
路径的长度,是一个整数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int[][] next={{-1,0},{0,-1},{1,0},{0,1}};
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String[] mn = String.valueOf(bufferedReader.readLine()).split(" ");
int m = Integer.parseInt(mn[0]);
int n = Integer.parseInt(mn[1]);
String[] tempArray = new String[m];
for(Integer i = 0; i < m; i++) {
tempArray[i] = bufferedReader.readLine();
}
char[][] mnArray = new char[m][n];
Integer startX = 0;
Integer startY = 0;
for(int i = 0; i < m; i++) {
if(tempArray[i].contains("2")) {
startX = i;
startY = tempArray[i].indexOf("2");
}
mnArray[i] = tempArray[i].toCharArray();
}
Integer result = step(startX, startY, m, n, mnArray);
System.out.println(result);
}
public static Integer step(Integer x, Integer y, Integer m, Integer n, char[][] mnArray) {
Queue<StepNode> stepQueue = new LinkedList<>();
stepQueue.offer(new StepNode(x,y,0,0));
byte[][][] arrived = new byte[m][n][1024];
while(!stepQueue.isEmpty()) {
StepNode stepNode = stepQueue.poll();
for(int i = 0; i < 4; i++) {
int[] nextNode = next[i];
int nextX = nextNode[0];
int nextY = nextNode[1];
int stepX = stepNode.x + nextX;
int stepY = stepNode.y + nextY;
int step = stepNode.step;
int key = stepNode.key;
if(stepX < 0 || stepY < 0 || stepX > m - 1 || stepY > n - 1|| mnArray[stepX][stepY] == '0') {
continue;
}
if(mnArray[stepX][stepY] >= 'a' && mnArray[stepX][stepY] <= 'z') {
int index = mnArray[stepX][stepY] - 'a';
key |= 1 << index;
}
if(mnArray[stepX][stepY] >= 'A' && mnArray[stepX][stepY] <= 'Z') {
int index = mnArray[stepX][stepY] - 'A';
if(((key >> index) & 1) == 0) {
continue;
}
}
if(mnArray[stepX][stepY] == '3') {
return step + 1;
}
if(arrived[stepX][stepY][key] == 0) {
arrived[stepX][stepY][key] = 1;
stepQueue.offer(new StepNode(stepX, stepY, step + 1, key));
}
}
}
return -1;
}
class StepNode {
int x;
int y;
int step;
int key;
public StepNode(int x, int y, int step, int key) {
this.x = x;
this.y = y;
this.step = step;
this.key = key;
}
}
}
题目描述
六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i] (即w[j]>=h[i]),他才会上去表演节目。老师的目标是将巧克力分发给孩子们,使得最多的小孩上台表演。可以保证每个w[i]> 0且不能将多块巧克力分给一个孩子或将一块分给多个孩子。
输入描述:
第一行:n,表示h数组元素个数
第二行:n个h数组元素
第三行:m,表示w数组元素个数
第四行:m个w数组元素
输出描述:
上台表演学生人数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
Integer result = 0;
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
Integer n = Integer.valueOf(bufferedReader.readLine());
String[] h = String.valueOf(bufferedReader.readLine()).split(" ");
Integer[] hArray = new Integer[n];
for (int i = 0; i < h.length; i++) {
hArray[i] = Integer.valueOf(h[i]);
}
Integer m = Integer.valueOf(bufferedReader.readLine());
String[] w = String.valueOf(bufferedReader.readLine()).split(" ");
Integer[] wArray = new Integer[m];
for (int i = 0; i < w.length; i++) {
wArray[i] = Integer.valueOf(w[i]);
}
Arrays.sort(hArray);
Arrays.sort(wArray);
Integer now = 0;
end:
for (Integer chw : wArray) {
for (int index = now; index < n; index++) {
if (hArray[index] <= chw) {
result++;
now = index + 1;
break;
}
if (index == n) {
break end;
}
}
}
System.out.println(result);
}
}
题目描述
给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)
输入描述:
无序整数数组A[n]
输出描述:
满足条件的最大乘积
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int number = Integer.valueOf(bufferedReader.readLine());
String[] str = bufferedReader.readLine().split(" ");
int[] array = new int[str.length];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(str[i]);
}
if (array.length == 3) {
System.out.println(array[0] * array[1] * array[2]);
return;
}
ArrayList<Integer> plus = new ArrayList(array.length);
ArrayList<Integer> minus = new ArrayList(array.length);
for (Integer member : array) {
if (member == 0) {
continue;
}
if (member < 0) {
int index = 0;
for (; index < minus.size(); index++) {
if (Math.abs(minus.get(index)) < Math.abs(member)) {
Integer temp = minus.get(index);
minus.set(index, member);
member = temp;
}
}
minus.add(member);
} else {
int index = 0;
for (; index < plus.size(); index++) {
if (Math.abs(plus.get(index)) < Math.abs(member)) {
Integer temp = plus.get(index);
plus.set(index, member);
member = temp;
}
}
plus.add(member);
}
}
Long result = null;
if (plus.size() >= 3) {
result = Long.valueOf(plus.get(0)) * plus.get(1) * plus.get(2);
}
if (plus.size() >= 1 && minus.size() >= 2) {
if (result == null) {
result = Long.valueOf(minus.get(0)) * minus.get(1) * plus.get(0);
} else {
result = Long.valueOf(minus.get(0)) * minus.get(1) * plus.get(0) > result ? Long.valueOf(minus.get(0)) * minus.get(1) * plus.get(0) : result;
}
}
if (result == null) {
result = 0L;
}
System.out.println(result);
return;
}
}