baidu 格子距离
import java.util.Scanner;
public class Main {
public static String input = "2\n" +
"3 3 1 1\n" +
"8 0 0\n" +
"2 0 0\n" +
"1 4 0\n" +
"2 2 1 1\n" +
"0 0\n" +
"0 0\n";
public static int[][] rst;
public static int[][] array;
public static int m; // m行
public static int n; // n列
public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
Scanner in = new Scanner(input);
int caseNum = in.nextInt();
for (int caseIndex = 0; caseIndex < caseNum; caseIndex++) {
m = in.nextInt(); // m行
n = in.nextInt(); // n列
int startX = in.nextInt();
int startY = in.nextInt();
array = new int[m][n];
rst = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array[i][j] = in.nextInt();
rst[i][j] = -1;
}
}
search(startX - 1, startY - 1, 0);
print(caseIndex);
}
}
/**
* 此处应该是广度优先遍历
*/
private static void search(int x, int y, int stepNum) {
if (x < 0 || x >= m || y < 0 || y >= n) {
return;
}
// 这个不好
if (rst[x][y] != -1 && stepNum >= rst[x][y]) {
return;
}
rst[x][y] = stepNum;
int wall = array[x][y];
if (wall % 2 != 1) {
search(x - 1, y, stepNum + 1);
}
if ((wall >> 1) % 2 != 1) {
search(x + 1, y, stepNum + 1);
}
if ((wall >> 2) % 2 != 1) {
search(x, y - 1, stepNum + 1);
}
if ((wall >> 3) % 2 != 1) {
search(x, y + 1, stepNum + 1);
}
}
private static void print(int caseIndex) {
int caseN = caseIndex + 1;
System.out.println("Case " + caseN);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(rst[i][j]);
if (j != n - 1) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
}
}
baidu 找子串
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
System.out.println(find(line));
}
private static int find(String str) {
Set<String> set = new HashSet<>();
String rst = "";
char[] chars = str.toCharArray();
for (char aChar : chars) {
if (rst.length() == 0 || rst.contains(aChar + "")) {
rst += aChar;
} else {
rst = aChar + "";
}
set.add(rst);
}
return set.size();
}
}