개발자 톡
연습문제 톡
진정한 효도
C++ 코드 공유합니다.
- 등록일
- 2024-07-15 06:51:22
- 조회수
- 96
- 작성자
- coconut
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdint> #include <iostream> #include <vector> #define MAP_SIZE 3 using namespace std; int calculate_min_cost(vector<vector<int>>& elements) { int min_cost = INT32_MAX; for(int y = 0; y < elements.size(); ++y) { // 정렬 후 중복 제거 sort(elements[y].begin(), elements[y].end()); auto dup = unique(elements[y].begin(), elements[y].end()); elements[y].erase(dup, elements[y].end()); if(elements[y].size() == 1) { min_cost = 0; break; } for(int x = 0; x < elements[y].size() - 1; ++x) min_cost = min(min_cost, abs(elements[y][x] - elements[y][x + 1])); } return min_cost; } int main(int argc, char** argv) { vector<vector<int>> map(MAP_SIZE, vector<int>(MAP_SIZE)); vector<vector<int>> elements(MAP_SIZE * 2, vector<int>(MAP_SIZE)); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); for(int y = 0; y < MAP_SIZE; ++y) { for(int x = 0; x < MAP_SIZE; ++x) { cin >> map[y][x]; elements[y][x] = map[y][x]; } } for(int x = MAP_SIZE; x < MAP_SIZE * 2; ++x) { for(int y = 0; y < MAP_SIZE; ++y) elements[x][y] = map[y][x - MAP_SIZE]; } cout << calculate_min_cost(elements); return 0; }
#진정한_효도