회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
테스트케이스는 다 통과 하는데 시간초과가 뜨네요 ㅜㅡ
12906번 - 새로운 하노이 탑
hellolee1234
2년 전
0
이 코드가 왜 시간초과가 뜰까요??
#include<iostream> #include<vector> #include<string> #include<queue> using namespace std; struct state{ string strA; string strB; string strC; }; queue<pair<state,int>> q; vector<state> checkList; bool isCompleted(string st1,string st2,string st3){ for(int i=0;i<st1.length();i++){ if(st1[i] != 'A') return false; } for(int i=0;i<st2.length();i++){ if(st2[i] != 'B') return false; } for(int i=0;i<st3.length();i++){ if(st3[i] != 'C') return false; } return true; } bool isChecked(state cur){ for(int i=0;i<checkList.size();i++){ if(checkList[i].strA == cur.strA && checkList[i].strB == cur.strB && checkList[i].strC == cur.strC) return true; } return false; } void move(state cur,int type,int cnt){ string tempA = cur.strA; string tempB = cur.strB; string tempC = cur.strC; if(type == 0){ if(tempA.length() == 0) return; tempB.push_back(tempA[tempA.length()-1]); tempA.pop_back(); }else if(type == 1){ if(tempA.length() == 0) return; tempC.push_back(tempA[tempA.length()-1]); tempA.pop_back(); }else if(type == 2){ if(tempB.length() == 0) return; tempA.push_back(tempB[tempB.length()-1]); tempB.pop_back(); }else if(type == 3){ if(tempB.length() == 0) return; tempC.push_back(tempB[tempB.length()-1]); tempB.pop_back(); }else if(type == 4){ if(tempC.length() == 0) return; tempA.push_back(tempC[tempC.length()-1]); tempC.pop_back(); }else { if(tempC.length() == 0) return; tempB.push_back(tempC[tempC.length()-1]); tempC.pop_back(); } state ncur = {tempA,tempB,tempC}; if(!isChecked(ncur)){ //cout << tempA << ' '<< tempB << ' '<< tempC << '\n'; q.push({ncur,cnt+1}); checkList.push_back(ncur); } } int bfs(state start){ q.push({start,0}); checkList.push_back(start); while(!q.empty()){ state cur = q.front().first; int cnt = q.front().second; q.pop(); if(isCompleted(cur.strA,cur.strB,cur.strC)){ //cout << cur.strA << ' '<< cur.strB << ' '<< cur.strC << '\n'; return cnt; } for(int i=0;i<6;i++){ move(cur,i,cnt); } } return 0; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int slen[3]; string sticks[3]; for(int i=0;i<3;i++){ cin >> slen[i]; sticks[i] = ""; if(slen[i]>0) cin >> sticks[i]; } state init = {sticks[0],sticks[1],sticks[2]}; cout << bfs(init); }
댓글을 작성하려면
로그인
해야 합니다.
hellolee1234 2년 전
이 코드가 왜 시간초과가 뜰까요??