21611번 마법사 상어와 블리자드

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

코딩테스트

목록 보기
73/76

주의!! 75%에서 틀린 문제이다. 나중에 다시 풀기!

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

using namespace std;
int setting[51][51];
int map[3000];
int nmap[3000];
int n, m, nn;
int d, s;
int dxx[4] = { 0,1,0,-1 };
int dyy[4] = { -1,0,1,0 };
int dx[5] = { 0,-1,1,0,0  };
int dy[5] = { 0,0,0,-1,1 };
int ans = 0;
void change() {
	//for (int i = 1; i <= nn; i++) nmap[i] = 0;
	int pos1 = 1;
	int cnt = 1;
	for (int i = 1; i <= nn; i+=cnt) {
		if (map[i] == 0) break;
		cnt = 1;
		int same = map[i];
		for (int j = i + 1; j <= i+4; j++) {
			if (map[j] == same) {
				cnt++;
			}
			else {
				nmap[pos1++] = cnt;
				nmap[pos1++] = same;
				break;
			}
		}
		if (pos1 > nn+1) break;
	}
	for (int i = 1; i <= nn; i++) {
		map[i] = nmap[i];
		nmap[i] = 0;
	}
	map[nn + 1] = 0;
}
void go() {
	while (1) {
		bool ok1 = true;
		//일단 빈칸없게 정렬
		while (1) {
			bool ok2 = true;
			for (int i = 1; i < nn; i++) {
				if (map[i] == 0 && map[i + 1] != 0) {
					map[i] = map[i + 1];
					map[i + 1] = 0;
					ok1 = false;
					ok2 = false;
				}
			}
			if (ok2) break;
		}
		int cnt = 1;
		for (int i = 1; i < nn; i+=cnt) {
			if (map[i] == 0) continue;
			cnt = 1;
			int same = map[i];
			for (int j = i + 1; j <= nn; j++) {
				if (map[j] == same) {
					cnt++;
				}
				else {
					if (cnt < 4) break;
					for (int k = i; k < j; k++) {
						map[k] = 0;
						ok1 = false;
					}
					if (same == 1) ans += 1 * cnt;
					else if (same == 2) ans += 2 * cnt;
					else if (same == 3) ans += 3 * cnt;
					break;
				}
			}
		}
		if (ok1) break;
	}
	map[nn + 1] = 0;
}

void magic() {
	int tmp = 0;
	if (d == 1) tmp = 7;
	else if (d == 2) tmp = 3;
	else if (d == 3) tmp = 1;
	else if (d == 4) tmp = 5;
	for (int i = 1; i <= s; i++) {
		if (d == 1) {
			map[tmp] = 0;
			tmp = tmp + 7 + 8 * i;
		}
		else if (d == 2) {
			map[tmp] = 0;
			tmp = tmp + 3 + 8 * i;
		}
		else if (d == 3) {
			map[tmp] = 0;
			tmp = tmp + 1 + 8 * i;
		}
		else {
			map[tmp] = 0;
			tmp = tmp + 5 + 8 * i;
		}
	}
}
int main() {
	//freopen("in1.txt", "rt", stdin);
	cin >> n >> m;
	nn = n * n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> setting[i][j];
		}
	}
	int x1=0, y1=0;
	x1 = (n+1) / 2;
	y1 = (n+1) / 2;
	int cnt = 0;
	int line = 1;
	int tmpline = line;
	int dir2 = 0;
	int pos1 = 1;
	while (!(x1 == 1 && y1 == 1)) {
		if (tmpline !=0) {
			tmpline--;
		}
		x1 = x1 + dxx[dir2];
		y1 = y1 + dyy[dir2];
		map[pos1++] = setting[x1][y1];
		if (tmpline == 0) {
			tmpline = line;
			cnt++;
			dir2 = (dir2 + 1) % 4;
		}
		if (cnt == 2) {
			line++;
			cnt = 0;
			tmpline = line;
		}
	}
	//map[pos1++] = setting[x1][y1];
	for (int i = 0; i < m; i++) {
		cin >> d >> s;
		magic();
		go();
		change();
	}
	cout << ans << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보