본문 바로가기
백준 알고리즘/구현

마법사 상어와 토네이도

by paysmile 2021. 2. 22.
#include <iostream>
#include <vector>

using namespace std;

int n, dir, answer=0;
const int MAX = 501;
int A[MAX][MAX];
struct MOVE { int x, y, wind; };
pair<int, int> cur_loc;

MOVE mv_up[10] = { {0,2,2}, {0,1,7}, {-1,1,10}, {1,1,1}, {-2,0,5}, {-1,-1,10}, {0,-1,7}, {1,-1,1}, {0,-2,2}, {-1,0,0} };
MOVE mv_down[10] = { {-1,-1,1}, {-1,1,1}, {0,-2,2}, {0,-1,7}, {0,1,7}, {0,2,2}, {1,-1,10}, {1,1,10}, {2,0,5}, {1,0,0} };
MOVE mv_right[10] = { {-2,0,2}, {-1,-1,1}, {-1,0,7}, {-1,1,10}, {0,2,5}, {1,-1,1}, {1,0,7}, {1,1,10}, {2,0,2}, {0,1,0} };
MOVE mv_left[10] = { {-1,1,1}, {-1,0,7}, {-1,-1,10}, {-2,0,2}, {0,-2,5}, {1,-1,10}, {1,0,7}, {1,1,1}, {2,0,2}, {0,-1,0} };
MOVE mv_wind[4] = { {0,-1,0}, {1,0,0}, {0,1,0}, {-1,0,0} };

void Moveleft(int d) {
	int movei, movej, wind_m, count;
	count = 0;

	for (int i = 0; i < 10; i++) {
		movei = cur_loc.first + mv_left[i].x;
		movej = cur_loc.second + mv_left[i].y;
		wind_m = A[cur_loc.first][cur_loc.second] * mv_left[i].wind / 100;
		count += wind_m;

		if (i == 9) {
			A[movei][movej] += (A[cur_loc.first][cur_loc.second] - count);
			if (A[movei][movej] < 0)
				A[movei][movej] = 0;
		}
		if (movei <= 0 || movej <= 0 || movei > n || movej > n) {
			if(i==9)
				answer += A[movei][movej];
			else
				answer += wind_m;
		}
		else
			A[movei][movej] += wind_m;

	}
	A[cur_loc.first][cur_loc.second] = 0;
	
}

void MoveRight(int d) {
	int movei, movej, wind_m, count;
	count = 0;

	for (int i = 0; i < 10; i++) {
		movei = cur_loc.first + mv_right[i].x;
		movej = cur_loc.second + mv_right[i].y;
		wind_m = A[cur_loc.first][cur_loc.second] * mv_right[i].wind / 100;
		count += wind_m;

		if (i == 9) {
			A[movei][movej] += (A[cur_loc.first][cur_loc.second] - count);
			if (A[movei][movej] < 0)
				A[movei][movej] = 0;
		}
		if (movei <= 0 || movej <= 0 || movei > n || movej > n) {
			if (i == 9)
				answer += A[movei][movej];
			else
				answer += wind_m;
		}
		else
			A[movei][movej] += wind_m;
	}
	A[cur_loc.first][cur_loc.second] = 0;
}

void MoveUp(int d) {
	int movei, movej, wind_m, count;
	count = 0;

	for (int i = 0; i < 10; i++) {
		movei = cur_loc.first + mv_up[i].x;
		movej = cur_loc.second + mv_up[i].y;
		wind_m = A[cur_loc.first][cur_loc.second] * mv_up[i].wind / 100;
		count += wind_m;

		if (i == 9) {
			A[movei][movej] += (A[cur_loc.first][cur_loc.second] - count);
			if (A[movei][movej] < 0)
				A[movei][movej] = 0;
		}
		if (movei <= 0 || movej <= 0 || movei > n || movej > n) {
			if (i == 9)
				answer += A[movei][movej];
			else
				answer += wind_m;
		}
		else
			A[movei][movej] += wind_m;
	}
	A[cur_loc.first][cur_loc.second] = 0;
}

void MoveDown(int d) {
	int movei, movej, wind_m, count;
	count = 0;

	for (int i = 0; i < 10; i++) {
		movei = cur_loc.first + mv_down[i].x;
		movej = cur_loc.second + mv_down[i].y;
		wind_m = A[cur_loc.first][cur_loc.second] * mv_down[i].wind / 100;
		count += wind_m;

		if (i == 9) {
			A[movei][movej] += (A[cur_loc.first][cur_loc.second] - count);
			if (A[movei][movej] < 0)
				A[movei][movej] = 0;
		}
		if (movei <= 0 || movej <= 0 || movei > n || movej > n) {
			if (i == 9)
				answer += A[movei][movej];
			else
				answer += wind_m;
		}
		else
			A[movei][movej] += wind_m;
	}
	A[cur_loc.first][cur_loc.second] = 0;
}

void MoveSand(int d) {

	switch (d) {
	case 0:
		Moveleft(d);
		break;
	case 1:
		MoveDown(d);
		break;
	case 2:
		MoveRight(d);
		break;
	case 3:
		MoveUp(d);
		break;
	}
}

void MoveWind(int count,int d) {
	int movei, movej;

	for (int i = 0; i < count; i++) {
		movei = cur_loc.first + mv_wind[d].x;
		movej = cur_loc.second + mv_wind[d].y;
		cur_loc = make_pair(movei, movej);

		MoveSand(d);
	}
	
}

int main(void) {
	cin >> n;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> A[i][j];
		}
	}

	cur_loc = make_pair(n/2 + 1, n/2 + 1);
	dir = 0;  // 0:왼, 1:아래, 2:오, 3:위
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < 2; j++) {
			MoveWind(i,dir);
			if (dir == 3)
				dir = 0;
			else
				dir++;
		}
		if (i == n - 1) {
			MoveWind(i, 0);
		}
	}

	cout << answer;
	return 0;
}

'백준 알고리즘 > 구현' 카테고리의 다른 글

어른 상어 C++  (0) 2021.03.04
컨베이어 벨트 위의 로봇 C++  (0) 2021.03.01
마법사 상어와 파이어볼 C++  (0) 2021.02.18
백준 2960번 C++  (0) 2019.02.18
백준 5565번 C++  (0) 2019.02.18