5014번 스타트링크

동도리동·2021년 9월 3일
0

코딩테스트

목록 보기
31/76

왜 정답 비율이 33%인지 잘 모르겠다. 그나마 주의할 것이 있다면 ch배열을 1000001로 해야한다는 것?

#include <iostream>
#include <queue>

using namespace std;
struct Loc {
	int x, cnt;
	Loc(int a, int b) {
		x = a;
		cnt = b;
	}
};
int ch[1000001];
int main() {
	//freopen("in1.txt", "rt", stdin);
	int f, s, g, u, d;
	cin >> f >> s >> g >> u >> d;
	queue<Loc> Q;
	int dx[2] = { u,d };
	Q.push(Loc(s, 0));
	ch[s] = 1;
	while (!Q.empty()) {
		Loc tmp = Q.front();
		Q.pop();
		//cout << tmp.x << " " << tmp.cnt << '\n';
		if (tmp.x == g) {
			cout << tmp.cnt << '\n';
			return 0;
		}
		for (int i = 0; i < 2; i++) {
			int xx;
			if (i == 0) xx = tmp.x + dx[i];
			else if(i==1) xx = tmp.x - dx[i];
			
			if (xx > f || xx <= 0) continue;
			if (ch[xx] == 0) {
				ch[xx] = 1;
				Q.push(Loc(xx, tmp.cnt + 1));
			}
		}
	}
	cout << "use the stairs" << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보