B - リモコン / AtCoderRegularContest#001

arc001.contest.atcoder.jp

import java.io.*;
import java.util.*;

public class Main   {
    public static int A, B;
    public static int answer;

    private void Input()  {
        Scanner sc = new Scanner(System.in);
        A = Integer.parseInt( sc.next() );
        B = Integer.parseInt( sc.next() );
    }

    private void BFS()  {
        int[] data = {-10, -5, -1, 1, 5, 10};
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        Queue<Integer> que = new LinkedList<Integer>();
        boolean flg = false;

        que.add(A);
        map.put(A,0);
        while ( !que.isEmpty() )    {
            int value = que.poll();
            if (flg)    break;
            for (int i=0; i<6; ++i) {
                if ( !map.containsKey(value+data[i]) )  {
                    que.add(value+data[i]);
                    map.put(value+data[i],map.get(value)+1);
                }
                if ( map.containsKey(B) )   {
                    flg = true;
                    break;
                }
            }
        }
        answer = map.get(B);
    }

    private void Solve() {
        new Main().BFS();
    }

    private void Output() {
        PrintWriter pw = new PrintWriter(System.out);

        pw.println(answer);
        pw.flush();
    }

    public static void main(String[] args) throws Exception {
        Main instance = new Main();

        instance.Input();
        instance.Solve();
        instance.Output();
    }
}

幅優先探索で設定温度になり次第、変更回数を出力。
〜了〜