%website: http://contest-cemc.uwaterloo.ca/ccc/sitemap.shtml
var inFile : int
var fileName : string := "FLOOR5.IN.txt"
open : inFile, fileName, get
var rows, cols, totalTiles : int
get : inFile, totalTiles
get : inFile, rows
get : inFile, cols
var tiles : array 1 .. cols, 1 .. rows of int
var tempRows : array 1 .. rows of string
var letter : string
for row : 1 .. rows
get : inFile, tempRows (row)
end for
for col : 1 .. cols
for row : 1 .. rows
letter := tempRows (row) (col)
if letter = "I" then
tiles (col, row) := 0
else
tiles (col, row) := 1
end if
end for
end for
/*****************************************************************************
******************************************************************************
*****************************************************************************/
var usedTiles : int := 0
var roomSize : array 1 .. 1000 of int
var OPEN, CLOSED : array 1 .. cols, 1 .. rows of int
for i : 1 .. 1000
roomSize (i) := 0
end for
for i : 1 .. cols
for j : 1 .. rows
OPEN (i, j) := 0
CLOSED (i, j) := 0
end for
end for
var found : int
procedure findRoom
found := 0
for tx : 1 .. cols
for ty : 1 .. rows
if tiles (tx, ty) = 1 and CLOSED (tx, ty) = 0 then
OPEN (tx, ty) := 1
found := 1
exit when found = 1
end if
end for
exit when found = 1
end for
end findRoom
procedure checkTiles (strtX, strtY : int)
if CLOSED (strtX, strtY) = 0 then
usedTiles += 1
CLOSED (strtX, strtY) := 1
OPEN (strtX, strtY) := 0
if strtX > 1 then
if CLOSED (strtX - 1, strtY) = 0 and OPEN (strtX - 1, strtY) = 0 and tiles (strtX - 1, strtY) = 1 then
OPEN (strtX - 1, strtY) := 1
end if
end if
if strtX < cols then
if CLOSED (strtX + 1, strtY) = 0 and OPEN (strtX + 1, strtY) = 0 and tiles (strtX + 1, strtY) = 1 then
OPEN (strtX + 1, strtY) := 1
end if
end if
if strtY > 1 then
if CLOSED (strtX, strtY - 1) = 0 and OPEN (strtX, strtY - 1) = 0 and tiles (strtX, strtY - 1) = 1 then
OPEN (strtX, strtY - 1) := 1
end if
end if
if strtY < rows then
if CLOSED (strtX, strtY + 1) = 0 and OPEN (strtX, strtY + 1) = 0 and tiles (strtX, strtY + 1) = 1 then
OPEN (strtX, strtY + 1) := 1
end if
end if
end if
end checkTiles
var sx, sy : int := 1
var check : int := 0
procedure explore
check := 0
for tx : 1 .. cols
for ty : 1 .. rows
if OPEN (tx, ty) = 1 then %and CLOSED (tx, ty) = 0 and tiles (tx, ty) = 1 then
check := 1
sx := tx
sy := ty
exit when check = 1
end if
end for
exit when check = 1
end for
end explore
var room : int := 1
findRoom
loop
explore
checkTiles (sx, sy)
if check = 0 then
roomSize (room) := usedTiles
usedTiles := 0
room += 1
findRoom
end if
exit when found = 0
end loop
room := 0
for i : 1 .. 1000
if roomSize (i) > 0 then
room += 1
end if
exit when roomSize (i) = 0
end for
%sort rooms
var temp : int
for i : 1 .. room
for j : 1 .. room - 1
if roomSize (j) < roomSize (j + 1) then
temp := roomSize (j)
roomSize (j) := roomSize (j + 1)
roomSize (j + 1) := temp
end if
end for
end for
var rooms : int := 0
for i : 1 .. room
exit when totalTiles - roomSize (i) < 0
if totalTiles - roomSize (i) >= 0 then
totalTiles -= roomSize (i)
rooms += 1
end if
end for
put rooms, " rooms, ", totalTiles, " square meters left over"
close : inFile
|