728x90
반응형
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<vector<int>>A;
int curr, curc;
int dr[4] = { 0,1,0,-1 };
int dc[4] = { -1,0,1,0 };
//격자 밖으로 나간 모래의 양
int ans = 0;
int N;
int dx[4][10] = { {-1,1,-2,2,-1,1,-1,1,0,0},{0,0,0,0,-1,-1,1,1,2,1},{-1,1,2,-2,1,-1,1,-1,0,0},{0,0,0,0,1,1,-1,-1,-2,-1} };
int dy[4][10] = { {0,0,0,0,1,1,-1,-1,-2,-1},{-1,1,-2,2,-1,1,-1,1,0,0} ,{0,0,0,0,-1,-1,1,1,2,1} ,{1,-1,2,-2,1,-1,-1,1,0,0} };
double degree[9] = { 0.07,0.07,0.02,0.02,0.01,0.01,0.1,0.1,0.05 }; //double!!!
void fly_away(int nr, int nc, int d) {
//nr,nc로 이동할것임
int total = A[nr][nc];//날아갈 모래의 양
A[nr][nc] = 0;
//d방향에 따라 사방으로 날리기
//a구하기 위한 날라간 양, 격자 밖으로 날라간 양
int tmp = 0; int out = 0;
//우선 9개 방향만 하기
for (int i = 0; i < 9; i++) {
int gr = nr + dx[d][i];
int gc = nc + dy[d][i];
int sub = (total * degree[i]);
tmp += sub;
//격자 밖이면 out++;
if (gr<1 || gr>N || gc<1 || gc>N) {
out += sub;
continue;
}
//격자 안이면 A에 더하기
A[gr][gc] += sub;
}
//a의 양
int a = (total - tmp);
int ar = nr + dx[d][9];
int ac = nc + dy[d][9];
if (ar<1 || ar>N || ac < 1 || ac>N){
out+=a;
}
else {
A[ar][ac]+=a;
}
ans += out;
}
int main() {
A.assign(505, vector<int>(505, 0));
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> A[i][j];
}
}
//초기 위치
curr = (N + 1) / 2; curc = (N + 1) / 2;
int cnt = 0;
int d = 0; //초기 방향
bool flag = false;
int k = 1;
while (1) {
cnt++;
//k=1,2,3,4...
for (int j = 0; j < 2; j++) {
for (int i = 0; i < k; i++) {
//다음 위치
int nr = curr + dr[d];
int nc = curc + dc[d];
//모래 흩날리기
fly_away(nr, nc, d);
if (nr == 1 && nc == 1) {
flag = true;
break;
}
curr = nr; curc = nc; //갱신하기
}
if (flag)break;
d = (d + 1) % 4;
}
if (flag)break;
cnt = 0;
k++;
}
cout << ans;
return 0;
}
https://www.acmicpc.net/problem/20057
20057번: 마법사 상어와 토네이도
마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을
www.acmicpc.net
double 배열 선언 조심하자..
728x90
반응형
'알고리즘 > 삼성 sw 역량테스트 기출문제' 카테고리의 다른 글
백준 마법상어와 비바라기(c++) (0) | 2023.04.07 |
---|---|
백준 14501번 퇴사(C++) (0) | 2023.03.19 |