#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int row, col;
int map[25][25];
int dice[7];
int diceX, diceY;
int commandNum;
 
int dx[5= {000-11}; // 동 서 북 남
int dy[5= {01-100}; // 동 서 북 남  
 
int safe(int x, int y)
{
    if(x >= 0 && y >= 0 && x < row && y < col)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void changeDice(int command)
{
    int temp[7];
    for(int i = 1; i <= 6; i++)
    {
        temp[i] = dice[i];
    }
    
    if(command == 1)
    {
        dice[1= temp[4];
        dice[3= temp[1];
        dice[4= temp[6];
        dice[6= temp[3];
    }
    else if(command == 2)
    {
        dice[1= temp[3];
        dice[3= temp[6];
        dice[4= temp[1];
        dice[6= temp[4];
    }
    else if(command == 3)
    {
        dice[1= temp[5];
        dice[2= temp[1];
        dice[5= temp[6];
        dice[6= temp[2];
    }
    else if(command == 4)
    {
        dice[1= temp[2];
        dice[2= temp[6];
        dice[5= temp[1];
        dice[6= temp[5];
    }    
}
 
int main(void)
{
//    freopen("B14499_input.txt", "r", stdin);
    
    cin >> row >> col >> diceX >> diceY >> commandNum;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> map[i][j];
        }
    }
    
    for(int i = 1; i <= commandNum; i++)
    {
        int command;
        cin >> command;
        
        diceX += dx[command];
        diceY += dy[command];
        
        if(safe(diceX, diceY) == 0)
        {
            diceX -= dx[command];
            diceY -= dy[command];
            continue;
        }
        
        changeDice(command);
        
        // 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다.
        if(map[diceX][diceY] == 0)
        {
            map[diceX][diceY] = dice[6];
        }
        // 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.
        else
        {
            dice[6= map[diceX][diceY];
            map[diceX][diceY] = 0;    
        }
        
        cout << dice[1<< "\n";
    }
    
    return 0;
}
cs

+ Recent posts