반응형

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

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

public class Main {

	static int n;
	static int m;
	static int[][] board;
	static int[] dice = {0,0,0,0,0,0};
	static int k;
	static int[] move;
	static Queue<Location> q;
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		n = sc.nextInt();
		m = sc.nextInt();
		
		board = new int[n][m];
		q = new LinkedList<>();
		
		int x = sc.nextInt();
		int y = sc.nextInt();
		
		q.offer(new Location(x, y));
		
		k = sc.nextInt(); //명령어 개수
		
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				board[i][j] = sc.nextInt();
			}
		}
		
		move = new int[k];
		
		for(int i=0;i<k;i++){
			move[i] = sc.nextInt();
		}
		
		moveDice();
	}
	
	public static void moveDice(){
		
		int[] xx = {0,0,0,-1,1};
		int[] yy = {0,1,-1,0,0};
		
		for(int i=0;i<k;i++){
			
			int x = q.peek().x;
			int y = q.poll().y;
			
			int ax = x+xx[move[i]];
			int ay = y+yy[move[i]];
			
			
			if(ax>=0 && ay>=0 && ax<n && ay<m){
				rollDice(move[i]);
				if(board[ax][ay] == 0){
					board[ax][ay] = dice[5];
				}else{
					dice[5] = board[ax][ay];
					board[ax][ay] = 0;
				}
				q.offer(new Location(ax, ay));
			}else{
				q.offer(new Location(x, y));
				continue;
			}
			
			System.out.println(dice[0]);
		}
		
		
	}
	
	public static void rollDice(int dir){
		
		// 1 : 왼 // 2 : 우 // 3 : 위 // 4 : 아래
		
		int temp=0;
		
		if(dir == 2){
			temp = dice[0];
			dice[0] = dice[2];
			dice[2] = dice[5];
			dice[5] = dice[3];
			dice[3] = temp;
		}else if(dir == 1){
			temp = dice[0];
			dice[0] = dice[3];
			dice[3] = dice[5];
			dice[5] = dice[2];
			dice[2] = temp;
		}else if(dir == 3){
			temp = dice[0];
			dice[0] = dice[4];
			dice[4] = dice[5];
			dice[5] = dice[1];
			dice[1] = temp;
		}else if(dir == 4){
			temp = dice[0];
			dice[0] = dice[1];
			dice[1] = dice[5];
			dice[5] = dice[4];
			dice[4] = temp;
		}
	}

	
	
}
반응형

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

[Baekjoon] #13458 시험 감독  (0) 2022.06.22
[Baekjoon] #13460 구슬 탈출 2  (0) 2022.06.22
[Baekjoon] #15649 N과 M (1)  (0) 2022.06.22
[Baekjoon] #15650 N과 M (2)  (0) 2022.06.22
[Baekjoon] #17086 아기 상어2  (0) 2022.06.22
반응형

import java.util.Scanner;

public class Main {

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

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
	
		arr = new int[m];
		visit = new int[n];
		
		getDFS(n, m, 0);
	}
	
	public static void getDFS(int n, int m, int depth) {
		
		if(depth == m) {
			for(int i=0;i<arr.length;i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println("");
			return;
		}
		
		for(int i=0;i<n;i++) {
			if(visit[i] == 0) {
				visit[i] = 1;
				arr[depth] = i+1;
				getDFS(n,m,depth+1);
				
				visit[i] = 0;
			}
		}
	}

}
반응형

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

[Baekjoon] #13460 구슬 탈출 2  (0) 2022.06.22
[Baekjoon] #14499 주사위 굴리기  (0) 2022.06.22
[Baekjoon] #15650 N과 M (2)  (0) 2022.06.22
[Baekjoon] #17086 아기 상어2  (0) 2022.06.22
[Baekjoon] #10773 제로  (0) 2022.05.26
반응형

import java.util.Scanner;

public class Main {

	static int[] arr;
	static int n;
	static int m;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		
		arr = new int[m];
		
		getDFS(1, 0);
	}
	
	public static void getDFS(int num, int depth) {
		
		if(depth == m) {
			for(int val : arr) {
				System.out.print(val + " ");
			}
			System.out.println();
			return;
		}
		
		for(int i=num;i<=n;i++) {
			arr[depth] = i;
			getDFS(i+1, depth+1);
		}
	}

}
반응형

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

[Baekjoon] #14499 주사위 굴리기  (0) 2022.06.22
[Baekjoon] #15649 N과 M (1)  (0) 2022.06.22
[Baekjoon] #17086 아기 상어2  (0) 2022.06.22
[Baekjoon] #10773 제로  (0) 2022.05.26
[Baekjoon] #11066 파일 합치기  (0) 2022.05.23
반응형

 

 

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

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

public class Main {

	static int n;
	static int m;
	static int[][] map;
	static int[][] visit;
	static int totalMax = 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];
		visit = new int[n][m];
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				map[i][j] = sc.nextInt();
			}
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(map[i][j] == 0) {
					GetBFS(i, j);					
				}
			}
		}
		
		System.out.println(totalMax);
	}
	
	public static void GetBFS(int x, int y) {
		int max = 0;
		int[] xx = {-1, -1, 0, 1, 1, 1, 0, -1};
		int[] yy = {0, 1, 1, 1, 0, -1, -1, -1};
		
		visit = new int[n][m];
		Queue<Location17086> q = new LinkedList<>();
		
		q.add(new Location17086(x, y));
		visit[x][y] = 1;
		
		while(!q.isEmpty()) {
			
			int size = q.size();
			
			for(int s=0;s<size;s++) {
				int curX = q.peek().x;
				int curY = q.poll().y;
				
				if(map[curX][curY] == 1) {
					totalMax = Math.max(max, totalMax);
					return;
				}
				
				for(int i=0;i<8;i++) {
					int ax = curX + xx[i];
					int ay = curY + yy[i];
					
					if(ax >=0 && ay>=0 && ax<n && ay<m && visit[ax][ay] == 0) {
						q.add(new Location17086(ax, ay));
						visit[ax][ay] = 1;
					}
				}
			}
			
			max++;
		}
		
	}

}
반응형

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

[Baekjoon] #15649 N과 M (1)  (0) 2022.06.22
[Baekjoon] #15650 N과 M (2)  (0) 2022.06.22
[Baekjoon] #10773 제로  (0) 2022.05.26
[Baekjoon] #11066 파일 합치기  (0) 2022.05.23
[Baekjoon] #6593 상범 빌딩  (0) 2022.05.18
반응형

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const caseCount = Number(input[0]);
const stack = [];

for(let i=1;i<=caseCount;i++){
    const value = Number(input[i]);

    if(value == 0 ){
        stack.pop();
    } else {
        stack.push(value);
    }
}

let result = 0;

for(let i=0;i<stack.length;i++){
    result += stack[i];
}

console.log(result);
반응형

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

[Baekjoon] #15650 N과 M (2)  (0) 2022.06.22
[Baekjoon] #17086 아기 상어2  (0) 2022.06.22
[Baekjoon] #11066 파일 합치기  (0) 2022.05.23
[Baekjoon] #6593 상범 빌딩  (0) 2022.05.18
[Baekjoon] #14502 연구소  (0) 2022.04.07

+ Recent posts