05-25-2005, 04:09 PM
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 It might not be fast, but I'm glad it works.
Wish they allowed FB on the programming competitions I did.
(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
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
#endif
Next x
#ifdef DEBUG
#endif
Close #FF
End Sub
[/syntax]
Tested it a few times and seems to work It might not be fast, but I'm glad it works.
Wish they allowed FB on the programming competitions I did.