标签:tin main 个人 linked 上下 base public ted 左右移动
只第一题过了80%,3.24复盘
第一题:
n个人,选任意多人组成一个队(至少一个),再从中选一个队长,问多少种方案,模1e9;
过80%
思路:dp打表,找规律,n*(2^(n-1))
private static int quickmi(long a, long b, long m) {
long ans = 1;
long base = a;
while (b > 0) {
if(b % 2 == 1) ans = ans * base % m;
base = base * base % m;
b >>= 1;
}
return (int) ans;
}
//打表
private static void solve1(int n) {
int[][] dp = new int[n+1][n+1];
dp[0][0] = 1;
int res = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
dp[i][j] = i - 1 >= j ? dp[i-1][j] : 0;
dp[i][j] += dp[i-1][j-1];
dp[i][j] += j > 1 ? dp[i-1][j-1] / (j - 1) : 0;
res += dp[i][j];
}
System.out.println(res/n);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long mod = 1000000007;
long tmp = quickmi(2, n - 1, mod);
System.out.println(((n % mod) * (tmp % mod)) % mod);
第二题:
走迷宫,不能走到障碍物上,每次上下左右移动一格,或者用飞行器飞到中心对称点(最多用五次),最少多少次到达。
static int[][] dir = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
private static int bfs(char[][] matrix, int n, int m, int sx, int sy, int ex, int ey) {
boolean[][][] visited = new boolean[n][m][6];
Queue<int[]> que = new LinkedList<>();
que.offer(new int[]{sx, sy, 0});
int res = 0;
while(!que.isEmpty()) {
for(int size = que.size(); size > 0; size--) {
int[] pos = que.poll();
if(pos[0] == ex && pos[1] == ey) return res;
visited[pos[0]][pos[1]][pos[2]] = true;
for(int[] d : dir) {
int x = pos[0] + d[0], y = pos[1] + d[1];
if(x >= 0 && x < n && y >= 0 && y < m && matrix[x][y] != ‘#‘ && !visited[x][y][pos[2]]) {
que.offer(new int[]{x, y, pos[2]});
}
}
if(pos[2] < 5) {
int dx = n - 1 - pos[0], dy = m - 1 - pos[1];
que.offer(new int[]{dx, dy, pos[2] + 1});
}
}
res++;
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.nextLine();
char[][] matrix = new char[n][m];
for(int i = 0; i < n; i++) matrix[i] = scanner.nextLine().toCharArray();
int sx = 0, sy = 0, ex = 0, ey = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(matrix[i][j] == ‘S‘) {
sx = i;
sy = j;
}
if(matrix[i][j] == ‘E‘) {
ex = i;
ey = j;
}
}
}
System.out.println(bfs(matrix, n, m, sx, sy, ex, ey));
}
标签:tin main 个人 linked 上下 base public ted 左右移动
原文地址:https://www.cnblogs.com/xlsryj/p/12558114.html