错误原因:没有人能够开一个二维的N = 1e5 + 10的数组,会爆内存!!!会发生超时
对于大数据的dfs和bfs应该使用动态数组
动态数组你不往里面放东西,里面是没有数的!!!
并且还要注意的一点是st[x].set(y, true);
在java中 想要修改!!!动态数组中的值
List<Integer> list = new ArrayList<>();
list.add(1);
System.out.println(list.get(0));
list.get(0) = 2;
需要使用set操作
CODE
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
public class Main{
static class Pair{
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static int N = 10010;
static List<Character> g [];
static Queue<Pair> q = new LinkedList<Pair>();
static int [] dx = {-1, 0, 0, 1};
static int [] dy = {0, 1, -1, 0};
static int n, m;
static List<Boolean> st [];
static int res = 0;
static int ans = 0;
static boolean bfs(int x, int y){
q.clear();
q.add(new Pair(x, y));
st[x].set(y, true);
boolean flag = false;
while(q.isEmpty() == false)
{
Pair p = q.poll();
if(g[p.x].get(p.y) >= '2' && g[p.x].get(p.y) <= '9' ) flag = true;
for(int i = 0; i < 4; i ++)
{
int a = p.x + dx[i];
int b = p.y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m || g[a].get(b) == '0' || st[a].get(b) == true) continue;
st[a].set(b,true);
q.offer(new Pair(a, b));
}
}
return flag;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
g = new ArrayList[n];
st = new ArrayList[n];
for(int i = 0; i < n; i ++){
g[i] = new ArrayList<>();
st[i] = new ArrayList<>();
String string = sc.next();
for(int j = 0; j < m; j ++){
g[i].add(string.charAt(j));
st[i].add(false);
}
}
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if(g[i].get(j) != '0' && st[i].get(j) == false)
{
if(bfs(i, j) == true) res ++; //宝藏数量
ans ++; //岛屿数量
}
}
}
System.out.println(ans + " " + res);
}
}
dfs
CODE
import java.util.*;
import java.io.*;
public class Main{
static class Pair{
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static List<Character> [] g;
static List<Boolean> [] st;
static int n, m;
static int [] dx = new int [] {-1, 0, 0, 1};
static int [] dy = new int [] {0, 1, -1, 0};
static boolean dfs(int x, int y){
boolean flag = false;
st[x].set(y, true);
if(g[x].get(y) >= '2' && g[x].get(y) <= '9') flag = true;
for(int i = 0; i < 4; i ++)
{
int a = x + dx[i];
int b = y + dy[i];
if(a >= n || a < 0 || b >= m || b < 0 || g[a].get(b) == '0' || st[a].get(b) == true) continue;
st[a].set(b, true);
if(dfs(a, b) == true) flag = true;
}
return flag;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
g = new ArrayList[n];
st = new ArrayList[n];
for(int i = 0; i < n; i ++){
g[i] = new ArrayList<>();
st[i] = new ArrayList<>();
String string = sc.next();
for(int j = 0; j < m; j ++) {
g[i].add(string.charAt(j));
st[i].add(false);
}
}
int res = 0;
int ans = 0;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
if(g[i].get(j) != '0' && st[i].get(j) == false)
{
if(dfs(i,j) == true) res ++;
ans ++;
}
}
}
System.out.println(ans + " " + res);
}
}