211203code
Updated:
// Combination
public class N12977 {
private static int answer;
public int solution(int[] nums) {
int n = nums.length;
boolean[] visited = new boolean[n];
combination(nums, visited, 0, n, 3);
return answer;
}
static void combination(int[] arr, boolean[] visited, int start, int n, int r) {
if (r == 0) {
answer += isPrime(arr, visited, n) ? 1 : 0;
return;
}
for (int i = start; i < n; i++) {
visited[i] = true;
combination(arr, visited, i + 1, n, r - 1);
visited[i] = false;
}
}
static boolean isPrime(int[] arr, boolean[] visited, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
if (visited[i]) {
sum += arr[i];
}
}
for (int i = 2; i < sum; i++) {
if (sum % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
N12977 n12977 = new N12977();
System.out.println(n12977.solution(new int[]{1, 2, 6, 7, 4}));
}
}
// DFS
import java.util.LinkedList;
import java.util.Queue;
public class N1844 {
public int DFS(int[][] maps, int N, int M) {
int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
boolean[][] visit = new boolean[N][M];
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{0, 0, 1});
visit[0][0] = true;
while (!queue.isEmpty()) {
int[] info = queue.poll();
int now_i = info[0];
int now_j = info[1];
int work = info[2];
if (now_i == N - 1 && now_j == M - 1) {
return work;
}
for (int[] dir : dirs) {
int visit_i = now_i + dir[0];
int visit_j = now_j + dir[1];
if (0 <= visit_i && visit_i < N && 0 <= visit_j && visit_j < M && !visit[visit_i][visit_j]) {
if (maps[visit_i][visit_j] == 1) {
visit[visit_i][visit_j] = true;
queue.add(new int[]{visit_i, visit_j, work + 1});
}
}
}
}
return -1;
}
public int solution(int[][] maps) {
final int N = maps.length;
final int M = maps[0].length;
return DFS(maps, N, M);
}
public static void main(String[] args) {
N1844 n1844 = new N1844();
int[][] maps = {{1, 0, 1, 1, 1}, {1, 0, 1, 0, 1}, {1, 0, 1, 1, 1}, {1, 1, 1, 0, 1}, {0, 0, 0, 0, 1}};
System.out.println(n1844.solution(maps));
}
}
// Graph μμ± ν DFS
import java.util.*;
public class N49189 {
public HashMap<Integer, List<Integer>> inItGraph(int n, int[][] edge) {
HashMap<Integer, List<Integer>> tmp = new HashMap<>();
for (int i = 1; i <= n; i++) {
tmp.put(i, new ArrayList<>());
}
for (int[] info : edge) {
int start = info[0];
int end = info[1];
tmp.get(start).add(end);
tmp.get(end).add(start);
}
return tmp;
}
public int solution(int n, int[][] edge) {
List<Integer> answer = new ArrayList<>();
HashMap<Integer, List<Integer>> graph = inItGraph(n, edge);
boolean[] visit = new boolean[n + 1];
visit[1] = true;
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{1, 0});
while (!queue.isEmpty()) {
int[] info = queue.poll();
int node = info[0];
int cost = info[1];
answer.add(cost);
for (int visitNode : graph.get(node)) {
if (!visit[visitNode]) {
queue.add(new int[]{visitNode, cost + 1});
visit[visitNode] = true;
}
}
}
return Collections.frequency(answer, Collections.max(answer));
}
public static void main(String[] args) {
N49189 n49189 = new N49189();
int n = 6;
int[][] edge = {{3, 6}, {4, 3}, {3, 2}, {1, 3}, {1, 2}, {2, 4}, {5, 2}};
System.out.println(n49189.solution(n, edge));
}
}
Leave a comment