회원가입
로그인
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
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
불 문제 어디가 틀린지 모르겠습니다....
5427번 - 불
phs2105
5년 전
0
도저히 어디가 틀린지 모르겠어요 ㅠㅠㅠ
자꾸 틀렸다 나옵니다...
반례가 있을까요?
#include <iostream> #include <stdio.h> #include <queue> using namespace std; int row; int col; char map[1001][1001]; char temp_map[1001][1001]; int dist[1001][1001]; int f_dist[1001][1001]; int edge = -1; queue<int> result; void init_dist(){ for(int i = 0 ; i < row ; i++){ for(int j = 0 ; j < col ; j++){ dist[i][j] = -1; f_dist[i][j] = 1000; } } } void bfs(){ queue<pair<int, int> > q1; queue<pair<int, int> > fire; pair<int,int> p1, p2; for(int i = 0 ; i < row ; i++){ for(int j = 0 ; j < col ; j++){ if(map[i][j] == '@'){ if(i == 0 || i == row - 1 || j == 0 || j == col -1){ edge = 1; return; } else{ p1 = make_pair(i,j); q1.push(p1); dist[i][j] = 0; } } if(map[i][j] == '*'){ p2 = make_pair(i,j); fire.push(p2); f_dist[i][j] = 0; } } } while(!fire.empty()){ //불 번짐 int curX = fire.front().first; int curY = fire.front().second; fire.pop(); if(curX - 1 >= 0 && (temp_map[curX - 1][curY] == '.' || temp_map[curX - 1][curY] == '@' )){ fire.push(make_pair((curX - 1),curY)); temp_map[curX - 1][curY] = '*'; f_dist[curX - 1][curY] = f_dist[curX][curY] + 1; } if(curX + 1 < row && (temp_map[curX + 1][curY] == '.' || temp_map[curX + 1][curY] == '@')){ fire.push(make_pair((curX + 1),curY)); temp_map[curX + 1][curY] = '*'; f_dist[curX + 1][curY] = f_dist[curX][curY] + 1; } if(curY - 1 >= 0 && (temp_map[curX][curY - 1] == '.' ||temp_map[curX][curY - 1] == '@')){ fire.push(make_pair(curX,(curY-1))); temp_map[curX][curY - 1] = '*'; f_dist[curX][curY - 1] = f_dist[curX][curY] + 1; } if(curY + 1 < col && (temp_map[curX][curY + 1] == '.' || temp_map[curX][curY + 1] == '@' )){ fire.push(make_pair(curX,(curY+1))); temp_map[curX][curY + 1] = '*'; f_dist[curX][curY + 1] = f_dist[curX][curY] + 1; } } while(!q1.empty()){ //상근이가 이동하는 경로, int curX = q1.front().first; int curY = q1.front().second; q1.pop(); if(map[curX - 1][curY] == '.' && dist[curX][curY]+1 < f_dist[curX - 1][curY]){ q1.push(make_pair((curX - 1),curY)); map[curX - 1][curY] = '@'; dist[curX - 1][curY] = dist[curX][curY] + 1; } if(map[curX + 1][curY] == '.' && dist[curX][curY]+1 < f_dist[curX + 1][curY]){ q1.push(make_pair((curX + 1),curY)); map[curX + 1][curY] = '@'; dist[curX + 1][curY] = dist[curX][curY] + 1; } if(map[curX][curY - 1] == '.' && dist[curX][curY]+1 < f_dist[curX][curY - 1]){ q1.push(make_pair(curX,(curY-1))); map[curX][curY - 1] = '@'; dist[curX][curY - 1] = dist[curX][curY] + 1; } if(map[curX][curY + 1] == '.' && dist[curX][curY]+1 < f_dist[curX][curY + 1]){ q1.push(make_pair(curX,(curY+1))); map[curX][curY + 1] = '@'; dist[curX][curY + 1] = dist[curX][curY] + 1; } } } void exit_search(){ int p_total = -1; int min = 100000000; //여러 탈출구가 있을 경우 최단 거리의 출구 if(edge == 1){ min = 0; result.push(min+1); return; } else{ for(int i = 0; i < col; i++){ if(dist[0][i] > 0){ p_total = dist[0][i]; if(p_total < min){ min = p_total; } } if(dist[row-1][i] > 0){ p_total = dist[row-1][i]; if(p_total < min){ min = p_total; } } } for(int j = 0; j < row; j++){ if(dist[j][0] > 0){ p_total = dist[j][0]; if(p_total < min){ min = p_total; } } if(dist[j][col-1] > 0){ p_total = dist[j][col-1]; if(p_total < min){ min = p_total; } } } } if( p_total < 0){ result.push(-1); } else{ result.push(min+1); } } int main() { int caseNum; scanf("%d",&caseNum); for(int a = 0; a < caseNum; a++){ scanf("%d %d",&col, &row); init_dist(); for(int b = 0; b < row; b++){ for(int c = 0; c < col; c++){ scanf("%1s",&map[b][c]); temp_map[b][c] = map[b][c]; } } bfs(); exit_search(); } while(!result.empty()){ if(result.front()==-1){ printf("IMPOSSIBLE\n"); } else{ printf("%d\n",result.front()); } result.pop(); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
phs2105 5년 전
도저히 어디가 틀린지 모르겠어요 ㅠㅠㅠ
자꾸 틀렸다 나옵니다...
반례가 있을까요?