长草
原题链接
问题描述
解题思路
BFS 模板题
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// static StreamTokenizer st = new StreamTokenizer(in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static int N = 1010, n, m, k;
static char[][] g = new char[N][N];
static boolean[][] vis = new boolean[N][N];
static Deque<int[]> dq = new ArrayDeque<>();
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
for (int i = 0; i < n; i++) {
String row = in.readLine();
for (int j = 0; j < m; j++) {
g[i][j] = row.charAt(j);
if (g[i][j] == 'g') {
dq.offerLast(new int[] {i, j});
vis[i][j] = true;
}
}
}
k = Integer.parseInt(in.readLine());
while (!dq.isEmpty()) {
if (k == 0) break;
int sz = dq.size();
for (int i = 0; i < sz; i++) {
int[] t = dq.pollFirst();
int x = t[0], y = t[1];
for (int j = 0; j < 4; j++) {
int nx = x + dx[j], ny = y + dy[j];
if (nx < 0 || ny < 0 || nx >= n || ny >= m || vis[nx][ny]) continue;
vis[nx][ny] = true;
g[nx][ny] = 'g';
dq.offerLast(new int[] {nx, ny});
}
}
k--;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
out.print(g[i][j]);
}
out.println("");
}
out.flush();
in.close();
}
}
蓝肽子序列
原题链接
问题描述
解题思路
最长公共子序列模板题
参考代码
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = 1010, n, m;
static String[] ss = new String[N], tt = new String[N];
static int[][] f = new int[N][N];
/** 预处理 */
public static void init(String s, String[] ss, int type) {
int len = s.length(), st = -1, idx = 1;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (Character.isUpperCase(c)) {
if (st != -1) {
ss[idx++] = s.substring(st, i);
}
st = i;
}
}
if (st != n - 1) ss[idx++] = s.substring(st, len);
if (type == 0) n = idx;
if (type == 1) m = idx;
}
public static void main(String[] args) throws Exception {
init(in.readLine(), ss, 0);
init(in.readLine(), tt, 1);
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (ss[i].equals(tt[j])) {
f[i][j] = f[i - 1][j - 1] + 1;
} else {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
}
}
}
out.println(f[n - 1][m - 1]);
out.flush();
in.close();
}
}
迷宫与陷阱
原题链接
问题描述
todo:这里复制题目描述
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码
送礼物
原题链接
问题描述
todo:这里复制题目描述
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码