• Home
  • About
    • Seokmin.Lee photo

      Seokmin.Lee

      Hello, I am a master's student in the Department of Convergence Security (Samsung Advanced Security) at Korea University.After graduation, I am expected as a security developer or researcher member of Samsung SDS.

    • Learn More
    • LinkedIn
    • Github
  • Posts
    • All Tags

[til][bj17144]미세먼지안녕

18 Jan 2025

Problem :

  • 제목: 미세먼지 안녕!
  • 링크 : https://www.acmicpc.net/problem/17144
  • 접근 방법 : 깡구현

Reference :

  • https://blockdmask.tistory.com/494
  • https://zoosso.tistory.com/836
  • https://ansohxxn.github.io/cpp/freopen/
  • https://developer-cat.tistory.com/17

Problem-solving method:

Try 1 : Success

  • 문제 자체는 쉬웠음(하라는데로 깡구현 하면되더라)
  • 효율성 문젠데.. 어케하지
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
using namespace std;
int r, c, T; 

int dr[4] = { 1,-1,0,0 };
int dc[4] = { 0,0,1,-1 };

int main(void) {

	cin.tie(0);
	cout.tie(0);
	iostream::sync_with_stdio(0);
	
	//freopen("텍스트.txt","r", stdin);

	//1. Init;
	cin >> r >> c >> T;
	vector <vector<int>> map(r, vector<int>(c, 0));
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			cin >> map[i][j];
		}
	}
			
	for (int t = 0; t < T; t++) {
		vector<vector<int>> temp_map(r, vector<int>(c, 0));

		int vaccumn_pos = 0;

		// 2. Diffusion
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				if (map[i][j] == 0) {
					continue;
				}
				if (map[i][j] == -1) {
					vaccumn_pos = i;
					temp_map[i][j] = -1;
					continue;
				}
				if (map[i][j] < 5 ) {
					temp_map[i][j] = temp_map[i][j] + map[i][j];
					continue;
				}

				int update_side_value_from_criteria = map[i][j] / 5;
				int diff_cnt_from_criteria = 0;

				for (int k = 0; k < 4; k++) {
					int update_r = i + dr[k];
					int update_c = j + dc[k];

					if (update_r < 0 || update_r >= r || update_c < 0 || update_c >= c) {
						continue;
					}
					if (map[update_r][update_c] == -1) {
						continue;
					}
					diff_cnt_from_criteria = diff_cnt_from_criteria + 1;
					temp_map[update_r][update_c] = temp_map[update_r][update_c] + update_side_value_from_criteria;
				}
				int center_value = map[i][j] - (diff_cnt_from_criteria * update_side_value_from_criteria);
				
				temp_map[i][j] = temp_map[i][j] + center_value;

			}
		}

		// 3. Vaccumn Cleaner
		vector<vector<int>> temp_map_2(r, vector<int>(c, 0));

		// 3.1 uppder position
		// 3.1.0 set default value
		int vaccumn_upper_pos = vaccumn_pos-1;
		temp_map_2[vaccumn_upper_pos][0] = -1;
		for (int i = 1; i < vaccumn_upper_pos; i++) {
			for (int j = 1; j < c - 1; j++) {
				temp_map_2[i][j] = temp_map[i][j];
			}
		}

		// 3.1.1 mov Dside
		for (int j = 1; j < c-1; j++) {
			temp_map_2[vaccumn_upper_pos][j+1] = temp_map[vaccumn_upper_pos][j];
		}

		// 3.1.2 mov Rside
		for (int i = vaccumn_upper_pos; i > 0; i--) {
			temp_map_2[i-1][c - 1] =  temp_map[i][c - 1];
		}
		// 3.1.3 mov Uside
		for (int j = c-1; j > 0; j--) {
			temp_map_2[0][j-1] = temp_map[0][j];
		}
		// 3.1.4 mov Lside
		for (int i = 0; i < vaccumn_upper_pos-1; i++) {
			temp_map_2[i+1][0] = temp_map[i][0];
		}

		// 3.2 down position
		int vaccumn_down_pos = vaccumn_pos;
		temp_map_2[vaccumn_down_pos][0] = -1;

		for (int i = vaccumn_down_pos+1; i < r-1; i++) {
			for (int j = 1; j < c - 1; j++) {
				temp_map_2[i][j] = temp_map[i][j];
			}
		}

		// 3.2.1 mov Uside
		for (int j = 1; j < c-1; j++) {
			temp_map_2[vaccumn_down_pos][j + 1] = temp_map[vaccumn_down_pos][j];
		}
		// 3.2.2 mov Rside
		for (int i = vaccumn_down_pos; i < r-1; i++) {
			temp_map_2[i + 1][c-1] = temp_map[i][c-1];
		}
		// 3.2.3 mov Dside
		for (int j = c-1; j > 0; j--) {
			temp_map_2[r-1][j-1] = temp_map[r-1][j];
		}
		// 3.2.4 mov Lside
		for (int i = r-1; i > vaccumn_down_pos+1; i--) {
			temp_map_2[i - 1][0] = temp_map[i][0];
		}

		// 4. cal answer

		if (t == T - 1) {
			int answer = 0;
			for (int i = 0; i < r; i++) {
				for (int j = 0; j < c; j++) {
					if (temp_map_2[i][j] == 0 || temp_map_2[i][j] == -1)
						continue;
					answer = answer + temp_map_2[i][j];
				}
			}
			cout << answer;
		}

		// 5. Change map (update map, from temp_map to map)
		map.clear();
		temp_map.clear();
		map.assign(temp_map_2.begin(), temp_map_2.end());
	
	}
}


Share Tweet +1