1
P1162 填涂颜色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 只需要环最外圈的0,然后标记,最后填色时没有标记的标为2即可
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
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 {
static final int N = 35;
static int[][] g = new int[N][N];
static boolean[][] str = new boolean[N][N];
static int n;
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static Queue<Integer> queue = new LinkedList<>();
public static void dfs(int x, int y) {
for(int i = 0; i < 4; i++) {
int nowx = dx[i] + x;
int nowy = dy[i] + y;
if(nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= n) {
if(g[nowx][nowy] == 0 && str[nowx][nowy] == false) {
str[nowx][nowy] = true;
dfs(nowx, nowy);
}
}
}
}
public static void bfs(int x, int y) {
}
public static void main(String[] args) throws IOException{
cin.nextToken();
n = (int)cin.nval;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cin.nextToken();
g[i][j] = (int)cin.nval;
}
}
for(int i = 1; i <= n; i++) {
if(g[1][i] == 0 && str[1][i] == false) {
str[1][i] = true;
dfs(1, i);
}
}
for(int i = 1; i <= n; i++) {
if(g[n][i] == 0 && str[n][i] == false) {
str[n][i] = true;
dfs(n, i);
}
}
for(int i = 2; i <= n - 1; i++) {
if(g[i][1] == 0 && str[i][1] == false) {
str[i][1] = true;
dfs(i, 1);
}
if(g[i][n] == 0 && str[i][n] == false) {
str[i][n] = true;
dfs(i, n);
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(g[i][j] == 0 && str[i][j] == false) {
g[i][j] = 2;
}
System.out.print(g[i][j] + " ");
}
System.out.println();
}
}
}
[P1460 USACO2.1] 健康的荷斯坦奶牛 Healthy Holsteins - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 注意几个剪枝的点,第一个是大于或等于最短的成功路径不必继续搜,主循环搜完的点不能再搜,因为我们是从1开始的,所以每一次我们都默认往后搜而不往前搜,往前搜必然是与之前的成功路径重复了,再剪一次就能够ac了。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
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 {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int v, g;//4 3
static final int N = (int)1e3;
static int[] wtmin_target = new int[N];
static int[] wtmin_now = new int[N];
static int[][] siliao = new int[N][N];
static boolean[] str = new boolean[N];
static PriorityQueue<Integer>[] targetQueue = new PriorityQueue[N];
static PriorityQueue<Integer> nowQueue = new PriorityQueue();
static List<Integer> lists = new ArrayList<>();
static int cnt = 0x3f3f;
static int activeMent;
public static void dfs(int x, int cnt_) {
if(cnt <= cnt_) {
return;
}
int num = 0;
for(int i = 1; i <= v; i++) {
if(wtmin_now[i] >= wtmin_target[i]) {
num++;
}
if(num == v) {
for (Integer integer : nowQueue) {
targetQueue[activeMent].add(integer);
}
activeMent++;
cnt = Math.min(cnt, cnt_);
return;
}
}
for(int i = x + 1; i <= g; i++) {
if(str[i] == false) {
str[i] = true;
for(int j = 1; j <= v; j++) {
wtmin_now[j] += siliao[i][j];
}
nowQueue.add(i);
dfs(i, cnt_ + 1);
nowQueue.remove(i);
for(int j = 1; j <= v; j++) {
wtmin_now[j] -= siliao[i][j];
}
str[i] = false;
}
}
}
public static void bfs(int x, int y) {
}
public static void main(String[] args) throws IOException {
for (int i = 0; i < N; i++) {
targetQueue[i] = new PriorityQueue<>();
}
cin.nextToken();
v = (int)cin.nval;
for (int i = 1; i <= v; i++) {
cin.nextToken();
wtmin_target[i] = (int) cin.nval;
}
cin.nextToken();
g = (int) cin.nval;
for (int i = 1; i <= g; i++) {
for (int j = 1; j <= v; j++) {
cin.nextToken();
siliao[i][j] = (int) cin.nval;
}
}
for(int i = 1; i <= g; i++) {
str[i] = true;
for(int j = 1; j <= v; j++) {
wtmin_now[j] += siliao[i][j];
}
nowQueue.add(i);
dfs(i, 1);
nowQueue.remove(i);
for(int j = 1; j <= v; j++) {
wtmin_now[j] -= siliao[i][j];
}
//str[i] = false;
}
int xiabiao = 0;
for(int i = 0; i < activeMent; i++) {
if (targetQueue[i].size() == cnt) {
xiabiao = i;
break;
}
}
System.out.print(cnt + " ");
while(targetQueue[xiabiao].size() != 0) {
System.out.print(targetQueue[xiabiao].poll() + " ");
}
}
}
[P1030 NOIP2001 普及组] 求先序排列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 每一次递归输出后序遍历最后一个节点,然后将中序分为两个子串,然后按照后续顺序,分为两个后序子串,因为个数肯定一样,可以使用java的substr,写者有点rz的没有想到二者两个字母个数是完全一样的,调了一个小时。然后分别递归dfs即可。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
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 {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static String zhongxu = new String();
static String houxu = new String();
// s中序 c后序
public static void dfs(String s, String c) {
if(s.length() == 1) {
System.out.print(c.charAt(c.length() - 1));
return;
}
System.out.print(c.charAt(c.length() - 1));
String[] t = s.split(Character.toString(c.charAt(c.length() - 1))); //t0 t1 中序
String houxu1 = new String();
String houxu2 = new String();
for(int i = 0; i < c.length() - 1; i++) {
if(t[0].contains(Character.toString(c.charAt(i)))) {
houxu1 += Character.toString(c.charAt(i));
} else {
houxu2 += Character.toString(c.charAt(i));
}
}
if(t.length == 2 && t[0].length() != 0 && t[1].length() != 0) {
dfs(t[0], houxu1);
dfs(t[1], houxu2);
} else {
if(houxu1.length() != 0) {
dfs(t[0], houxu1);
} else {
dfs(t[1], houxu2);
}
}
}
public static void main(String[] args) throws Exception{
cin.nextToken();
zhongxu = cin.sval;
cin.nextToken();
houxu = cin.sval;
System.out.print(houxu.charAt(houxu.length() - 1));
String[] t = zhongxu.split(Character.toString(houxu.charAt(houxu.length() - 1)));
String houxu1 = new String();
String houxu2 = new String();
for(int i = 0; i < houxu.length() - 1; i++) {
if(t[0].contains(Character.toString(houxu.charAt(i)))) {
houxu1 += Character.toString(houxu.charAt(i));
} else {
houxu2 += Character.toString(houxu.charAt(i));
}
}
if(t.length == 2 && t[0].length() != 0 && t[1].length() != 0) {
dfs(t[0], houxu1);
dfs(t[1], houxu2);
} else {
if(houxu1.length() != 0) {
dfs(t[0], houxu1);
} else {
dfs(t[1], houxu2);
}
}
}
}
标签:java,int,static,io,import,new,1.21
From: https://www.cnblogs.com/Mikkeykarl/p/18683821