Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

1R/2016 09 23: Difference between revisions

From ZeroWiki
No edit summary
No edit summary
Line 5: Line 5:


= 참가자 =
= 참가자 =
* 15이원준
* 박인서
* 박인서
* [[곽정흠]]
* [[곽정흠]]

Revision as of 14:13, 25 September 2016

오늘의 문제

참가자

코드

15이원준

#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<utility>
using namespace std;

int main(){
  int arr[100001] = {0,};
  int N, M, ans;
  queue<pair<int, int>> que;
  cin>> N >> M;
  if(N >= M){
    cout<< N - M << endl;
    return 0;
  }
  que.push(make_pair(N, 0));
  while(que.front().first != M){
    int n, dep;
    n = que.front().first;
    dep = que.front().second;
    que.pop();
    if(n-1 >= 0 && !arr[n-1]){
      arr[n-1] = 1;
      que.push(make_pair(n - 1, dep + 1));
    }
    if(n+1 <= 100000 && !arr[n+1]){
      arr[n+1] = 1;
      que.push(make_pair(n + 1, dep + 1));
    }
    if(n * 2 <= 100000 && !arr[n*2]){
      arr[n*2] = 1;
      que.push(make_pair(n * 2, dep + 1));
    }
  }
  cout<< que.front().second << endl;

}

박인서

#include <iostream>
#include <queue>
typedef std::pair<int, int> pair_int;
std::queue<pair_int> q;
bool visit[200001];

int main()
{
	int s, e;
	std::cin >> s >> e;
	if (s < e) {
		q.push(pair_int(s, 0));
		while (q.front().first != e) {
			pair_int tq = q.front();
			if (tq.first < e && !visit[tq.first * 2])
				q.push(pair_int(tq.first * 2, tq.second + 1)), visit[tq.first * 2] = true;
			if (tq.first < e && !visit[tq.first + 1])
				q.push(pair_int(tq.first + 1, tq.second + 1)), visit[tq.first + 1] = true;
			if (tq.first > 0 && !visit[tq.first - 1])
				q.push(pair_int(tq.first - 1, tq.second + 1)), visit[tq.first - 1] = true;
			q.pop();
		}

		std::cout << q.front().second;
	}
	else std::cout << s - e;
	return 0;
}

곽정흠

아이디어

15이원준

박인서

  • 기본적인 아이디어는 Queue를 이용한 BFS이다.
  • 단 너무 숫자가 커지면 안되므로, 200000이 넘어가면 제한을 한다.
  • 그리고 뒤로 갈 경우 갈 수 있는 방법은 -1밖에 없으므로 그 것을 예외 처리 해준다.

곽정흠