회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
어디가 틀린 지 모르겠습니다.. ㅜ
14868번 - 문명
busalee
2년 전
0
고수님들 어디가 틀렸을까요.. ㅜ 이틀동안 한 문제만 붙잡고 있으니 너무 힘들어요
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <queue> #define MAX_K 100002 #define MAX_LEN 2001 using namespace std; int N, K, cnt, res; int Matrix[MAX_LEN][MAX_LEN], parents[MAX_K], weight[MAX_K]; bool visited[MAX_LEN][MAX_LEN]; int x_move[] = { -1,1,0,0 }, y_move[] = { 0,0,-1,1 }; queue<pair<int, pair<int, int>>> q; // color xpos ypos //Function for Debug() void show_matrix(); void show_visited(); void show_parents(); void show_weight(); void show_queue(); //Function for Union Find void setParents() { for (int i = 1; i <= cnt; i++) { parents[i] = i; weight[i] = 1; } } int Find(int a) { if (parents[a] == a) return a; else return parents[a] = Find(parents[a]); } void Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) return; else if (a > b) swap(a, b); if (weight[a] >= weight[b]) { weight[a] += weight[b]; parents[b] = a; } else { weight[b] += weight[a]; parents[a] = b; } } bool isSame(int a, int b) { if (Find(a) == Find(b)) return true; else return false; } void Input() { int xpos, ypos; cin >> N >> K; for (int i = 0; i < K; i++) { cin >> xpos >> ypos; Matrix[xpos][ypos] = ++cnt; q.push({ cnt,{xpos,ypos} }); } } //Function For BFS bool InTheMatrix(int xpos, int ypos) { if (xpos >= 1 && xpos <= N && ypos >= 1 && ypos <= N) return true; else return false; } bool isBreak() // 탈출조건 { if (weight[1] == cnt) return true; else return false; } void setOuter() // 모든 값을 Union하고 겉이 있는 pos들만 queue에 넣는다 { int Len = q.size(); for (int i = 0; i < Len; i++) { int x_stand = q.front().second.first; int y_stand = q.front().second.second; int color = q.front().first; q.pop(); for (int j = 0; j < 4; j++) { int x = x_stand + x_move[j]; int y = y_stand + y_move[j]; if (!InTheMatrix(x, y)) continue; if (Matrix[x][y] == 0) // 가쪽에 있는 경우 { if (visited[x_stand][y_stand]) continue; else { q.push({ Find(color),{x_stand,y_stand} }); visited[x_stand][y_stand] = true; } } else if (Matrix[x][y]) Union(Matrix[x][y], Matrix[x_stand][y_stand]); } visited[x_stand][y_stand] = true; } } void Solve() { while (!q.empty()) { ++res; //First check int Len = q.size(); for (int i = 0; i < Len; i++) { int x_stand = q.front().second.first; int y_stand = q.front().second.second; int color = q.front().first; q.pop(); for (int j = 0; j < 4; j++) { int x = x_stand + x_move[j]; int y = y_stand + y_move[j]; if (!InTheMatrix(x, y)) continue; if (visited[x][y]) { if (isSame(color, Matrix[x][y])) continue; else Union(color, Matrix[x][y]); } else if(!visited[x][y]) { if (Matrix[x][y] == 0) // 아직 방문하지 않은 경우 { Matrix[x][y] = color; // 색칠한다 visited[x][y] = true; // 방문처리한다 q.push({ color,{x,y} }); //큐에 넣는다 } } } } //second Check Len = q.size(); for (int i = 0; i < Len; i++) { int x_stand = q.front().second.first; int y_stand = q.front().second.second; int color = q.front().first; q.pop(); for (int j = 0; j < 4; j++) { int x = x_stand + x_move[j]; int y = y_stand + y_move[j]; if (!InTheMatrix(x, y)) continue; if (Matrix[x][y] == 0) // 아직 방문하지 않은 경우 continue; else if (Matrix[x][y] != 0 && (Find(color)!=Find(Matrix[x][y]))) { Union(color, Matrix[x][y]); } } q.push({ color,{x_stand,y_stand} }); } if (isBreak()) return; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); Input(); setParents(); setOuter(); if (isBreak()) { cout << 0; return 0; } Solve(); cout << res; return 0; } void show_matrix() { cout << "This is matrix" << endl; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) cout << Matrix[i][j] << " "; cout << endl; } } void show_visited() { cout << "This is visited" << endl; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) cout << visited[i][j] << " "; cout << endl; } } void show_parents() { cout << "This is parents" << endl; for (int i = 1; i <= K; i++) cout << parents[i] << " "; cout << endl; } void show_weight() { cout << "This is weight" << endl; for (int i = 1; i <= K; i++) cout << weight[i] << " "; cout << endl; } void show_queue() { cout << "This is queue" << endl; while (!q.empty()) { cout << q.front().first << " " << q.front().second.first << " " << q.front().second.second << endl; q.pop(); } }
댓글을 작성하려면
로그인
해야 합니다.
busalee 2년 전
고수님들 어디가 틀렸을까요.. ㅜ 이틀동안 한 문제만 붙잡고 있으니 너무 힘들어요