灌溉
原题链接
问题描述
解题思路
BFS,把每一个水管都添加到队列中,在某一个时刻,遍历队列中所有的点,看它的四个方向是否被灌溉过,没有的话就添加到队列中。当时间为 0 时退出循环。
参考代码
import java.util.*;
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 = 110, n, m, t, ans;
static int[][] a = new int[N][N];
static boolean[][] vis = new boolean[N][N];
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static Deque<int[]> dq = new ArrayDeque<>();
public static void bfs() {
while (!dq.isEmpty()) {
if (t == 0) break;
int sz = dq.size();
for (int i = 0; i < sz; i++) {
int[] tt = dq.pollFirst();
int x = tt[0], y = tt[1];
for (int j = 0; j < 4; j++) {
int nx = x + dx[j], ny = y + dy[j];
if (nx <= 0 || nx > n || ny <= 0 || ny > m || vis[nx][ny]) continue;
vis[nx][ny] = true;
dq.offerLast(new int[] {nx, ny});
ans++;
}
}
t--;
}
}
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
t = Integer.parseInt(in.readLine());
for (int i = 0; i < t; i++) {
s = in.readLine().split(" ");
int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]);
if (!vis[x][y]) {
dq.offerLast(new int[] {x, y});
vis[x][y] = true;
ans++;
}
}
t = Integer.parseInt(in.readLine());
bfs();
out.println(ans);
out.flush();
in.close();
}
}
小朋友崇拜圈
原题链接
问题描述
解题思路
按题意模拟就行,判断当前的小学生的一个崇拜关系链,如果长度超过数组长度,说明有环,直接退出循环即可,如果在循环的过程中回到了原点,说明可以构成,维护最大的一个长度即可。
参考代码
import java.util.*;
import java.math.*;
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 = (int)1e5 + 10, n;
static int[] a = new int[N], cnt = new int[N];
public static void main(String[] args) throws Exception {
n = Integer.parseInt(in.readLine());
String[] s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int t = i, cur = a[i], cnt = 1;
while (cur != t) {
cnt++;
if (cnt > n) {
cnt = 0;
break;
}
cur = a[cur];
}
ans = Math.max(ans, cnt);
}
out.println(ans);
out.flush();
in.close();
}
}
括号序列
原题链接
问题描述
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码
砍竹子
原题链接
问题描述
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码