20057번 마법사 상어와 토네이도

동도리동·2021년 10월 19일
0

코딩테스트

목록 보기
68/76

이것도 2시간 걸렸다.. 왤캐 문제를 이해하기가 어렵지...
똑바로 이해하고 코딩에 들어가자!!

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int map[510][510];
int n;
int dir = 0;

int x, y;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int ans = 0;
void morae(int x, int y) {
	int xx = x + dx[dir];
	int yy = y + dy[dir];
	int tmp = map[xx][yy];
	map[xx][yy] = 0;
	int sum = 0;
	int L = (dir + 1) % 4;
	int R = (dir + 3) % 4;
	pair<int, int> v[11];
	v[1].first = xx + dx[dir]; v[1].second = yy + dy[dir];
	v[2].first = v[1].first + dx[dir]; v[2].second = v[1].second + dy[dir];
	v[3].first = xx + dx[L]; v[3].second = yy + dy[L];
	v[4].first = v[3].first + dx[L]; v[4].second = v[3].second + dy[L];
	v[5].first = xx + dx[R]; v[5].second = yy + dy[R];
	v[6].first = v[5].first + dx[R]; v[6].second = v[5].second + dy[R];
	v[7].first = v[1].first + dx[L]; v[7].second = v[1].second + dy[L];
	v[8].first = x + dx[L]; v[8].second = y + dy[L];
	v[9].first = v[1].first + dx[R]; v[9].second = v[1].second + dy[R];
	v[10].first = x + dx[R]; v[10].second = y + dy[R];
	int per[11] = { 0,0,5,7,2,7,2,10,1,10,1 };
	for (int i = 10; i >= 2; i--) {
		sum += tmp * per[i] / 100;
		if (v[i].first > n || v[i].first <= 0 || v[i].second > n || v[i].second <= 0) continue;
		map[v[i].first][v[i].second] += tmp * per[i] / 100;
		
	}
	map[v[1].first][v[1].second] += tmp - sum;
	

}
int main() {
	//freopen("in1.txt", "rt", stdin);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> map[i][j];
			ans += map[i][j];
		}
	}
	x = y = 1 + n / 2;
	int cnt = 0;
	int line = 1; //직선하는 횟수
	int tmpline = line;
	while (!(x == 1 && y == 1)) {
		//모래 처리
		morae(x,y);

		//이동
		if (tmpline != 0) { //아직 직선횟수가 남았다.
			tmpline--;
		}
		x = x + dx[dir];
		y = y + dy[dir];
		if (tmpline == 0) { //담번부터 방향을 바꾸세용
			cnt++;
			dir = (dir + 1) % 4;
			tmpline = line;
		}

		if (cnt == 2) {//방향도 두번째예용, 담번부터는 직선+1
			line++;
			cnt = 0;
			tmpline = line;
		}
		
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			ans -= map[i][j];
		}
	}
	cout << ans << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보