Find the lost keys : Algorithms « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Algorithms 




Find the lost keys
Find the lost keys
 
/*
 
 * Chapter 10 - AI-Based Problem Solving The Art of Java by Herbert Schildt and
 * James Holmes McGraw-Hill/Osborne 2003
 */

import java.util.Stack;

//Room information.
class RoomInfo {
  String from;

  String to;

  boolean skip;

  RoomInfo(String f, String t) {
    from = f;
    to = t;
    skip = false;
  }
}

public class Keys {
  final int MAX = 100;

  // This array holds the room information.
  RoomInfo room[] new RoomInfo[MAX];

  int numRooms = 0// number of rooms

  Stack btStack = new Stack()// backtrack stack

  public static void main(String args[]) {
    String to, from;
    Keys ob = new Keys();

    ob.setup();

    from "front_door";
    to = "keys";

    ob.iskeys(from, to);

    if (ob.btStack.size() != 0)
      ob.route(to);
  }

  // Initialize the room database.
  void setup() {
    addRoom("front_door""lr");
    addRoom("lr""bath");
    addRoom("lr""hall");
    addRoom("hall""bd1");
    addRoom("hall""bd2");
    addRoom("hall""mb");
    addRoom("lr""kitchen");
    addRoom("kitchen""keys");
  }

  // Put rooms into the database.
  void addRoom(String from, String to) {
    if (numRooms < MAX) {
      room[numRoomsnew RoomInfo(from, to);
      numRooms++;
    else
      System.out.println("Room database full.\n");
  }

  // Show the route.
  void route(String to) {
    Stack rev = new Stack();
    RoomInfo r;
    int num = btStack.size();

    // Reverse the stack to display path.
    for (int i = 0; i < num; i++)
      rev.push(btStack.pop());

    for (int i = 0; i < num; i++) {
      r = (RoomInforev.pop();
      System.out.print(r.from " to ");
    }

    System.out.println(to);
  }

  /*
   * If there is a path between from and to, return true, otherwise return
   * false.
   */
  boolean match(String from, String to) {
    for (int i = numRooms - 1; i > -1; i--) {
      if (room[i].from.equals(from&& room[i].to.equals(to)
          && !room[i].skip) {
        room[i].skip = true// prevent reuse
        return true;
      }
    }

    return false// not found
  }

  // Given from, find any path.
  RoomInfo find(String from) {
    for (int i = 0; i < numRooms; i++) {
      if (room[i].from.equals(from&& !room[i].skip) {
        RoomInfo r = new RoomInfo(room[i].from, room[i].to);
        room[i].skip = true// prevent reuse

        return r;
      }
    }
    return null;
  }

  // Determine if there is a path between from and to.
  void iskeys(String from, String to) {
    int dist;
    RoomInfo r;

    // See if at destination.
    if (match(from, to)) {
      btStack.push(new RoomInfo(from, to));
      return;
    }
    // Try another connection.
    r = find(from);
    if (r != null) {
      btStack.push(new RoomInfo(from, to));
      iskeys(r.to, to);
    else if (btStack.size() 0) {
      // Backtrack and try another connection.
      r = (RoomInfobtStack.pop();
      iskeys(r.from, r.to);
    }
  }
}

           
         
  














Related examples in the same category
1.AnagramsAnagrams
2.Hanoi puzzleHanoi puzzle
3.FibonacciFibonacci
4.Sieve Sieve
5.Find connections using a depth-first searchFind connections using a depth-first search
6.Find connections using hill climbing.
7.Find optimal solution using least-cost
8.Compute the area of a triangle using Heron's FormulaCompute the area of a triangle using Heron's Formula
9.Compute prime numbers
10.Print a table of fahrenheit and celsius temperatures 1
11.Print a table of fahrenheit and celsius temperatures 2
12.Print a table of Fahrenheit and Celsius temperatures 3Print a table of Fahrenheit and Celsius temperatures 3
13.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
14.A programmable Finite State Machine implementation.
15.An extendable Graph datastructure.
16.Utilities for flop (floating-point operation) counting.
17.LU Decomposition
18.Reverse Polish Notation
19.Permutator test
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.