마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을 의미한다.
토네이도를 시전하면 격자의 가운데 칸부터 토네이도의 이동이 시작된다. 토네이도는 한 번에 한 칸 이동한다. 다음은 N = 7인 경우 토네이도의 이동이다.

토네이도가 한 칸 이동할 때마다 모래는 다음과 같이 일정한 비율로 흩날리게 된다.

토네이도가 x에서 y로 이동하면, y의 모든 모래가 비율과 α가 적혀있는 칸으로 이동한다. 비율이 적혀있는 칸으로 이동하는 모래의 양은 y에 있는 모래의 해당 비율만큼이고, 계산에서 소수점 아래는 버린다. α로 이동하는 모래의 양은 비율이 적혀있는 칸으로 이동하지 않은 남은 모래의 양과 같다. 모래가 이미 있는 칸으로 모래가 이동하면, 모래의 양은 더해진다. 위의 그림은 토네이도가 왼쪽으로 이동할 때이고, 다른 방향으로 이동하는 경우는 위의 그림을 해당 방향으로 회전하면 된다.
토네이도는 (1, 1)까지 이동한 뒤 소멸한다. 모래가 격자의 밖으로 이동할 수도 있다. 토네이도가 소멸되었을 때, 격자의 밖으로 나간 모래의 양을 구해보자.
입력
첫째 줄에 격자의 크기 N이 주어진다. 둘째 줄부터 N개의 줄에는 격자의 각 칸에 있는 모래가 주어진다. r번째 줄에서 c번째 주어지는 정수는 A[r][c] 이다.
출력
격자의 밖으로 나간 모래의 양을 출력한다.
제한
- 3 ≤ N ≤ 499
- N은 홀수
- 0 ≤ A[r][c] ≤ 1,000
- 가운데 칸에 있는 모래의 양은 0
#include <iostream>
#include <vector>
using namespace std;
int map[500][500];
int number_map[500][500];
int numbuer_dir[4][2] ={ {0,-1} ,{1,0}, {0,1}, {-1,0}};
int N , ans = 0;
vector<pair<int , pair <int,int > > > number;
vector<pair<int, pair <int,int > > > send_moves[4];
void make_send(){
send_moves[0].push_back( { 2,{-2 ,0 }});
send_moves[0].push_back( { 10,{-1,-1}});
send_moves[0].push_back( { 7,{ -1,0 }});
send_moves[0].push_back( { 1,{ -1,1 }});
send_moves[0].push_back( { 5,{ 0,-2 }});
send_moves[0].push_back( { 10,{ 1,-1 }});
send_moves[0].push_back( { 7,{ 1,0 }});
send_moves[0].push_back( { 1,{ 1,1 }});
send_moves[0].push_back( { 2,{ 2,0 }});
send_moves[1].push_back( { 1,{ -1,-1 }});
send_moves[1].push_back( { 1,{ -1,1 }});
send_moves[1].push_back( { 2,{ 0,-2 }});
send_moves[1].push_back( { 7,{ 0,-1 }});
send_moves[1].push_back( { 7,{ 0,1 }});
send_moves[1].push_back( { 2,{ 0,2 }});
send_moves[1].push_back( { 10,{ 1, -1}});
send_moves[1].push_back( { 10,{ 1, 1}});
send_moves[1].push_back( { 5,{ 2,0 }});
send_moves[2].push_back( { 2,{ -2,0 }});
send_moves[2].push_back( { 1,{ -1,-1 }});
send_moves[2].push_back( { 7,{ -1,0 }});
send_moves[2].push_back( { 10,{ -1,1 }});
send_moves[2].push_back( { 5,{ 0,2 }});
send_moves[2].push_back( { 1,{ 1,-1 }});
send_moves[2].push_back( { 7,{ 1,0 }});
send_moves[2].push_back( { 10,{ 1, 1}});
send_moves[2].push_back( { 2,{ 2,0 }});
send_moves[3].push_back( { 5,{ -2,0}});
send_moves[3].push_back( { 10,{-1,-1}});
send_moves[3].push_back( { 10,{-1,1}});
send_moves[3].push_back( { 2,{0,-2}});
send_moves[3].push_back( { 7,{0,-1}});
send_moves[3].push_back( { 7,{0,1 }});
send_moves[3].push_back( { 2,{0,2 }});
send_moves[3].push_back( { 1,{1, -1}});
send_moves[3].push_back( { 1,{1, 1}});
}
void numbering(){
int curDir = 0 , num = 0;
int move_cnt = 0;
int max_move_cnt = 1 ;
int wave_cnt = 0;
int max_wave_cnt = 2;
int curY = N/2;
int curX = N/2;
number.push_back({0,{curY,curX}});
while (true){
curY += numbuer_dir[curDir][0];
curX += numbuer_dir[curDir][1];
if(curY < 0 || curY >= N || curX < 0 || curX >= N) break;
num++;
move_cnt++;
number_map[curY][curX] = num;
number.push_back({ curDir, {curY, curX}});
if(move_cnt == max_move_cnt){
curDir++;
wave_cnt++;
move_cnt = 0;
if(curDir == 4) curDir = 0;
if(wave_cnt == max_wave_cnt){
wave_cnt = 0;
max_move_cnt++;
}
}
}
}
void move(){
for(int num = 0 ; num < number.size() ; num++){
int curY = number[num].second.first;
int curX = number[num].second.second;
int curS = map[curY][curX];
int alpa = map[curY][curX];
int curDir = number[num].first;
for(int k = 0 ; k < 9 ;k++){
int nY = curY + send_moves[curDir][k].second.first;
int nX = curX + send_moves[curDir][k].second.second;
int nS = curS * send_moves[curDir][k].first / 100;
alpa -= nS;
if(nY < 0 || nY >= N || nX < 0 || nX >= N){
ans += nS;
}else{
map[nY][nX] += nS;
}
}
int nY = curY + numbuer_dir[curDir][0];
int nX = curX + numbuer_dir[curDir][1];
if(nY < 0 || nY >= N || nX < 0 || nX >= N){
ans += alpa;
}else{
map[nY][nX] += alpa;
}
map[curY][curX] = 0;
}
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 0; i < N;i++){
for(int j = 0; j < N ;j++){
cin >> map[i][j];
}
}
make_send();
numbering();
move();
cout << ans ;
}
'Algorithm > S사 코딩테스트' 카테고리의 다른 글
백준 상어 중학교 (0) | 2021.06.23 |
---|---|
백준 마법사상어와 비바라기 (0) | 2021.06.18 |
백준 스타트택시 (0) | 2021.03.23 |
백준 마법사 상어와 파이어스톰 (0) | 2021.03.23 |
49. SW_Test 연습 백준 어른상어 (0) | 2021.02.28 |
댓글