개발자 톡
연습문제 톡
장애물 인식 프로그램
C++ 코드 공유합니다.
- 등록일
- 2024-07-11 10:54:40
- 조회수
- 255
- 작성자
- coconut
#include <algorithm> #include <cstdio> #include <cstdint> #include <iostream> #include <string> #include <vector> #define NUM_DIRS 4 #define DIR_OFFSET_X 0 #define DIR_OFFSET_Y 1 using namespace std; static int map_size; static vector<vector<int>> map; static int DIRS[NUM_DIRS][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; // Left, Top, Right, Bottom int seek_region(int x, int y, int region_size = 0) { map[y][x] = 0; for(int i = 0; i < NUM_DIRS; ++i) { int nx = x + DIRS[i][DIR_OFFSET_X]; int ny = y + DIRS[i][DIR_OFFSET_Y]; if(nx < 0 || ny < 0 || nx >= map_size || ny >= map_size) continue; if(map[ny][nx] == 0) continue; region_size += seek_region(nx, ny); } return ++region_size; } void search() { vector<int> num_regions; for(int y = 0; y < map_size; ++y) { for(int x = 0; x < map_size; ++x) { if(map[y][x] == 0) continue; num_regions.push_back(seek_region(x, y)); } } sort(num_regions.begin(), num_regions.end()); cout << num_regions.size() << endl; for(int i = 0; i < num_regions.size(); ++i) cout << num_regions[i] << endl; } int main(int argc, char** argv) { string line; ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> map_size; map.resize(map_size, vector<int>(map_size)); for(int i = 0; i < map_size; ++i) { cin >> line; for(int j = 0; j < line.size(); ++j) map[i][j] = line[j] - '0'; } search(); return 0; }
#장애물_인식_프로그램
#c++