728x90
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<deque>
#include<math.h>
#include<cmath>
using namespace std;
int dice[4][3];
int mp[21][21];
int n, m;
int k;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1}; //남 북 동 서
bool canGo(int x, int y)
{
return x >= 0 && y >= 0 && x < n && y < m;
}
void print() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
cout << dice[i][j]<<" ";
}
cout << endl;
}
}
void movesouth() { //남
int j = 1;
int tmp = dice[3][1];
for (int i = 3; i >0; i--) {
dice[i][j] = dice[i-1][j]; //헷갈렸던 부분
}
dice[0][1] = tmp;
}
void movenorth() { //북
int j = 1;
int tmp = dice[0][1];
for (int i = 0; i < 3; i++) {
dice[i][j] = dice[i+1][j];
}
dice[3][1] = tmp;
}
void moveeast() { //동
int i = 1;
int tmp = dice[1][2];
for (int j = 2; j > 0; j--) {
dice[i][j] = dice[i][j-1]; //헷갈렸던 부분
}
dice[1][0] = dice[3][1];
dice[3][1] = tmp;
}
void movewest() { //서
int i = 1;
int tmp = dice[1][0];
for (int j = 0; j < 2; j++) {
dice[i][j] = dice[i][j + 1];
}
dice[1][2] = dice[3][1];
dice[3][1] = tmp;
}
void simulation(vector<int>&dir,int x, int y) {
for (int i = 0; i < dir.size(); i++) {
if (dir[i] == 1) { //동
int nx = x + dx[2];
int ny = y + dy[2];
if (canGo(nx, ny)) {
moveeast();
if (mp[nx][ny] != 0) {
dice[3][1] = mp[nx][ny];
mp[nx][ny] = 0;
}
else {
mp[nx][ny] = dice[3][1];
}
cout << dice[1][1]<<endl;
x = nx; y = ny;
}
}
else if (dir[i] == 2) { //서
int nx = x + dx[3];
int ny = y + dy[3];
if (canGo(nx, ny)) {
movewest();
if (mp[nx][ny] != 0) {
dice[3][1] = mp[nx][ny];
mp[nx][ny] = 0;
}
else {
mp[nx][ny] = dice[3][1];
}
cout << dice[1][1]<<endl;
x = nx; y = ny;
}
}
else if (dir[i] == 3) { //북
int nx = x + dx[1];
int ny = y + dy[1];
if (canGo(nx, ny)) {
movenorth();
if (mp[nx][ny] != 0) {
dice[3][1] = mp[nx][ny];
mp[nx][ny] = 0;
}
else {
mp[nx][ny] = dice[3][1];
}
cout << dice[1][1]<<endl;
x = nx; y = ny;
}
}
else if (dir[i] == 4) { //남
int nx = x + dx[0];
int ny = y + dy[0];
if (canGo(nx, ny)) {
movesouth();
if (mp[nx][ny] != 0) {
dice[3][1] = mp[nx][ny];
mp[nx][ny] = 0;
}
else {
mp[nx][ny] = dice[3][1];
}
cout << dice[1][1]<<endl;
x = nx; y = ny;
}
}
}
}
int main() {
cin >> n;
cin >> m;
int x, y;
cin >> x;
cin >> y;
cin >> k;
vector<int>dir(k);
memset(dice, 0, sizeof(dice));
memset(mp, 0, sizeof(mp));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mp[i][j];
}
}
for (int i = 0; i < k; i++) {
cin >> dir[i];
}
simulation(dir,x,y);
}
<Java>
package Samsung;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class 주사위굴리기 {
static int n,m;
static int[][] arr;
static int[] way;
static int[][] dice;
static int dx[] = {0,0,0,-1,1}; //동서북남
static int dy[] = {0,1,-1,0,0};
static public void movenorth() { //북으로 움직일때
int tmp = dice[3][1];
for(int i=2; i>=0; i--) {
dice[i+1][1]=dice[i][1];
}
dice[0][1]=tmp;
}
static public void movesouth() { //남으로 움직일떄
int tmp = dice[0][1];
for(int i=1; i<=3; i++) {
dice[i-1][1]=dice[i][1];
}
dice[3][1]=tmp;
}
static public void moveeast() { //동으로 움직일때
int tmp = dice[1][0];
for(int j=1;j<=2; j++) {
dice[1][j-1]=dice[1][j];
}
dice[1][2]=dice[3][1];
dice[3][1]=tmp;
}
static public void movewest() { //서로 움직일떄
int tmp = dice[1][2];
for(int j=1; j>=0; j--) {
dice[1][j+1]=dice[1][j];
}
dice[1][0]=dice[3][1];
dice[3][1]=tmp;
}
public static boolean cango(int x,int y) {
return x>=0 && x<n && y>=0 && y<m;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
way = new int[k];
arr = new int[n][m];
for(int i=0; i<n; i++) {
String s = br.readLine();
st = new StringTokenizer(s);
for(int j=0; j<m; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
for(int i=0; i<k; i++) {
way[i] = Integer.parseInt(st.nextToken());
}
dice = new int[4][3]; //배열에 주사위의 전개도를 나타낸다.
int bx = 1;
int by = 1; //bx,by 바닥면 숫자 좌표
for(int i=0; i<k; i++) {
int go = way[i];
if(go==1) {
x+=dx[go];
y+=dy[go];
if(!cango(x,y)) { //범위를 벗어나서 못움직이면
x-=dx[go];
y-=dy[go];
continue;
}
moveeast();
if(arr[x][y]!=0) {dice[bx][by]=arr[x][y]; arr[x][y]=0;}
else {arr[x][y]=dice[bx][by];} //조건 잘 파악해야 함!
}
else if(go==2) {
x+=dx[go];
y+=dy[go];
if(!cango(x,y)) {
x-=dx[go];
y-=dy[go];
continue;
}
movewest();
if(arr[x][y]!=0) {dice[bx][by]=arr[x][y]; arr[x][y]=0;}
else {arr[x][y]=dice[bx][by];}
}
else if(go==3) {
x+=dx[go];
y+=dy[go];
if(!cango(x,y)) {
x-=dx[go];
y-=dy[go];
continue;
}
movenorth();
if(arr[x][y]!=0) {dice[bx][by]=arr[x][y]; arr[x][y]=0;}
else {arr[x][y]=dice[bx][by];}
}
else {
x+=dx[go];
y+=dy[go];
if(!cango(x,y)) {
x-=dx[go];
y-=dy[go];
continue;
}
movesouth();
if(arr[x][y]!=0) {dice[bx][by]=arr[x][y]; arr[x][y]=0;}
else {arr[x][y]=dice[bx][by];}
}
System.out.println(dice[3][1]);
}
}
}
- 주사위 동서남북으로 움직이는 부분을 좀더 빨리 짤 수 있으면 좋겠다.
'백준 > 삼성기출' 카테고리의 다른 글
경사로- Java, 구현 (0) | 2021.04.02 |
---|---|
스타트와 링크 - Java, 백트래킹 (0) | 2021.04.01 |
시험감독 (0) | 2021.03.25 |
뱀 (0) | 2021.03.25 |
2048(easy) - DFS/시뮬레이션 (0) | 2021.03.23 |