第一题:灌溉
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N = 110;
static boolean[][] st = new boolean[N][N];
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static Queue<int[]> q = new LinkedList<>();
public static void main(String[] args) throws Exception{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int t = s.nextInt();
while(t-- > 0) {
int x = s.nextInt();
int y = s.nextInt();
q.add(new int[] {x, y});
st[x][y] = true;
}
int k = s.nextInt();
int cnt = 0;
while(!q.isEmpty() && k-- >= 0) {
int len = q.size();
cnt += len;
while(len-- > 0) {
int[] d = q.poll();
int x = d[0], y = d[1];
for(int i = 0; i < 4; i++) {
int tX = x + dx[i], tY = y + dy[i];
if(tX >= 1 && tX <= n && tY >= 1 && tY <= m && !st[tX][tY]) {
st[tX][tY] = true;
q.add(new int[] {tX, tY});
}
}
}
}
System.out.println(cnt);
}
}
第二题:小朋友崇拜圈
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static int N = (int)1e5 + 10;
static int[] a = new int[N];
static boolean[] st = new boolean[N];
static int res, n, t;
static void dfs(int x, int cnt) {
if(st[x]) {
if(a[x] == a[t]) {
res = Math.max(res, cnt);
}
return;
}
st[x] = true;
dfs(a[x], cnt+1);
st[x] = false;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
for(int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i-1]);
}
for(int i = 1; i <= n; i++) {
t = i;
dfs(i, 0);
}
System.out.println(res);
}
}
第三题:括号序列
第四题:砍竹子