๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][์‹œ๋ฎฌ๋ ˆ์ด์…˜] 17144๋ฒˆ ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

ibelieveinme 2021. 9. 19. 14:42
728x90

๋ฌธ์ œ

 

17144๋ฒˆ: ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

๋ฏธ์„ธ๋จผ์ง€๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ์ง‘์„ ํฌ๊ธฐ๊ฐ€ R×C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๊ณ , 1×1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆด๋‹ค. ๊ตฌ์‚ฌ

www.acmicpc.net

 

ํ’€์ด

  1. ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ํผ์ง€๊ณ  ๋‚˜์„œ ๊ธฐ์กด ๋งต์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด๋‘๋ฉด ๋‹ค์Œ ๋ฏธ์„ธ๋จผ์ง€์˜ ํ™•์‚ฐ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋ฏ€๋กœ int cal_map[1001][1001] = { {0,0}, }; ์ด๋ผ๋Š” ์ƒˆ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ๋ณ€์ˆ˜์— ๊ฐ’์„ ๊ณ„์† ๋”ํ•œ ํ›„, ๋งˆ์ง€๋ง‰์— ๋”ํ•˜๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  2. ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ์ˆœํ™˜๋ถ€๋ถ„์€ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์œ„์•„๋ž˜๋ฅผ 0์œผ๋กœ ํ‘œ์‹œํ•ด๋‘๊ณ  ์œ„์ชฝ์€ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ, ์•„๋ž˜์ชฝ์€ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ์”ฉ ๋Œ๋ ธ๋‹ค. for ๋ฌธ 8๊ฐœ๋กœ ;;;;;
  3. ๋งˆ์ง€๋ง‰์— ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ map์—์„œ -1์ด๋‚˜ 0์ด ์•„๋‹Œ ๊ฐ’๋“ค(๋ฏธ์„ธ๋จผ์ง€๋“ค)์„ ๋‹ค ๋”ํ•ด์ค˜์„œ ์ถœ๋ ฅํ•ด์คฌ๋‹ค.

 

์ฐธ๊ณ 

ํ˜น์‹œ๋‚˜ Visual Studio์˜ 0xC00000FD: Stack overflow ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, visual studio ์Šคํƒ ํฌ๊ธฐ๊ฐ€ 1M๋ฅผ ๋„˜์–ด์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์ด๋‹ค. Stack ์˜ˆ์•ฝ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ํ•ด๊ฒฐ ๋œ๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€ ์ƒ๋‹จ์˜ [ํ”„๋กœ์ ํŠธ]-[ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ์†์„ฑ] ํด๋ฆญ. ๋ง์ปค>์‹œ์Šคํ…œ>์Šคํƒ์˜ˆ์•ฝํฌ๊ธฐ์— 10485760 ์ž…๋ ฅํ•ด์ฃผ๊ธฐ.

(10M๋กœ ๋Š˜๋ ค์ค€ ๊ฒƒ. 1024*1024*1=1MB ์ด๋ฏ€๋กœ 1024*1024*10=10485760 ์„ ์ž…๋ ฅํ•œ ๊ฒƒ)

 

 

์ฝ”๋“œ

#include <iostream>
#include <vector>
using namespace std;

int map[1001][1001];
int row, col, time;
int robot_location;
int direction[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//์ƒํ•˜์ขŒ์šฐ

void Spray();
void Machine();
int PrintGrayNum();

int main() {
	cin >> row >> col >> time;

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cin >> map[i][j];
			if (map[i][j] == -1) {
				robot_location = i;
			}
		}
	}
	for (int i = 0; i < time; i++) {
		Spray();
		Machine();
	}
	cout << PrintGrayNum();
}

void Spray() {
	int cal_map[1001][1001] = { {0,0}, };
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (map[i][j] != 0 && map[i][j] != -1) {
				int count = 0;
				for (int d = 0; d < 4; d++) {
					int next_x = i + direction[d][0];
					int next_y = j + direction[d][1];
					if (next_x >= 0 && next_x < row && next_y >= 0 && next_y < col && map[next_x][next_y] != -1) {
						cal_map[next_x][next_y] += map[i][j] / 5;
						count++;
					}
				}
				cal_map[i][j] += (map[i][j] - (map[i][j] / 5) * count);
			}

		}
	}
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (cal_map[i][j] != 0 && cal_map[i][j] != -1) {
				map[i][j] = cal_map[i][j];
			}
		}
	}
}

void Machine() {
	//๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์œ„์•„๋ž˜๋ฅผ 0์œผ๋กœ ํ‘œ์‹œํ•ด๋‘๊ณ  ์œ„์ชฝ์€ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ, ์•„๋ž˜์ชฝ์€ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ๋Œ๋ฆฌ๊ธฐ
	map[robot_location - 2][0] = 0;
	map[robot_location + 1][0] = 0;

	//์œ„์ชฝ ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•˜๋Š” ๋ถ€๋ถ„
	for (int i = robot_location - 2; i > 0; i--) {
		int temp = map[i][0];
		map[i][0] = map[i - 1][0];
		map[i - 1][0] = temp;
	}
	for (int j = 0; j < col - 1; j++) {
		int temp = map[0][j];
		map[0][j] = map[0][j + 1];
		map[0][j + 1] = temp;
	}
	for (int i = 0; i < robot_location - 1; i++) {
		int temp = map[i][col - 1];
		map[i][col - 1] = map[i + 1][col - 1];
		map[i + 1][col - 1] = temp;
	}
	for (int j = col - 1; j > 1; j--) {
		int temp = map[robot_location - 1][j];
		map[robot_location - 1][j] = map[robot_location - 1][j - 1];
		map[robot_location - 1][j - 1] = temp;
	}

	//์•„๋ž˜์ชฝ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•˜๋Š” ๋ถ€๋ถ„
	for (int i = robot_location + 1; i < row - 1; i++) {
		int temp = map[i][0];
		map[i][0] = map[i + 1][0];
		map[i + 1][0] = temp;
	}
	for (int j = 0; j < col - 1; j++) {
		if (j + 1 < col) {
			int temp = map[row - 1][j];
			map[row - 1][j] = map[row - 1][j + 1];
			map[row - 1][j + 1] = temp;
		}
	}
	for (int i = row - 1; i > robot_location; i--) {
		int temp = map[i][col - 1];
		map[i][col - 1] = map[i - 1][col - 1];
		map[i - 1][col - 1] = temp;
	}
	for (int j = col - 1; j > 1; j--) {
		int temp = map[robot_location][j];
		map[robot_location][j] = map[robot_location][j - 1];
		map[robot_location][j - 1] = temp;
	}
}

int PrintGrayNum() {
	int gray_num = 0;
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (map[i][j] != -1 && map[i][j] != 0) {
				gray_num += map[i][j];
			}
		}
	}
	return gray_num;
}
728x90