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

마법사 상어와 파이어볼 C++

by paysmile 2021. 2. 18.
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct fireball { int x, y, mass, s, d; };
vector <fireball> map[51][51];
struct Move { int x, y; };
Move mv[8] = { { -1,0 },{ -1,1 },{ 0,1 },{ 1,1 },{ 1,0 },{ 1,-1 },{ 0,-1 },{ -1,-1 } };
vector <fireball> fb;


int dx[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1 };


int n, M, k;

void Move_fireball() {

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			map[i][j].clear();
		}
	}

	int size = fb.size();

	for (int i = 0; i < size; i++) {
		//fireball cur = fb[i];
		//int cx = cur.x, cy = cur.y;
		//int movei = cx + dx[cur.d] * cur.s, movej = cy + dy[cur.d] * cur.s;

		int movei, movej;
		movei = fb[i].x + (mv[fb[i].d].x * fb[i].s);
		movej = fb[i].y + (mv[fb[i].d].y * fb[i].s);

		while (movei < 1) movei += n;
		while (movej < 1) movej += n;
		while (movei > n) movei -= n;
		while (movej > n) movej -= n;

		map[movei][movej].push_back({movei,movej,fb[i].mass,fb[i].s,fb[i].d });
	}
	return;
}

bool isDir(int x, int y) { // 방향이 모두 홀수인지, 모두 짝수인지 판별하는 함수
	bool evenFlag = false, oddFlag = false;
	for (int i = 0; i<map[x][y].size(); i++) {
		if (map[x][y][i].d % 2) oddFlag = true;
		else evenFlag = true;
	}
	if (evenFlag && !oddFlag) return true;
	else if (!evenFlag && oddFlag) return true;
	else return false;
}

void Check_SL() {
	vector <fireball> temp;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (map[i][j].size() == 0) continue;
			else if (map[i][j].size() == 1) temp.push_back(map[i][j][0]);
			else {
				int mcount=0, scount=0, d;
				d = map[i][j][0].d % 2;
				bool flag = true;
				for (int k = 0; k < map[i][j].size(); k++) {
					//if (map[i][j][k].m == 0)
						//continue;
					mcount += map[i][j][k].mass;
					scount += map[i][j][k].s;
					if ((map[i][j][k].d % 2) != d)
						flag = false;
				}

				int mm = mcount / 5;
				int s = scount / map[i][j].size();
				if (mm == 0) continue;
				if (flag) {
					temp.push_back({ i,j,mm,s,0 });
					temp.push_back({ i,j,mm,s,2 });
					temp.push_back({ i,j,mm,s,4 });
					temp.push_back({ i,j,mm,s,6 });
				}
				else {
					temp.push_back({ i,j,mm,s,1 });
					temp.push_back({ i,j,mm,s,3 });
					temp.push_back({ i,j,mm,s,5 });
					temp.push_back({ i,j,mm,s,7 });
					}
				}
			}
		}
	fb = temp;
}

int main(void) {
	int answer = 0;
	cin >> n >> M >> k;

	for (int j = 0; j < M; j++) {
		int r, c, mass, s, d;
		cin >> r >> c >> mass >> s >> d;
		fb.push_back({r,c,mass,s,d});
	}

	for (int i = 0; i < k; i++) {
		Move_fireball();
		Check_SL();
	}

	for (int i = 0; i < fb.size(); i++) {
		answer += fb[i].mass;
	}
	cout << answer;
	return 0;
}

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

컨베이어 벨트 위의 로봇 C++  (0) 2021.03.01
마법사 상어와 토네이도  (0) 2021.02.22
백준 2960번 C++  (0) 2019.02.18
백준 5565번 C++  (0) 2019.02.18
백준 5543번 C++  (0) 2019.02.18