반응형

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

	private static int[][] M, T;
	private static int[] books = new int[501];
	
	public static void main(String[] args)throws IOException{
		int loopCount = Integer.parseInt(reader.readLine());
		
		while(loopCount-- > 0){
			int n = Integer.parseInt(reader.readLine());
			books = new int[n + 1];
			M = new int[n + 1][n + 1];
			T = new int[n + 1][n + 1];
			
			StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
			for(int i = 1; i <= n; i++)books[i] = Integer.parseInt(tokenizer.nextToken());
			
			for(int i = 1; i <= n; i++){
				for(int j = i; j <= n; j++)M[i][j] = M[i][j - 1] + books[j];
				Arrays.fill(T[i], -1);
			}
			if(n != 1){
				writer.write(String.valueOf(dp(1, n)) + "\n");	
			}
			else{
				writer.write(String.valueOf(books[1]) + "\n");
			}
						
		}
		writer.flush();
	}
	private static int dp(int i, int j){
		if(T[i][j] == -1){
			if(i == j){
				T[i][j] = 0;
			}
			else{
				T[i][j] = 999999111;
				for(int k = i; k < j; k++)T[i][j] = min(M[i][k] + M[k + 1][j] + dp(i, k) + dp(k + 1, j), T[i][j]);	
			}
		}
		return T[i][j];
	}
	private static int min(int val1, int val2){
		return val1 > val2? val2 : val1;
	}
}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #17086 아기 상어2  (0) 2022.06.22
[Baekjoon] #10773 제로  (0) 2022.05.26
[Baekjoon] #6593 상범 빌딩  (0) 2022.05.18
[Baekjoon] #14502 연구소  (0) 2022.04.07
[Baekjoon] #7568 덩치  (0) 2022.03.15
반응형

배치(Batch)

  • 사용자와의 상호작용 없이 대량의 데이터를 처리하는 일련의 작업들을 묶어 정기적으로 반복 수행하거나 정해진 규칙에 따라 자동으로 수행하는 것

    • 정기 배치 : 정해진 시점(주로 야간)에 실행
    • 이벤트성 배치 : 사전에 정의해 둔 조건이 충족되면 자동으로 실행
    • On-Demand 배치 : 사용자의 명시적인 요구가 있을 때마다 실행
      예제) 대량 데이터 처리, 대량 인쇄, 일괄 이미지 처리, 백업 데이터 생성, 일괄 데이터 변환 등

참고 사이트
https://velog.io/@emawlrdl/%EC%98%A4%EB%9D%BC%ED%81%B4-%EB%B0%B0%EC%B9%98Batch
https://dataonair.or.kr/db-tech-reference/d-guide/sql/?mod=document&uid=374

반응형
반응형

 

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

//Q)상범빌딩
//#6593

class Location{
	
	int f;
	int x;
	int y;
	
	public Location(int f, int x, int y) {
		this.f = f;
		this.x = x;
		this.y = y;
		// TODO Auto-generated constructor stub
	}
}

public class Main {

	static int l;
	static int r;
	static int c;
	static char[][][] board;
	static int[][][] visit;
	static Queue<Location> q;
	static int e_x;
	static int e_y;
	static int e_f;
	static int s_x;
	static int s_y;
	static int s_f;
	
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		boolean flag = true;
		
		while(flag){
			
			l = sc.nextInt();
			r = sc.nextInt();
			c = sc.nextInt();
			
			if(l ==0 && r==0 && c==0){
				flag = false;
				return;
			}
			
			board = new char[l][r][c];
			visit = new int[l][r][c];
			q = new LinkedList<>();

			sc.nextLine();
			
			for(int i=0;i<l;i++){
				for(int j=0;j<r;j++){
					String str = sc.next();
					for(int k=0;k<c;k++){
						board[i][j][k] = str.charAt(k);
					}
				}
				sc.nextLine();
			}
			
			sc.nextLine();
	
			for(int i=0;i<l;i++){
				for(int j=0;j<r;j++){
					for(int k=0;k<c;k++){
						if(board[i][j][k] == 'S'){
							q.offer(new Location(i, j, k));
							visit[i][j][k] = 1;
						}
						if(board[i][j][k] == 'E'){
							e_f = i;
							e_x = j;
							e_y = k;
						}
					}
				}
			}
			
			getSearch();
			
			//getSearch();
			//System.out.println(visit[e_f][e_x][e_y]-1);
		}
	}
	public static void getSearch(){
		
		int[] x = {-1, 0, 1, 0, 0, 0};
		int[] y = {0, 1, 0, -1, 0, 0};
		int[] m = {0, 0, 0, 0, 1, -1};
		int cnt =0;
		boolean noEnd_flag = true;
		
		while(!q.isEmpty()){
			
			int size = q.size();
			
			for(int i=0;i<size;i++){
				
				s_f = q.peek().f;
				s_x = q.peek().x;
				s_y = q.poll().y;

				if(s_f == e_f && s_x == e_x && s_y == e_y){
					System.out.println("Escaped in "+cnt+" minute(s).");
					noEnd_flag = false;
					break;
				}
				
				for(int j=0;j<6;j++){
					
					int ff = s_f+m[j];					
					int xx = s_x+x[j];
					int yy = s_y+y[j];
					if(xx>=0 && yy>=0 && xx<r && yy<c && ff>=0 && ff<l){
						if(board[ff][xx][yy] != '#' && visit[ff][xx][yy] == 0){
							visit[ff][xx][yy] = 1;
							q.offer(new Location(ff, xx, yy));
						}
					}
				}
				
			}
			cnt++;
			
		}
		if(noEnd_flag){
			System.out.println("Trapped!");
		}
	}
	
}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #10773 제로  (0) 2022.05.26
[Baekjoon] #11066 파일 합치기  (0) 2022.05.23
[Baekjoon] #14502 연구소  (0) 2022.04.07
[Baekjoon] #7568 덩치  (0) 2022.03.15
[Baekjoon] #2589 보물섬  (0) 2022.03.13
반응형

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class VirusLocation{
	int x;
	int y;
	
	public VirusLocation(int x, int y) {
		// TODO Auto-generated constructor stub
		this.x = x;
		this.y = y;
	}
}

public class Baekjoonalgo_14052 {

	static int[][] map;
	
	static int n;
	static int m;
	static int result = 0;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		
		n = sc.nextInt();
		m = sc.nextInt();
		
		map = new int[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				map[i][j] = sc.nextInt();
			}
		}
		
		// 벽 세우기
		SetWall(0);
		
		System.out.println(result);
	}
	
	// DFS 벽 세우기 (꼭 3개)
	public static void SetWall(int wallCnt) {
		if(wallCnt == 3) {
			SpreadVirus();
			return;
		}
		
		// 벽 세우기
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(map[i][j] == 0) {
					map[i][j] = 1;
					SetWall(wallCnt+1);
					
					// 다시 되돌리기
					map[i][j] = 0;
				}
			}
		}
	}
	
	// BFS 바이러스 확산
	public static void SpreadVirus() {
		int safeCnt = 0;
		int[] xx = {-1,0,1,0};
		int[] yy = {0,-1,0,1};
		
		int[][] virusMap = new int[n][m];
		Queue<VirusLocation> q = new LinkedList<>();
		
		// 바이러스 확산 맵
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				virusMap[i][j] = map[i][j];
				if(virusMap[i][j] == 2) q.add(new VirusLocation(i, j));
			}
		}
		
		while(!q.isEmpty()) {
			int virusX = q.peek().x;
			int virusY = q.poll().y;
			
			for(int i=0;i<4;i++) {
				int ax = virusX + xx[i];
				int ay = virusY + yy[i];
				
				if(ax >=0 && ay>=0 && ax<n && ay<m && virusMap[ax][ay] == 0) {
					virusMap[ax][ay] = 2;
					q.add(new VirusLocation(ax, ay));
				}
			}
		}
		
		// 안전 영역 카운트
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(virusMap[i][j] == 0) {
					safeCnt++;
				}
			}
		}
		
		result = Math.max(safeCnt, result);
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #11066 파일 합치기  (0) 2022.05.23
[Baekjoon] #6593 상범 빌딩  (0) 2022.05.18
[Baekjoon] #7568 덩치  (0) 2022.03.15
[Baekjoon] #2589 보물섬  (0) 2022.03.13
[Baekjoon] #1978 소수 찾기  (0) 2022.03.12
반응형

 

[브루트포스]

📢 제한 N, x,y 값이 크지 않은 것으로 보아 브루트포스 알고리즘으로 접근할 수 있다.

 

[풀이]

✔ 몸무게, 키를 담은 객체를 ArrayList에 담은 후,

이차 For문을 통해 모든 객체를 비교하여 등수를 매기는 방식으로 접근하였다.

 

import java.util.ArrayList;
import java.util.Scanner;

class info7568{
	int w;
	int h;
	
	public info7568(int w, int h) {
		// TODO Auto-generated constructor stub
		this.w = w;
		this.h = h;
	}
}

public class BaekjoonAlgo_7568 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		
		int n= sc.nextInt();
		ArrayList<info7568> ar = new ArrayList<>();
		
		StringBuilder sb = new StringBuilder();
		
		for(int i=0;i<n;i++) {
			int w = sc.nextInt();
			int h = sc.nextInt();
			
			ar.add(new info7568(w, h));
		}
		
		for(int i=0;i<ar.size();i++) {
			int w1 = ar.get(i).w;
			int h1 = ar.get(i).h;
			int rankNum = 1;
			
			for(int j=0;j<ar.size();j++) {
				int w2 = ar.get(j).w;
				int h2 = ar.get(j).h;
				
				if(w2 > w1 && h2 > h1) {
					rankNum++;
				}
			}
			
			sb.append(rankNum + "\n");
		}
		
		System.out.println(sb);
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #6593 상범 빌딩  (0) 2022.05.18
[Baekjoon] #14502 연구소  (0) 2022.04.07
[Baekjoon] #2589 보물섬  (0) 2022.03.13
[Baekjoon] #1978 소수 찾기  (0) 2022.03.12
[Baekjoon] #2571 수 정렬하기 2  (0) 2022.03.12

+ Recent posts