Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polyominoes (sp?)
#6
I made this in a few minutes:

(it's FreeBasic code)
[syntax="FreeBasic"]'Program for counting Polyominoes in a small grid
'Not very fast or efficient, but working
'- Neo

'#define DEBUG

'$dynamic

Declare Sub LoadData ()
Declare Sub Calculate ()
Declare Sub RecursiveScan (ByVal Object As Integer, ByVal X As Integer, ByVal Y As Integer)

Dim Shared Grid(0, 0) As Integer, gWidth As Integer, gHeight As Integer
Dim Shared NoPatches As Integer = 0

Call LoadData
Call Calculate

#ifdef DEBUG
For y = 0 To gHeight - 1
For x = 0 To gWidth - 1
Print Grid(x, y);
Next x
Print
Next y

Print: Print
#endif
Print "Number of Polyominoes in Grid:"; NoPatches
Sleep
End

Private Sub Calculate
For y = 0 To gHeight - 1
For x = 0 To gWidth - 1
If Grid(x, y) <> -1 Then
NoPatches+=1
RecursiveScan(Grid(x, y), x, y)
End If
Next x
Next y
End Sub

Private Sub RecursiveScan (ByVal Object As Integer, ByVal X As Integer, ByVal Y As Integer)
Grid(X, Y) = -1
If X - 1 >= 0 Then If Grid(X - 1, Y) = Object Then RecursiveScan(Object, X - 1, Y)
If X + 1 < gWidth Then If Grid(X + 1, Y) = Object Then RecursiveScan(Object, X + 1, Y)
If Y - 1 >= 0 Then If Grid(X, Y - 1) = Object Then RecursiveScan(Object, X, Y - 1)
If Y + 1 < gHeight Then If Grid(X, Y + 1) = Object Then RecursiveScan(Object, X, Y + 1)
End Sub

Private Sub LoadData
FF = FreeFile
Open "test.dat" For Input As #FF
'width and height
Input #FF, gWidth
Input #FF, gHeight

#ifdef DEBUG
Print "Width:"; gWidth
Print "Height:"; gHeight
#endif

Redim Grid(gWidth - 1, gHeight - 1) As Integer

#ifdef DEBUG
Print Chr$(13) + "Grid:"
#endif
For y = 0 To gHeight - 1
For x = 0 To gWidth - 1
Input #FF, letter$
Grid(x, y) = Asc(letter$) - Asc("A")
#ifdef DEBUG
Print Grid(x, y);
#endif
Next y
#ifdef DEBUG
Print
#endif
Next x
#ifdef DEBUG
Print
#endif

Close #FF
End Sub
[/syntax]

Tested it a few times and seems to work Smile It might not be fast, but I'm glad it works.

Wish they allowed FB on the programming competitions I did.
Reply


Messages In This Thread
Polyominoes (sp?) - by Lithium - 05-24-2005, 07:06 PM
Polyominoes (sp?) - by Blitz - 05-24-2005, 08:03 PM
Polyominoes (sp?) - by Z!re - 05-24-2005, 08:41 PM
Polyominoes (sp?) - by Meg - 05-24-2005, 08:42 PM
Oopps - by Lithium - 05-24-2005, 10:58 PM
Polyominoes (sp?) - by Neo - 05-25-2005, 04:09 PM
Wow - by Lithium - 05-25-2005, 06:57 PM
Polyominoes (sp?) - by Neo - 05-26-2005, 05:01 AM
Polyominoes (sp?) - by Lithium - 05-26-2005, 09:11 PM
Polyominoes (sp?) - by Pyrokid - 05-27-2005, 02:10 AM
Polyominoes (sp?) - by Lithium - 05-30-2005, 07:24 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)