1.22
P1123 取数游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
-
本题一开始是想写双for循环,复杂度\(O(n^n)\),不出意外T了三个点,然后思考剪枝,考虑到不需要重新找,做了一个bool数组来标记,防止重新搜索已经出现过的方案,过了第四个点。
-
后面看了题解后,发现这一思路极好,枚举整个棋盘,每个点可以选也可以不选,则复杂度直接降为\(O(2^n)\),直接ac,根本不用剪枝
package shuati;
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static Scanner scanner = new Scanner(System.in);
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static int t, n, m;
static final int N = 10;
static int[][] g = new int[N][N];
// cnt[i][j] 表示以(i,j)为中心的周围八个格子有几个数被选取
static int[][] cnt = new int[N][N];
static int ans, nowans;
static int dx[] = {0, 1, 1, 0, -1, -1, -1, 0, 1};
static int dy[] = {0, 0, 1, 1, 1, 0, -1, -1, -1};
public static void dfs(int x, int y) {
// 处理越界
if(y == m + 1) {
dfs(x + 1, 1);
return;
}
if(x == n + 1) {
ans = Math.max(nowans, ans);
return;
}
// 不选择这个点
dfs(x,y + 1);
if(cnt[x][y] == 0) {
for(int i = 0; i < 9; i++) {
cnt[x + dx[i]][y + dy[i]]++;
}
nowans += g[x][y];
dfs(x, y + 1);
nowans -= g[x][y];
for(int i = 0; i < 9; i++) {
cnt[x + dx[i]][y + dy[i]]--;
}
}
}
public static void solve() throws IOException {
for (int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin.nextToken();
g[i][j] = (int) cin.nval;
}
}
dfs(1, 1);
cout.println(ans);
cout.flush();
}
public static void main(String[] args) throws IOException{
cin.nextToken();
t = (int) cin.nval;
while(t-- != 0) {
cin.nextToken();
n = (int) cin.nval;
cin.nextToken();
m = (int) cin.nval;
ans = 0;
nowans = 0;
solve();
}
}
}
深さ優先探索 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 经典搜索
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static Scanner scanner = new Scanner(System.in);
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
static final int N = 510;
static int n, m;//长和宽
static char[][] g = new char[N][N];
static boolean[][] str = new boolean[N][N];
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static int cnt;
public static void dfs(int x, int y) {
if(g[x][y] == 'g') {
cnt = 1;
cout.print("Yes");
return;
}
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 <= m) {
if(str[nowx][nowy] == false && g[nowx][nowy] != '#' && cnt == 0) {
str[nowx][nowy] = true;
dfs(nowx, nowy);
}
}
}
}
public static void main(String[] args) throws IOException{
// cin.nextToken();
// n = (int) cin.nval;
// cin.nextToken();
// m = (int) cin.nval;
n = scanner.nextInt();
m = scanner.nextInt();
int chux = 0;
int chuy = 0;
for(int i = 1; i <= n; i++) {
String s = scanner.next();
for(int j = 1; j <= m; j++) {
g[i][j] = s.charAt(j - 1);
if(s.charAt(j - 1) == 's') {
chux = i;
chuy = j;
}
}
}
str[chux][chuy] = true;
dfs(chux, chuy);
if(cnt == 0) {
cout.print("No");
}
cout.flush();
}
}
P1706 全排列问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 水题
import java.io.*;
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 n;
static boolean[] str = new boolean[10];
static Queue<Integer> myQueue = new LinkedList<>();
public static void dfs(int cnt) {
if(cnt == n) {
for (Integer i : myQueue) {
cout.print(" " + i);
}
cout.println();
cout.flush();
return;
}
for(int i = 1; i <= n; i++) {
if(str[i] == false) {
str[i] = true;
cnt++;
myQueue.add(i);
dfs(cnt);
myQueue.remove(i);
cnt--;
str[i] = false;
}
}
}
public static void main(String[] args) throws IOException{
cin.nextToken();
n = (int)cin.nval;
dfs(0);
}
}
浙公网安备 33010602011771号