Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions MinJu/9week/src/Simulation12100.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import java.io.*;
import java.util.*;

public class Simulation12100 {
static int n;
static int[][] board = new int[20][20];
static int[][] mainboard = new int[20][20];
static boolean[][] status = new boolean[20][20]; //합쳐진 상태
static int maxblock=0;

//최대 5번 이동해서 만들 수 있는 가장 큰 블록의 값
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int i = 0; i < n; i++) { //2048 칸 채우기
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
mainboard[i][j] = Integer.parseInt(st.nextToken());
}
}

//상하좌우 이동을 5번하고 가장 큰 블록을 찾는다. 4*4*4*4*4
int i = 0;
while (i < 1024) {
for (int j = 0; j < n; j++) { //이동할 보드 클론
board[j] = mainboard[j].clone();
}

int t=i;
for (int j = 0; j <= 4; j++) {
int ti= t%4;
if (ti == 0) {
up();
}
if (ti == 1) {
down();
}
if (ti == 2) {
left();
}
if (ti == 3) {
right();
}
for (int l = 0; l < n; l++) {
Arrays.fill(status[l], false); //상태 행렬 초기화
}

t/=4;
}

i++;
int max=0;
for (int j = 0; j < n; j++) {
for (int l = 0; l < n; l++) {
if(max<board[j][l]) {
max=board[j][l];
}
}
}
maxblock=Math.max(maxblock, max);
}

System.out.println(maxblock);
}

public static void up() {
for (int j = 0; j < n; j++) { //열
for (int i = 1; i < n; i++) { //행
if (board[i][j] != 0) { //블록에 숫자가 써있는 경우
int temp = i - 1; //가장 위에 위치하는 숫자 블록 위치
while (temp >= 0) {
if (board[temp][j] != 0) {
break;
}
temp--;
}
if(temp==i-1&& board[temp][j]!=board[i][j]) continue; //아무일도 일어나지 않는 경우

if (temp!=-1 && !status[temp][j] && board[temp][j] == board[i][j]) { //블록이 합쳐지는 경우
board[temp][j] += board[i][j];
status[temp][j] = true;
board[i][j] = 0;
continue;
}

//블록이 합쳐지지 않는 경우 : 이동만
board[temp + 1][j] = board[i][j];
board[i][j] = 0;
}
}
}
}

public static void down() {
for (int j = 0; j <n; j++) { //열
for (int i = n-2; i >=0; i--) { //행
if (board[i][j] != 0) { //블록에 숫자가 써있는 경우
int temp = i +1; //가장 아래 숫자 블록 위치
while (temp <n) {
if (board[temp][j] != 0) {
break;
}
temp++;
}
if(temp==i+1&& board[temp][j]!=board[i][j]) continue; //아무일도 일어나지 않는 경우

if (temp!=n && !status[temp][j] && board[temp][j] == board[i][j]) { //블록이 합쳐지는 경우
board[temp][j] += board[i][j];
status[temp][j] = true;
board[i][j] = 0;
continue;
}

//블록이 합쳐지지 않는 경우 : 이동만
board[temp - 1][j] = board[i][j];
board[i][j] = 0;
}
}
}
}

public static void left() {
for (int i = 0; i < n; i++) { //행
for (int j = 1; j < n; j++) { //열
if (board[i][j] != 0) { //블록에 숫자가 써있는 경우
int temp = j - 1; //가장 왼쪽에 위치하는 숫자 블록 위치
while (temp >= 0) {
if (board[i][temp] != 0) {
break;
}
temp--;
}
if(temp==j-1&& board[i][temp]!=board[i][j]) continue; //아무일도 일어나지 않는 경우

if (temp!=-1 && !status[i][temp] && board[i][temp] == board[i][j]) { //블록이 합쳐지는 경우
board[i][temp] += board[i][j];
status[i][temp] = true;
board[i][j] = 0;
continue;
}

//블록이 합쳐지지 않는 경우 : 이동만
board[i][temp+1] = board[i][j];
board[i][j] = 0;
}
}
}
}

public static void right() {
for (int i = 0; i < n; i++) { //행
for (int j = n-2; j >=0; j--) { //열
if (board[i][j] != 0) { //블록에 숫자가 써있는 경우
int temp = j + 1; //가장 오른쪽에 위치하는 숫자 블록 위치
while (temp <n) {
if (board[i][temp] != 0) {
break;
}
temp++;
}
if(temp==j+1&& board[i][temp]!=board[i][j]) continue; //아무일도 일어나지 않는 경우

if (temp!=n && !status[i][temp] && board[i][temp] == board[i][j]) { //블록이 합쳐지는 경우
board[i][temp] += board[i][j];
status[i][temp] = true;
board[i][j] = 0;
continue;
}

//블록이 합쳐지지 않는 경우 : 이동만
board[i][temp-1] = board[i][j];
board[i][j] = 0;
}
}
}
}
}
95 changes: 95 additions & 0 deletions MinJu/9week/src/Simulation18808.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.io.*;
import java.util.*;

public class Simulation18808 {
static int n, m, k;
static boolean[][] note = new boolean[45][45]; //노트북
static int cnt=0; //스티커가 붙은 칸의 수
static Queue<int[][]> queue= new LinkedList<>(); //스티커 저장 배열

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken()); //노트북의 세로 길이
m = Integer.parseInt(st.nextToken()); //가로 길이
k = Integer.parseInt(st.nextToken()); //스티커의 개수

for (int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());
int r= Integer.parseInt(st.nextToken());
int c= Integer.parseInt(st.nextToken());
int[][] sticker= new int[r][c];
for (int j = 0; j < r; j++) {
st = new StringTokenizer(br.readLine());
for (int l = 0; l < c; l++) {
sticker[j][l]=Integer.parseInt(st.nextToken());
}
}
queue.add(sticker);
}

while (!queue.isEmpty()) { //차례대로 스티커 붙이기
int[][] sticker = queue.poll();

boolean isCnt=false;
//90도씩 돌려서 네 번 반복
for (int t = 0; t < 4; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
isCnt=func(sticker, i, j); //스티커 대보기
if(isCnt) {
func2(sticker, i, j);
break;
}
}
if(isCnt) break;
}
if(isCnt) break;
sticker = func3(sticker).clone(); //스티커 90도 회전
}
}

System.out.println(cnt);
}

public static boolean func(int[][] sticker, int x, int y) { //sticker를 붙일 수 있는지 확인
int r = sticker.length;
int c = sticker[0].length;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if(x+i>=n || y+j>=m) return false; //범위 밖
if(sticker[i][j]==1 && note[x+i][y+j]){ //스티커를 붙일 수 없다.
return false;
}
}
}
return true;
}

public static void func2(int[][] sticker, int x, int y) { //sticker 붙이기
int r = sticker.length;
int c = sticker[0].length;

for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if(sticker[i][j]==1){ //스티커를 붙일 수 없다.
note[x+i][y+j]=true;
cnt++;
}
}
}
}

public static int[][] func3(int[][] sticker) {
int r= sticker.length;
int c= sticker[0].length;
int[][] newSticker= new int[c][r];

for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
newSticker[j][r-1-i] = sticker[i][j];
}
}
return newSticker;
}
}