Skip to content

Commit

Permalink
change directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyi authored and dongyi committed Mar 11, 2022
1 parent c17c977 commit 4c65f42
Show file tree
Hide file tree
Showing 75 changed files with 2,764 additions and 2,764 deletions.
63 changes: 63 additions & 0 deletions Bonus/Problem05A/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.*;
import java.lang.*;
import java.util.*;


public class Main {
public static final Scanner scanner = new Scanner(System.in);

public static final int EMPTY = 0; // 폐기물이 존재하지 않는 칸
public static final int EXISTS = 1; // 폐기물이 존재하는 칸

public static void testCase(int caseIndex) {
int N = scanner.nextInt();
int K = scanner.nextInt();

// 각 칸의 정보를 입력받는다.
int[][] wastes = new int[N][N];
for (int r = 0; r < N; r += 1) {
for (int c = 0; c < N; c += 1) {
// wastes[r][c] := (r행 c열)칸의 폐기물 존재 여부
wastes[r][c] = scanner.nextInt();
}
}

int answer = Integer.MAX_VALUE;

// K*K 크기의 영역이 존재할 수 있는 모든 지점을 탐색한다.
// 왼쪽 위 모서리 칸을 (firstRow, firstCol)로 기준을 두고 탐색한다.
for (int firstRow = 0; firstRow + K - 1 < N; firstRow += 1) {
for (int firstCol = 0; firstCol + K - 1 < N; firstCol += 1) {

int lastRow = firstRow + K - 1;
int lastCol = firstCol + K - 1;

// 해당 영역 내부에 존재하는 폐기물의 수를 계산한다
int numberOfWastes = 0;

for (int r = firstRow; r <= lastRow; r += 1) {
for (int c = firstCol; c <= lastCol; c += 1) {
if (wastes[r][c] == EXISTS) {
numberOfWastes += 1;
}
}
}

// 최소값을 갱신한다.
answer = Math.min(numberOfWastes, answer);
}
}

System.out.println(answer);
}

public static void main(String[] args) throws Exception {
int caseSize = scanner.nextInt();

for (int caseIndex = 1; caseIndex <= caseSize; caseIndex += 1) {
testCase(caseIndex);
}

}

}
File renamed without changes.
64 changes: 64 additions & 0 deletions Bonus/Problem05A/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<cstdio>
#include<cstdlib>
#include<memory.h>
#include<iostream>
#include<algorithm>


using namespace std;


int getTile(int** waste, int K, int row, int col) {
int answer = 0;

for (int i = row; i < row + K; ++i) {
for (int j = col; j < col + K; ++j) {
if (waste[i][j]) answer++;
}
}
return answer;

}
int get_minimum_trashes(int** waste, int N, int K) {

int answer = getTile(waste, K, 0, 0);
for (int row = 0; row <= N - K; ++row) {
for (int col = 0; col <= N - K; ++col) {
answer = std::min(answer, getTile(waste, K, row, col));
}

}

return answer;
}

void test_case(int caseIndex) {
int N, K;
std::cin >> N >> K;

int** wastes = new int* [N];
for (int r = 0; r < N; r += 1) {
wastes[r] = new int[N];
for (int c = 0; c < N; c += 1) {
std::cin >> wastes[r][c];
}
}

int answer = get_minimum_trashes(wastes, N, K);
printf("%d\n", answer);

for (int r = 0; r < N; r += 1) {
delete[] wastes[r];
}
delete[] wastes;
}

int main() {
int caseSize;
std::cin >> caseSize;

for (int caseIndex = 0; caseIndex < caseSize; caseIndex += 1) {
test_case(caseIndex);
}
return 0;
}
File renamed without changes.
79 changes: 79 additions & 0 deletions Bonus/Problem05B/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.io.*;
import java.lang.*;
import java.util.*;


public class Main {
public static final Scanner scanner = new Scanner(System.in);

public static final int EMPTY = 0; // 폐기물이 존재하지 않는 칸
public static final int EXISTS = 1; // 폐기물이 존재하는 칸

public static void testCase(int caseIndex) {
int N = scanner.nextInt();
int K = scanner.nextInt();

// 각 칸의 정보를 입력받는다.
int[][] wastes = new int[N][N];
for (int r = 0; r < N; r += 1) {
for (int c = 0; c < N; c += 1) {
// wastes[r][c] := (r행 c열)칸의 폐기물 존재 여부
wastes[r][c] = scanner.nextInt();
}
}

int answer = Integer.MAX_VALUE;

// K*K 크기의 영역이 존재할 수 있는 모든 지점을 탐색한다.
// 가장 위의 칸이 (firstRow)행에 존재하는 영역들을 조사한다.
for (int firstRow = 0; firstRow + K - 1 < N; firstRow += 1) {
int lastRow = firstRow + K - 1;

// 가장 왼쪽 K열로 구성된 영역에 대한 폐기물 수를 계산하고 갱신한다.
int numberOfWastes = 0;
for (int r = firstRow; r <= lastRow; r += 1) {
for (int c = 0; c <= K - 1; c += 1) {
if (wastes[r][c] == EXISTS) {
numberOfWastes += 1;
}
}
}
answer = Math.min(answer, numberOfWastes);

// 이후 열 방향으로 Sliding Window 기법으로 이동하며 영역의 정보를 갱신한다.
for (int c = K; c < N; c += 1)
{ // 기존 영역을 열방향으로 오른쪽으로 이동한 경우

int newColumn = c; // 새롭게 추가되는 열의 번호
int oldColumn = c - K; // 새롭게 제거되는 열의 번호

for (int r = firstRow; r <= lastRow; r += 1) {
// 해당 열에서 추가되는 폐기물의 수를 반영한다
if (wastes[r][newColumn] == EXISTS) {
numberOfWastes += 1;
}

// 해당 열에서 삭제되는 폐기물의 수를 반영한다
if (wastes[r][oldColumn] == EXISTS) {
numberOfWastes -= 1;
}
}

// 계산된 해당 영역의 폐기물 수를 갱신한다
answer = Math.min(answer, numberOfWastes);
}
}

System.out.println(answer);
}

public static void main(String[] args) throws Exception {
int caseSize = scanner.nextInt();

for (int caseIndex = 1; caseIndex <= caseSize; caseIndex += 1) {
testCase(caseIndex);
}

}

}
File renamed without changes.
94 changes: 94 additions & 0 deletions Bonus/Problem05C/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.io.*;
import java.lang.*;
import java.util.*;


public class Main {
public static final Scanner scanner = new Scanner(System.in);
public static final int[] deltaR = { -1, -1, -1, 0, 0, 1, 1, 1 };
public static final int[] deltaC = { -1, 0, 1, -1, 1, -1, 0, 1 };


public static void testCase(int caseIndex) {
int N = scanner.nextInt();
int M = scanner.nextInt();

GameMap gameMap = new GameMap(N, N);

for (int r = 0; r < N; r += 1) {
for (int c = 0; c < N; c += 1) {
int buildings = scanner.nextInt();
gameMap.setBuildingsAt(r, c, buildings);
}
}

int answer = 0;

for(int i = 0 ; i < M ; i += 1){
int r = scanner.nextInt() - 1;
int c = scanner.nextInt() - 1;

int buildings = 0;
buildings += gameMap.getBuildingsAt(r, c);

for(int di = 0 ; di < deltaR.length; di += 1){
for(int length = 1; length <= N; length += 1){
int newR = r + deltaR[di] * length;
int newC = c + deltaC[di] * length;
if(gameMap.isInside(newR, newC) == false){
break;
}

buildings += gameMap.getBuildingsAt(newR, newC);
}
}

answer = Math.max(answer, buildings);
}

System.out.println(answer);
}

public static void main(String[] args) throws Exception {
int caseSize = scanner.nextInt();

for (int caseIndex = 1; caseIndex <= caseSize; caseIndex += 1) {
testCase(caseIndex);
}
}

}

class GameMap{
public final int rows;
public final int columns;
private final int[][] buildings;

public GameMap(int rows, int columns){
this.rows = rows;
this.columns = columns;
this.buildings = new int[rows][columns];
}

public int getBuildingsAt(int r, int c){
if(this.isInside(r, c) == false)
return 0;

return this.buildings[r][c];
}

public void setBuildingsAt(int r, int c, int value){
this.buildings[r][c] = value;
}

public boolean isInside(int r, int c){
if(r < 0 || r >= this.rows)
return false;
if(c < 0 || c >= this.columns)
return false;
return true;
}



}
File renamed without changes.
Loading

0 comments on commit 4c65f42

Please sign in to comment.