Practices
  Home arrow Practices arrow Page 4 - Solving Problems with Recursion
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
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? 
PRACTICES

Solving Problems with Recursion
By: Mohamed Saad
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2004-11-17

    Table of Contents:
  • Solving Problems with Recursion
  • The Plot Thickens
  • First Programming Example
  • The Flood Fill Algorithm
  • How it Works
  • Connect 4 AI program
  • The Minimax Method
  • Placing Pieces
  • Trying it Out
  • Two More Tips

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Solving Problems with Recursion - The Flood Fill Algorithm


    (Page 4 of 10 )

    Now, let's turn our attention to a totally different problem. We are working on a 2D drawing package, and we want to make a bucket fill tool similar to that found in Microsoft Paint. The user selects a color, clicks on a point, and the color spreads to fill the area with this color. Have a look at this figure.

    Solving Problems with Recursion

    Fig 2. We need to write a program to flood fill a shape

    We want to write a function to implement this feature. We are given a 2 dimensional array of colors, a point to start painting from, and a color. We should fill the fill area with the color as mentioned above.

    There is no easy non-recursive solution to this problem, but we can solve this recursively in just 8 lines of code. How? The idea is simply to make a flood fill from a point do the following:

    • If the point we want to color is already filled, don't do anything and stop, else color it.
    • If the point above it is not colored, call flood fill from that point (recursive step).
    • If the point to the right is not colored, call flood fill from that point (recursive step).
    • If the point to the left is not colored, call flood fill from that point (recursive step).
    • If the point below it is not colored, call flood fill from that point (recursive step).

    Now, you see, the whole solution is just 5 steps, and 4 of them are recursive calls. It is important to understand how this actually works.

    class Canvas
    {
     byte board[][]=new byte[100][100];
     public void Fill(int x,int y,byte c)
     {
      if(board[x][y]==c) return;
      board[x][y]=c;
      if(x<99&&board[x+1][y]!=c)
       Fill(x+1,y,c);
      if(x>0&&board[x-1][y]!=c)
       Fill(x-1,y,c);
      if(y<99&&board[x][y+1]!=c)
       Fill(x,y+1,c);
      if(y>0&&board[x][y-1]!=c)
       Fill(x,y-1,c);
     }
    }

    I have tried to make the code simpler by fixing width and height. But, of course, it is easy to generalize the code to different sizes and color formats.

    More Practices Articles
    More By Mohamed Saad


       · Great article, looking forward to seeing more from you.
       · Must have been a loot of work.:))
       · I think the article is great and recursion is a very powerful technique to use when...
       · Excellent Article, looking for articles like these in the future.
       · have had a problem with my new ipod it keep running but does not play song that i...
     

       

    PRACTICES ARTICLES

    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas
    - Choosing the Right Team
    - Trees





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway