2022-04-26-21611-마법사상어와블리자드
본문 바로가기
알고리즘 모음집/New 알고리즘

2022-04-26-21611-마법사상어와블리자드

by KyeongMin 2022. 4. 26.
728x90
반응형

01.불마법

void magic(int dir, int S) {
    int y = (N + 1) / 2;
    int x = (N + 1) / 2;
    y--;
    x--;
    for (int s = 1; s <= S; s++) {
        Data n;
        n.y = y + (dy[dir] * s);
        n.x = x + (dx[dir] * s);
        board[n.y][n.x] = 0;
        //if (board[n.y][n.x] == 1) {
        //	one++;
        //	board[n.y][n.x] = 0;
        //}
        //else if (board[n.y][n.x] == 2) {
        //	two++;
        //	board[n.y][n.x] = 0;
        //}
        //else if (board[n.y][n.x] == 3) {
        //	three++;
        //	board[n.y][n.x] = 0;
        //}
    }
}
  • 이문제 실제 시험장에서는 전부 다 더했던것 같은데 이문제는 불마법 쓰는 부분은 계산에 포함하지 않음

02.0인부분 제외하고 백터에 저장

int num = 1;
int cnt = 2;
int y = (N - 1) / 2;
int x = (N - 1) / 2;
int dir = 0;
while (1) {
    if (N - 1 == num) cnt = 3;
    if (y == 0 && x == 0)break;
    for (int c = 0; c < cnt; c++) {
        for (int ni = 0; ni< num; ni++) {
            Data n;
            n.y = y + dy_s[dir];
            n.x = x + dx_s[dir];
            if (board[n.y][n.x] != 0) {
                O.push_back({ board[n.y][n.x] });
                board[n.y][n.x] = 0;
            }
            y = n.y;
            x = n.x;
        }
        dir = (dir + 1) % 4;

    }
    num++;

03.같은수 4개이상일때 제거

while (1) {
    int flag = 0;
    for (int i = 0; i < O.size(); i++) {
        if (O.size() == 0)break;
        int cnt = 0;
        for (int j = i; j < O.size(); j++) {
            while (O[i] == O[j]) {
                cnt++;
                j++;
                if (j == O.size())break;
            }
            if (cnt>= 4) {//제거
                flag = 1;
                if (O[i] == 1) one += cnt;
                if (O[i] == 2)two += cnt;
                if (O[i] == 3)three += cnt;
                O.erase(O.begin() + i, O.begin() + j);
                i--;
                break;
            }
            else break;
        }
    }
    if (flag == 0)break;
}

04.새로운 구슬 생성

for (int i = 0; i < O.size(); i++) {//새로운 구슬 생성
    if (O.size() == 0)break;
    int cnt = 0;
    for (int j = i; j < O.size(); j++) {
        while (O[i] == O[j]) {
            cnt++;
            j++;
            if (j == O.size())break;
        }
        newO.push_back(cnt);
        newO.push_back(O[i]);
        i = j;
        i--;
        //if (i + 1 == O.size() - 1) {
        //	newO.push_back(1);
        //	newO.push_back({ O[i + 1] });
        //}
        break;
    }
}

05.다시 배열에 리버스 달팽이 배열로 데이터 저장

void snailadd() {
	int num = 1;
	int cnt = 2;
	int y = (N - 1) / 2;
	int x = (N - 1) / 2;
	int dir = 0;
	while (1) {
		int flag = 0;
		if (N - 1 == num) cnt = 3;
		if (y == 0 && x == 0)break;
		for (int c = 0; c < cnt; c++) {
			for (int ni = 0; ni < num; ni++) {
				Data n;
				n.y = y + dy_s[dir];
				n.x = x + dx_s[dir];

				if (newO.size() == 0) {
					flag = 1;
					break;
				}
				board[n.y][n.x] = newO.front();
				newO.erase(newO.begin());
				y = n.y;
				x = n.x;
			}
			if (flag)break;
			dir = (dir + 1) % 4;

		}
		if (flag)break;
		num++;
	}
}

06.전체소스

#include<stdio.h>
#include<iostream>
#include<vector>
#define NS 51
using namespace std;
int N, M;
int board[NS][NS];
int ret;
vector<int>O;//구슬 저장
vector<int>newO;
int dy[] = { 0,-1,1,0,0 };
int dx[] = { 0,0,0,-1,1 };
int dy_s[] = {0,1,0,-1 };//당팽이 배열 dir
int dx_s[] = { -1,0,1,0 };
int one, two, three;
struct Data {
	int y, x;
};

void init() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			scanf("%d", &board[i][j]);
		}
	}
}
void snailPush() {
	O.clear();
	newO.clear();
	int num = 1;
	int cnt = 2;
	int y = (N - 1) / 2;
	int x = (N - 1) / 2;
	int dir = 0;
	while (1) {
		if (N - 1 == num) cnt = 3;
		if (y == 0 && x == 0)break;
		for (int c = 0; c < cnt; c++) {
			for (int ni = 0; ni< num; ni++) {
				Data n;
				n.y = y + dy_s[dir];
				n.x = x + dx_s[dir];
				if (board[n.y][n.x] != 0) {
					O.push_back({ board[n.y][n.x] });
					board[n.y][n.x] = 0;
				}
				y = n.y;
				x = n.x;
			}
			dir = (dir + 1) % 4;

		}
		num++;

	}
	while (1) {
		int flag = 0;
		for (int i = 0; i < O.size(); i++) {
			if (O.size() == 0)break;
			int cnt = 0;
			for (int j = i; j < O.size(); j++) {
				while (O[i] == O[j]) {
					cnt++;
					j++;
					if (j == O.size())break;
				}
				if (cnt>= 4) {//제거
					flag = 1;
					if (O[i] == 1) one += cnt;
					if (O[i] == 2)two += cnt;
					if (O[i] == 3)three += cnt;
					O.erase(O.begin() + i, O.begin() + j);
					i--;
					break;
				}
				else break;
			}
		}
		if (flag == 0)break;
	}
	for (int i = 0; i < O.size(); i++) {//새로운 구슬 생성
		if (O.size() == 0)break;
		int cnt = 0;
		for (int j = i; j < O.size(); j++) {
			while (O[i] == O[j]) {
				cnt++;
				j++;
				if (j == O.size())break;
			}
			newO.push_back(cnt);
			newO.push_back(O[i]);
			i = j;
			i--;
			//if (i + 1 == O.size() - 1) {
			//	newO.push_back(1);
			//	newO.push_back({ O[i + 1] });
			//}
			break;
		}
	}


}
void snailadd() {
	int num = 1;
	int cnt = 2;
	int y = (N - 1) / 2;
	int x = (N - 1) / 2;
	int dir = 0;
	while (1) {
		int flag = 0;
		if (N - 1 == num) cnt = 3;
		if (y == 0 && x == 0)break;
		for (int c = 0; c < cnt; c++) {
			for (int ni = 0; ni < num; ni++) {
				Data n;
				n.y = y + dy_s[dir];
				n.x = x + dx_s[dir];

				if (newO.size() == 0) {
					flag = 1;
					break;
				}
				board[n.y][n.x] = newO.front();
				newO.erase(newO.begin());
				y = n.y;
				x = n.x;
			}
			if (flag)break;
			dir = (dir + 1) % 4;

		}
		if (flag)break;
		num++;
	}


	
}
void magic(int dir, int S) {
	int y = (N + 1) / 2;
	int x = (N + 1) / 2;
	y--;
	x--;
	for (int s = 1; s <= S; s++) {
		Data n;
		n.y = y + (dy[dir] * s);
		n.x = x + (dx[dir] * s);
		board[n.y][n.x] = 0;
		//if (board[n.y][n.x] == 1) {
		//	one++;
		//	board[n.y][n.x] = 0;
		//}
		//else if (board[n.y][n.x] == 2) {
		//	two++;
		//	board[n.y][n.x] = 0;
		//}
		//else if (board[n.y][n.x] == 3) {
		//	three++;
		//	board[n.y][n.x] = 0;
		//}
	}
}
void play() {
	for (int m = 0; m < M; m++) {
		int dir, S;
		scanf("%d %d", &dir, &S);
		magic(dir, S);
		snailPush();
		snailadd();
	}
	ret = (1 * one) + (2 * two) + (3 * three);
}
int main(void) {
	init();
	play();
	snailPush();
	printf("%d\n", ret);
	return 0;
}

https://github.com/3DPIT/study/blob/master/02.studyData/10.Algorithm/2022/%EB%B0%B1%EC%A4%80%EC%BD%94%ED%85%8C/Algorithm/2022/04/0426/2022-04-26-21611-%EB%A7%88%EB%B2%95%EC%82%AC%EC%83%81%EC%96%B4%EC%99%80%EB%B8%94%EB%A6%AC%EC%9E%90%EB%93%9C.md

 

GitHub - 3DPIT/study

Contribute to 3DPIT/study development by creating an account on GitHub.

github.com

 

728x90
반응형

댓글