Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 다익스트라
- GatherTown
- BFS
- 네트워크플로우
- spring
- dp
- 백준
- 엘라스틱서치
- 백준코딩테스트
- 01BFS
- 우선순위큐
- 세그먼트트리
- 자바PS
- 알고리즘
- 취득후기
- QUICKSTARTGUIDE
- deque
- COSPROJAVA1급
- 게더타운시작
- 시뮬레이션
- java
- COSPRO
- 구현
- YBMCOS
- DFS
- 이젠 골드구현도 어렵네..
- 다이나믹프로그래밍
- 재귀함수
- PS
- 완전탐색
Archives
- Today
- Total
공부공간
BOJ - 14499 ) 주사위 굴리기 본문
14499번: 주사위 굴리기
첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도
www.acmicpc.net
주사위를 1차원 배열이라고 생각하고 동서남북에대해서 index의 변화를 하드코딩하자.
문제정의->규칙->모델링에 30분투자하자..
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class 주사위굴리기 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int map[][] = new int[n][m];
for(int i=0;i<n;i++) {st=new StringTokenizer(br.readLine());for(int j=0;j<m;j++) {map[i][j] =Integer.parseInt(st.nextToken()); }}
int move[] = new int[k]; st = new StringTokenizer(br.readLine());
for(int i=0;i<k;i++)move[i]=Integer.parseInt(st.nextToken()); //end of input
int dir[][]= {{0,1},{0,-1},{-1,0},{1,0}};
int dice[]=new int[7];
StringBuilder sb = new StringBuilder();
for(int i=0;i<k;i++) {
int d=move[i];
int nx =x+dir[d-1][1]; int ny=y+dir[d-1][0];
if(ny>=0&&nx>=0&&nx<m&&ny<n) {
x=nx;y=ny;
if(d==1) {dice[0]=dice[6];dice[6]=dice[3];dice[3]=dice[5];dice[5]=dice[1];dice[1]=dice[0];}
else if(d==2) {dice[0]=dice[3];dice[3]=dice[6];dice[6]=dice[1];dice[1]=dice[5];dice[5]=dice[0];}
else if(d==3) {dice[0]=dice[2];dice[2]=dice[1];dice[1]=dice[4];dice[4]=dice[3];dice[3]=dice[0];}
else if(d==4) {dice[0]=dice[2];dice[2]=dice[3];dice[3]=dice[4];dice[4]=dice[1];dice[1]=dice[0];}
if(map[ny][nx]==0) {
map[ny][nx]=dice[1];sb.append(dice[3]+"\n");
} else {
dice[1]=map[ny][nx];map[ny][nx]=0;sb.append(dice[3]+"\n");
}}
}System.out.println(sb);
}
}
'알고리즘 > 구현,시뮬' 카테고리의 다른 글
BOJ - 17837 ) 새로운게임 2 (0) | 2020.10.16 |
---|---|
BOJ - 17140 ) 이차원 배열과 연산 (1) | 2020.09.14 |
프로그래머스 ) 블록게임 (0) | 2020.09.07 |
Trie 에 대해서 알아봅시다 + Java구현 + [백준] 전화번호 목록 풀이 (6) | 2020.09.02 |
BOJ - 14500 ) 테트로미노 (0) | 2020.08.27 |
Comments