728x90
๋ฌธ์
ํ์ด
- ๋ฏธ์ธ๋จผ์ง๊ฐ ํผ์ง๊ณ ๋์ ๊ธฐ์กด ๋งต์ ๊ฐ์ ๊ฐฑ์ ํด๋๋ฉด ๋ค์ ๋ฏธ์ธ๋จผ์ง์ ํ์ฐ์ ์ํฅ์ ๋ฏธ์น๋ฏ๋ก int cal_map[1001][1001] = { {0,0}, }; ์ด๋ผ๋ ์ ๋ณ์๋ฅผ ๋ง๋ค์ด์ ๊ทธ ๋ณ์์ ๊ฐ์ ๊ณ์ ๋ํ ํ, ๋ง์ง๋ง์ ๋ํ๋ ์์ผ๋ก ๊ตฌํํ๋ค.
- ๊ณต๊ธฐ์ฒญ์ ๊ธฐ์ ์ํ๋ถ๋ถ์ ๊ณต๊ธฐ์ฒญ์ ๊ธฐ ์์๋๋ฅผ 0์ผ๋ก ํ์ํด๋๊ณ ์์ชฝ์ ๋ฐ์๊ณ๋ฐฉํฅ, ์๋์ชฝ์ ์๊ณ๋ฐฉํฅ์ผ๋ก ํ ์นธ์ฉ ๋๋ ธ๋ค. for ๋ฌธ 8๊ฐ๋ก ;;;;;
- ๋ง์ง๋ง์ ๋ฏธ์ธ๋จผ์ง์ ์์ ๊ณ์ฐํ๋ ๊ฒ์ด๋ฏ๋ก 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
'๐ Coding Test Study > Algorithm Problem' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++][BAEKJOON][๋ธ๋ฃจํธํฌ์ค] 3085๋ฒ ์ฌํ ๊ฒ์ (0) | 2021.09.21 |
---|---|
[C++][BEAKJOON][DP] ๋์ 1 (0) | 2021.09.21 |
[C++][BAEKJOON][DP] 9456๋ฒ ์คํฐ์ปค (0) | 2021.08.22 |
[C++][ํ๋ก๊ทธ๋๋จธ์ค] ๊ดํธ ๋ณํ (0) | 2021.08.22 |
[C++][Baekjoon] 14503๋ฒ ๋ก๋ด ์ฒญ์๊ธฐ (0) | 2021.08.22 |