회원가입
로그인
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
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
런타임 에러 질문입니다
7576번 - 토마토
jsoomin0103
4년 전
0
예제랑 다른 테스트케이스 넣어봐도 다 답이 잘 나오는데 자꾸 런타임 에러가 나네요ㅠ 뭐가 문제일까요??
#include <stdio.h> #include <utility> #include <queue> #pragma warning(disable:4996) using namespace std; int arr[1010][1010]; int visit[1010][1010];//해당 좌표 방문했는지 확인 typedef struct { int temp_i; int temp_j; }Node; Node temp_arr[1010];//큐에서 빼낸 경로 임시로 저장 int x[4] = { -1,1,0,0 };//상하좌우 int y[4] = { 0,0,-1,1 };//상하좌우 queue< pair<int, int> > q; //i좌표,j좌표 저장 int BFS(int N, int M) { int day_cnt = 0;//몇일 걸렸는지 int temp1, temp2;//큐에서 빼낸 i 좌표와 j 좌표 int temp_cnt;//큐에서 빼낸 개수 cnt while (!q.empty()) { temp_cnt = 0; while (!q.empty()) { //범위 벗어나는 경우 고려 //-1인 경우 고려 temp1 = q.front().first;//i 좌표 temp2 = q.front().second;//j 좌표 q.pop(); visit[temp1][temp2] = -1;//방문 표시 arr[temp1][temp2] = 1;//익음 표시 temp_arr[temp_cnt].temp_i = temp1; temp_arr[temp_cnt].temp_j = temp2; ++temp_cnt; }//while문(안쪽) bool check = false; for (int i = 0; i < temp_cnt; i++) { temp1 = temp_arr[i].temp_i; temp2 = temp_arr[i].temp_j;//큐에서 값 빼냄 for (int j = 0; j < 4; j++) { if (temp1 + x[j]<0 || temp1 + x[j]>=N || temp2 + y[j]<0 || temp2 + y[j]>=M) { continue; }//범위 벗어나는 경우 if (visit[temp1 + x[j]][temp2 + y[j]] == -1) { continue; }//이미 방문한 경우 if (arr[temp1 + x[j]][temp2 + y[j]] == -1) { continue; }//빈곳인 경우 q.push(pair<int, int>(temp1 + x[j], temp2 + y[j])); check = true; }//상하좌우 탐색 }//큐에서 빼낸 토마토의 개수만큼 도는 for문 if (check) { ++day_cnt; } }//while문(바깥쪽) return day_cnt; }//BFS int main() { int M, N;//가로,세로 int cnt = 0;//들어왔을때 토마토의 개수가 몇개인지 scanf("%d %d", &M, &N); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%d", &arr[i][j]); if (arr[i][j] == 1) { ++cnt; q.push(pair<int, int>(i, j));//큐에 값 넣음 } }//가로 }//세로 //배열에 값 넣음 if (cnt == N * M) { printf("0"); }//모두 익은 토마토인 경우 else { int result=BFS(N,M);//함수 호출 cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (arr[i][j] == -1 || arr[i][j] == 1) { ++cnt; } } } if (cnt != N * M) { result = -1; } printf("%d", result); }//BFS return 0; }
댓글을 작성하려면
로그인
해야 합니다.
jsoomin0103 4년 전
예제랑 다른 테스트케이스 넣어봐도 다 답이 잘 나오는데 자꾸 런타임 에러가 나네요ㅠ 뭐가 문제일까요??