Java
  Home arrow Java arrow Page 9 - AI-Based Problem Solving
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

AI-Based Problem Solving
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2005-06-02

    Table of Contents:
  • AI-Based Problem Solving
  • Search Techniques
  • The Depth-First Search
  • An Analysis of the Depth-First Search
  • The Hill-Climbing Search
  • An Analysis of Hill Climbing
  • Node Removal
  • Finding the “Optimal” Solution
  • Back to the Lost Keys

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    AI-Based Problem Solving - Back to the Lost Keys


    (Page 9 of 9 )

    To conclude this chapter on problem solving, it seems only fitting to provide a Java program that finds the lost car keys described in the first example. The accompanying code employs the same techniques used in the problem of finding a route between two cities, so the program is presented without further explanation.

    // Find the lost keys!
    import java.util.*;
    import java.io.*;
    // Room information.
    class RoomInfo {
      String from;
      String to;
      boolean skip;
     
    RoomInfo(String f, String t) {
        from = f;
        to = t;
        skip = false;
     
    }
    }
    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[numRooms] = new 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 = (RoomInfo) rev.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 = (RoomInfo) btStack.pop();
          iskeys(r.from, r.to);
        
    }
      }
    }


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    Buy this book now. This article is excerpted from chapter 10 of The Art of Java, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072229713). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT