FBI树
[P1087 NOIP2004 普及组] FBI 树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 不用去建树,直接不断拆分递归,然后每次判断一下这个区间内有多少个1,0.
- 感觉类似二分
import java.util.Scanner;
public class Main {
static int n;
static String s;
public static void dfs(int up, int down) {
if(up != down) {
int mid = (up + down) / 2;
dfs(up, mid);
dfs(mid + 1, down);
}
int num_1 = 0;
int num_0 = 0;
for(int i = up; i <= down; i++) {
if(s.charAt(i) == '1') {
num_1++;
} else {
num_0++;
}
}
if(num_1 != 0 && num_0 != 0) {
System.out.print("F");
}else if(num_1 != 0){
System.out.print("I");
}else {
System.out.print("B");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
s = scanner.next();
s = " " + s;
dfs(1, s.length() - 1);
return;
}
}
#include<iostream>
#include<string>
using namespace std;
int n;
string s;
void dfs(int up, int down) {
if(up != down) {
int mid = (up + down) / 2;
dfs(up, mid);
dfs(mid + 1, down);
}
int num_1 = 0;
int num_0 = 0;
for(int i = up; i <= down; i++) {
if(s[i] == '1') {
num_1++;
} else {
num_0++;
}
}
if(num_1 && num_0) {
cout<<"F";
}else if(num_1){
cout<<"I";
}else {
cout<<"B";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>s;
s = " " + s;
dfs(1, s.size() - 1);
return 0;
}
01迷宫
P1141 01迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 本题一开始用的dfs来了一发,但是发现有几个点会爆TLE,考虑优化方案。
- 考虑到就是,如果我能从一个块走到另一个块,那么我必然可以走回去。
- 即其实就是一个染色连通块,只要已经染过色,就已经产生了答案,也就是不必再去搜索。
- 那之后就比较简单了,我们只需要把走过的点标记然后下次遇到走过的点不搜就是了
- 至于染色块答案存储,选用的时map
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int N = 1e3 + 10;
int g[N][N];
bool str[N][N];
int cnt[N][N];
int cnt_bankuai;
int n, m;
int ans;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
map<int,int>myMap;
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(!str[nowx][nowy] && g[nowx][nowy] == !g[x][y]) {
str[nowx][nowy] = true;
cnt[nowx][nowy] = cnt_bankuai;
myMap[cnt_bankuai]++;
ans++;
dfs(nowx, nowy);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++) {
string s;
cin>>s;
for(int j = 1;j <= n; j++) {
g[i][j] = s[j - 1] - '0';
}
}
while(m--) {
//memset(str, false, sizeof(str));
ans = 1;
int x, y;
cin>>x>>y;
if(!str[x][y]) {
cnt_bankuai++;
myMap[cnt_bankuai]++;
cnt[x][y] = cnt_bankuai;
str[x][y] = true;
dfs(x, y);
}else{
int num = cnt[x][y];
ans = myMap[num];
}
cout<<ans<<endl;
}
return 0;
}
package shuati;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
static final int N = 1010;
static int[][] g = new int[N][N];
static boolean[][] str = new boolean[N][N];
static int[][]cnt = new int[N][N];
static int dx[] = {1, 0, -1, 0};
static int dy[] = {0, 1, 0, -1};
static int n, m, ans, cnt_bankuai;
static Map<Integer, Integer>myMap = new HashMap<Integer, Integer>();
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(!str[nowx][nowy] && ((g[nowx][nowy] == g[x][y] + 1) ||( g[nowx][nowy] == g[x][y] - 1))) {
str[nowx][nowy] = true;
cnt[nowx][nowy] = cnt_bankuai;
myMap.put(cnt_bankuai, myMap.get(cnt_bankuai) + 1);
ans++;
dfs(nowx, nowy);
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
for(int i = 1; i <= n; i++) {
String s;
s = scanner.next();
for(int j = 1;j <= n; j++) {
g[i][j] = s.charAt(j - 1) - '0';
}
}
while(m-- != 0) {
ans = 1;
int x, y;
x = scanner.nextInt();
y = scanner.nextInt();
if(!str[x][y]) {
cnt_bankuai++;
myMap.put(cnt_bankuai, 1);
cnt[x][y] = cnt_bankuai;
str[x][y] = true;
dfs(x, y);
}else{
int num = cnt[x][y];
ans = myMap.get(num);
}
System.out.println(ans);
}
return;
}
}
- java的这个代码会爆栈溢出,不知道怎么调了
- 今晚还有场div3,✌