선생님, 89번 토마토 문제 마지막 캐이스에서 시간초과가 됩니다.
291
forcommang
작성한 질문수 1
0
선생님, 안익은 토마토 개수를 세고 그 갯수를 빼가면서 다 익었는지 안익었는지를 검사하고 큐에 하루마다 -1을 집어넣어서 날짜를 셌습니다. 그런데 나머진 다 되도 마지막 문제만 시간초과가됩니다. 어디서 뭐 때문에 느려진 것인지 알 수 있을까요??
#include <cstdio>
#include <queue>
using std::pair;
int main(){
//freopen("input.txt", "r", stdin);
int n, m, tomatocnt = 0, pre = 0, now = 1, day = 0, tox, toy;;
short map[1002][1002];
std::queue<pair<int, int>> ripentomato;
scanf("%d%d",&n, &m);
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
scanf("%d", map[i]+j);
if(map[i][j] == 0)++tomatocnt;
else if(map[i][j] == 1)ripentomato.push(pair<int,int>(j,i));
}
}
for(int i = 0; i <= n+1; i++)map[0][i] = map[m+1][i] = -1;
for(int i = 0; i <= m+1; i++)map[i][0] = map[i][n+1] = -1;
int move[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
pair<int,int> temp;
while(tomatocnt){
ripentomato.push(std::make_pair(-1,-1));
while(true){
temp = ripentomato.front();
ripentomato.pop();
if(temp.first == -1)break;
for(int i = 0; i < 4; i++){
tox = temp.first+move[i][0];
toy = temp.second+move[i][1];
if(map[toy][tox] == 0){
ripentomato.push(pair<int,int>(tox, toy));
map[toy][tox] = 1;
--tomatocnt;
}
}
}
if(ripentomato.empty()){
printf("-1");
return 0;
}
++day;
}
printf("%d", day);
return 0;
}
답변 1
테스트 케이스 질문
0
371
1
병합정렬 시간복잡도 질문
0
461
1
41.연속된 자연수의 합 문제풀이에서 수학적인 원리를 모르고 있습니다.
0
1341
2
질문드립니다.
0
374
1
질문드립니다!
0
428
1
dev 프로그램 질문
0
273
1
문제가 이해가 안되요
0
374
1
4번 나이차이 문제 접근법 질문 드립니다.
0
305
1
source file not compiled
0
1033
3
59번 질문드립니다.
0
370
1
25번 문제 질문
0
346
1
4. 나이차이 문제 질문입니다.
0
368
1
90번 라이언 킹 심바 1번 테스트 케이스
0
468
1
71번 문제 전역 변수 질문 있습니다
0
357
1
75번, 79번 priority_queue관련
1
353
1
75.최대 수입 스케줄
0
394
2
복면산 정답의 수
0
428
1
테스트 케이스에 대해서
0
443
1
수업 내용 질문입니다!
1
229
1
풀어보면 좋은 문제 목록 - 2580 스토쿠 DFS 질문입니다!!
0
818
2
12. 플로이드-와샬(그래프 최단거리) . 27:25초
0
252
1
다른 풀이 방식
0
314
1
크루스칼 vs 프림
0
304
1
숫자 총개수 small 질문있습니다.
0
234
1





