public static void main(String[] args) throws IOException {
   while ((n = readInt()) != 0) {
     boolean possible = false;
     v = new HashSet<Integer>();
     State start = new State();
     for (int x = 0; x < n; x++) start.s[x].push(readInt());
     int end = 0;
     for (int x = 1; x <= n; x++) end += (x) * Math.pow(RADIX, x);
     Queue<Pos> q = new LinkedList<Pos>();
     q.offer(new Pos(toIndex(start), 0));
     while (!q.isEmpty()) {
       Pos curr = q.poll();
       State c = toState(curr.curr);
       if (curr.curr == end) {
         System.out.println(curr.moves);
         possible = true;
         break;
       }
       for (int x = 0; x < n - 1; x++) {
         if (c.s[x].size() == 0 && c.s[x + 1].size() == 0) continue;
         if (c.s[x + 1].size() == 0) {
           c.move(x, x + 1);
           int ni = toIndex(c);
           if (!v.contains(ni)) {
             v.add(ni);
             q.offer(new Pos(ni, curr.moves + 1));
           }
           c.move(x + 1, x);
         } else if (c.s[x].size() == 0) {
           c.move(x + 1, x);
           int ni = toIndex(c);
           if (!v.contains(ni)) {
             v.add(ni);
             q.offer(new Pos(ni, curr.moves + 1));
           }
           c.move(x, x + 1);
         } else if (c.s[x].peek() < c.s[x + 1].peek()) {
           c.move(x, x + 1);
           int ni = toIndex(c);
           if (!v.contains(ni)) {
             v.add(ni);
             q.offer(new Pos(ni, curr.moves + 1));
           }
           c.move(x + 1, x);
         } else if (c.s[x].peek() > c.s[x + 1].peek()) {
           c.move(x + 1, x);
           int ni = toIndex(c);
           if (!v.contains(ni)) {
             v.add(ni);
             q.offer(new Pos(ni, curr.moves + 1));
           }
           c.move(x, x + 1);
         }
       }
     }
     if (!possible) System.out.println("IMPOSSIBLE");
   }
 }
Esempio n. 2
0
  public Action absearch(State state, int d) {
    int score;
    int mi = 0;
    int mj = 0;
    alpha = Integer.MIN_VALUE;
    beta = Integer.MAX_VALUE;
    int best = alpha;

    startTime = System.currentTimeMillis();
    for (int i = 0; i < state.board.length; i++) {
      for (int j = 0; j < state.board.length; j++) {
        if (state.board[i][j] == 0) {
          state.move(i, j, false);
          score = minValue(state, d - 1);
          state.undo(i, j); // undo move
          // System.out.printf("%+4d", score);
          if (score > best) {
            mi = i;
            mj = j;
            best = score;
          }
        } // else
        // System.out.print(" [] ");
        // Potential random choice area. need list and PRNG
      }
      System.out.println();
    }
    // System.out.println(best);
    return new Action(mi, mj);
  }
Esempio n. 3
0
File: DFS.java Progetto: chaozhao/AI
  public void solve() {
    State currentState;
    boolean isMove = false;

    while (!startState.isEqual(endState)) {
      int move = this.getRandomNumber();
      System.out.println(move);
      isMove = startState.isMoveable(move);
      if (isMove) {
        startState.move(move);
        startState.print();
      }
    }
  }
Esempio n. 4
0
  private int minValue(State state, int d) {
    if (state.checkWin() == 1) return Integer.MAX_VALUE / 2;
    if (state.checkWin() == -1) return Integer.MIN_VALUE / 2;
    if (state.spaces == 0) return 0;
    if (cutoff() || d <= 0) return eval(state);

    int best = beta;
    for (int i = 0; i < state.board.length; i++) {
      for (int j = 0; j < state.board.length; j++) {
        if (state.board[i][j] == 0) {
          state.move(i, j, false);
          best = Integer.min(best, maxValue(state, d - 1));
          state.undo(i, j);
          // if (best <= alpha)
          // return best;
          // beta = Integer.min(beta, best);
        }
      }
    }
    return best;
  }