14499 Samsung sw test
์๊ณ ๋ฆฌ์ฆ
1. ๊ฐ ๋ช
๋ น๋ณ๋ก ๊ฒ์ฌ. ์ด๊ธฐ ์ฃผ์ฌ์ ์ํ (top : 1, north : 2, east : 3, south : 5, western : 4, bottom : 6)
2. ๋ช
๋ น๋๋ก ์ฃผ์ฌ์๋ฅผ ๊ตด๋ฆฌ๊ณ -> ๊ทธ ์์น๊ฐ ๋งต ๋ฐ์ด๋ฉด ๋ค์ ๋ช
๋ น ์คํ
๋งต ์์ด๋ฉด ์ด๋์ํค๊ณ , ์ฃผ์ฌ์ ์ํ ๋ณ๊ฒฝ -> ์ดํ ์ด๋ํ ์นธ์ ์ซ์ ์ ์ฉํ๊ณ -> ์๋จ ํ๋ฆฐํธ -> ๋ค์ ๋ช
๋ น ์คํ
์ฝ๋
#include <iostream>
#include <cstring>
#define MAX_WIDTH 20
#define MAX_COMMAND 1000
using namespace std;
typedef struct Location {
int row;
int col;
} Location;
typedef struct Dice {
int top;
int north;
int east;
int south;
int western;
int bottom;
}Dice;
int width;
int height;
int map[MAX_WIDTH][MAX_WIDTH];
Location locationDice;
int numOfCommand;
int command[MAX_COMMAND];
int dice[7];
void getProperties();
Dice diceChange(Dice d, int direction);
bool diceMove(int command);
void game();
int main() {
getProperties();
game();
}
void getProperties() {
cin >> height >> width >> locationDice.row >> locationDice.col >> numOfCommand;
for (int i = 0; i < MAX_WIDTH; i++)
memset(map, 0, sizeof(int) * MAX_WIDTH);
memset(dice, 0, sizeof(int) * 7);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
cin >> map[i][j];
}
for (int i = 0; i < numOfCommand; i++)
cin >> command[i];
}
void game() {
Dice d = { 1, 2, 3, 5, 4, 6 };
for (int i = 0; i < numOfCommand; i++) {
if (diceMove(command[i])) {
d = diceChange(d, command[i]);
if (map[locationDice.row][locationDice.col] == 0)
map[locationDice.row][locationDice.col] = dice[d.bottom];
else {
dice[d.bottom] = map[locationDice.row][locationDice.col];
map[locationDice.row][locationDice.col] = 0;
}
cout << dice[d.top] << endl;
}
}
}
bool diceMove(int command) {
//temp๋ฅผ ๋จผ์ ์ฎ๊ธฐ๊ณ , ๋ฒ์ ๋ฐ์ด๋ฉด false๋ฆฌํด, ์๋๋ฉด ์์ ํ true๋ฆฌํด
Location temp = { locationDice.row, locationDice.col };
if (command == 1)
temp.col++;
else if (command == 2)
temp.col--;
else if (command == 3)
temp.row--;
else if (command == 4)
temp.row++;
if (temp.col < 0 || temp.col >= width || temp.row < 0 || temp.row >= height)
return false;
locationDice.col = temp.col;
locationDice.row = temp.row;
return true;
}
Dice diceChange(Dice d, int direction) {
//1 : ๋, 2: ์, 3: ๋ถ, 4 : ๋จ
Dice temp;
if (direction == 1) {
temp.top = d.western;
temp.north = d.north;
temp.east = d.top;
temp.south = d.south;
temp.western = d.bottom;
temp.bottom = d.east;
}
else if (direction == 2) {
temp.top = d.east;
temp.north = d.north;
temp.east = d.bottom;
temp.south = d.south;
temp.western = d.top;
temp.bottom = d.western;
}
else if (direction == 3) {
temp.top = d.south;
temp.north = d.top;
temp.east = d.east;
temp.south = d.bottom;
temp.western = d.western;
temp.bottom = d.north;
}
else if (direction == 4) {
temp.top = d.north;
temp.north = d.bottom;
temp.east = d.east;
temp.south = d.top;
temp.western = d.western;
temp.bottom = d.south;
}
return temp;
}