-
백준 - 단지번호붙이기 [2667]공부/baekjoon 문제 2019. 6. 13. 15:16
#include <iostream> #include <vector> #include <algorithm> using namespace std; char map[25][25]; char map2[25][25]; vector<int> result; int num; int mapping(int i, int j, int island_num); int main() { int size = 0; int island_num = 1; cin >> size; for (int i = 0; i < size; i++) { cin >> map[i]; } for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) map2[i][j] = '0'; for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (map[i][j] == '1' && map2[i][j] == '0') { result.push_back(mapping(i,j,island_num)); num = 0; island_num++; } cout << island_num - 1 << endl; sort(result.begin(), result.end()); for (int i = 0; i < (int)result.size(); i++) if(result[i]!=-1) cout << result[i] << endl; return 0; } int mapping(int i, int j, int island_num) { if (map2[i][j] != '0') return -1; map2[i][j] = 1; num++; if (i > 0 && map[i - 1][j] == '1' && map2[i-1][j] == '0') { mapping(i-1, j, island_num); } if (j>0 && map[i][j - 1] == '1' && map2[i][j-1] == '0') { mapping(i, j - 1, island_num); } if (j+1<25 && map[i][j + 1] == '1' && map2[i][j+1] == '0') { mapping(i, j + 1, island_num); } if (i+1<25 && map[i+1][j] == '1' && map2[i + 1][j] == '0') { mapping(i+1, j, island_num); } return num; }
'공부 > baekjoon 문제' 카테고리의 다른 글
백준 - 연산자 끼워넣기 [14888] (0) 2019.06.13 백준 - Puyo Puyo [11559] (0) 2019.06.13 백준 - 바이러스 [2606] (0) 2019.06.13 해당 게시물들은 전부 기록을 위한 내용이며 주석 및 해석은 포함이 되어 있지 않습니다. (0) 2019.06.13 백준 - 알파벳 [1987] (0) 2019.06.13