이거 왜 틀릴까요??
#include <iostream>#include <algorithm>#include <stdio.h>#include <iostream>#include <queue>
using namespace std;
queue<int> t;queue<int> r;
int chk[100001];
int main(){ int s,e,p,tt=0; scanf("%d %d",&s,&e); r.push(s); t.push(0);
while(1) { p=r.front(); tt=t.front(); r.pop(); t.pop(); chk[p]=1; if(p-1>=0&&chk[p-1]==0) { r.push(p-1); t.push(tt+1); } if(p*2<=100001 && chk[p+1]==0) { r.push(p+1); t.push(tt+1); } if(p*2<=100001 && chk[p*2]==0) { r.push(p*2); t.push(tt+1); } if(p==e) { break; } } printf("%d",tt); return 0;}
그리고
[1]
아래 코드를 따로 올릴 수 있는 칸이 있는데 거기 올려주시면 코드를 보기 답변자 입장에서 편할 것 같고,
[2]
코드를 질문 올리실 때는 자신이 어떤 알고리즘으로 이 문제를 푸셨는지 알려주시면 답변자 입장에서 더 답변하기 쉬울 것 같습니다.
댓글을 작성하려면 로그인해야 합니다.
rose0122 3년 전
이거 왜 틀릴까요??
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
queue<int> t;
queue<int> r;
int chk[100001];
int main()
{
int s,e,p,tt=0;
scanf("%d %d",&s,&e);
r.push(s);
t.push(0);
while(1)
{
p=r.front();
tt=t.front();
r.pop();
t.pop();
chk[p]=1;
if(p-1>=0&&chk[p-1]==0)
{
r.push(p-1);
t.push(tt+1);
}
if(p*2<=100001 && chk[p+1]==0)
{
r.push(p+1);
t.push(tt+1);
}
if(p*2<=100001 && chk[p*2]==0)
{
r.push(p*2);
t.push(tt+1);
}
if(p==e)
{
break;
}
}
printf("%d",tt);
return 0;
}