1.30
1.30
Maze - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 从一个空格走
cnt - k个点并标记,然后将没有标记的点设为A即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class Main implements Runnable {
static Scanner cin = new Scanner(System.in);
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int n, m, k, t;
static final int N = (int) (5e2 + 10);
static int[][] g = new int[N][N];
static boolean[][] str = new boolean[N][N];
static int[] dx = {1, 0, -1, 0}; static int[] dy = {0, 1, 0, -1};
public static void main(String[] args) {
new Thread(null, new Main(), "", 1 << 29).start();
}
public static void dfs(int x, int y) {
if(k == 0) return;
for(int i = 0; i < 4; i++) {
int nowx = x + dx[i];
int nowy = y + dy[i];
if(nowx < 1 || nowx > n || nowy < 1 || nowy > m) continue;
if(str[nowx][nowy] || g[nowx][nowy] == 1 || k == 0) continue;
str[nowx][nowy] = true; k--;
dfs(nowx, nowy);
}
}
@Override
public void run() {
n = cin.nextInt();m = cin.nextInt();k = cin.nextInt();
int cnt = 0;
for (int i = 1; i <= n; i++) {
String s = cin.next();
for (int j = 1; j <= m; j++) {
if (s.charAt(j - 1) == '#') {
g[i][j] = 1;
} else {
cnt++;
g[i][j] = 0;
}
}
}
k = cnt - k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!str[i][j] && g[i][j] == 0 && k != 0) {
k--;str[i][j] = true;dfs(i, j);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!str[i][j] && g[i][j] == 0) {
cout.print('X');continue;
}
if (g[i][j] == 0) cout.print('.');
else cout.print('#');
}
cout.println();
}
cout.flush();
}
}
New Reform - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 本题可以用并查集来做,但是因为是练习搜索,姑且还是先用搜索来做
- 1 考虑图为一个连通块,图的形式其实就只有三种形态,可以看到分别是不是环,一个环,多个环。可以看到,只有不是环的时候,才会出现一个入度为0的点,也就是根节点。

- 2 考虑图为多个连通块,那么会发现答案为2,就是无环的两个根节点

- 那么ok,答案已经出来了,就是遍历所有结点,对没有被标记过的结点进行递归,寻找这个连通块是否有环即可,注意是单向联通
package shuati;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class Main implements Runnable {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int n, m;
static final int N = (int) (1e5 + 10);
static Vector<Integer>[] city = new Vector[N];
static boolean[] str = new boolean[N];
static boolean isCircle;
public static void main(String[] args) {
new Thread(null, new Main(), "", 1 << 29).start();
}
public static int nextInt() throws IOException {
cin.nextToken();
return (int) cin.nval;
}
public static void dfs(int son, int parent) {
for (Integer now : city[son]) {
if (now == parent) continue;
if (str[now]) {
isCircle = true;
continue;
}
str[now] = true;
dfs(now, son);
}
}
@Override
public void run() {
try {
for(int i = 0; i < N; i++) {city[i] = new Vector<>();}
n = nextInt();m = nextInt();
for(int i = 1; i <= m; i++) {int x = nextInt();int y = nextInt();city[x].add(y);city[y].add(x);}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(!str[i]) {
str[i] = true;
isCircle = false;
dfs(i, -1);
if(!isCircle) ans++;
}
}
cout.println(ans);cout.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Ecological Bin Packing - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 数据比较简单,大模拟即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class Main implements Runnable {
static Scanner scanner = new Scanner(System.in);
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int[][] g = new int[3][3];
static String[] s = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
static int[] ans = new int[6];
public static void main(String[] args) {
new Thread(null, new Main(), "", 1 << 29).start();
}
@Override
public void run() {
while (scanner.hasNext()) {
g[0][0] = scanner.nextInt();g[0][1] = scanner.nextInt();g[0][2] = scanner.nextInt();//bgc
g[1][0] = scanner.nextInt();g[1][1] = scanner.nextInt();g[1][2] = scanner.nextInt();
g[2][0] = scanner.nextInt();g[2][1] = scanner.nextInt();g[2][2] = scanner.nextInt();
ans[0] = g[1][0] + g[2][0] + g[0][2] + g[2][2] + g[0][1] + g[1][1];//bcg
ans[1] = g[1][0] + g[2][0] + g[0][1] + g[2][1] + g[0][2] + g[1][2];//bgc
ans[2] = g[1][2] + g[2][2] + g[0][0] + g[2][0] + g[0][1] + g[1][1];//cbg
ans[3] = g[1][2] + g[2][2] + g[0][1] + g[2][1] + g[0][0] + g[1][0];//cgb
ans[4] = g[1][1] + g[2][1] + g[0][0] + g[2][0] + g[0][2] + g[1][2];//gbc
ans[5] = g[1][1] + g[2][1] + g[0][2] + g[2][2] + g[0][0] + g[1][0];//gcb
int k = ans[0];int t = 0;
for(int i = 1; i <= 5; i++) {
if(k > ans[i]) {
k = ans[i];t = i;
}
}
cout.println(s[t] + " " + k);
cout.flush();
}
}
}
浙公网安备 33010602011771号