War between two tribes

1

1 votes
Data Structures, Graph Theory, Algorithms, Approved, Medium
Problem

The inhabitants of BitVille and ByteVille are engaged in a periodic wars. Last month, BitVille successfully launched and orbited a spy telescope called the Hobble Scope. The purpose of the Hobble Scope was to count the number of Byte Warriors in ByteVille. The Hobble Scope, however, developed two problems because of poor quality control during its construction. Its primary lens was contaminated with bugs which block part of each image, and its focusing mechanism malfunctioned so that images vary in size and sharpness.

The computer programmers, who must rectify the Hobble Scope's problems are being held hostage in a The Byte Inn. The Hobble Scope's flawed images are stored by pixel in a file called Hobble.im

Each image is square and each pixel or cell contains either a 0 or a 1. The unique Hobble Scope Camera (HSC) records at each pixel location a 1 if part or all of a Byte Warrior is present and a 0 if any other object, including a bug, is visible. The programmers must assume the following:

  1. A Byte warrior is represented by at least a single binary one.
  2. Cells with adjacent sides on common vertices or Cells which are diagonally adjacent - which contain binary ones, comprise one Byte Warrior. A very large image of one Byte Warrior might contain all ones.
  3. Distinct Byte Warriers do not touch one another. This assumption is probably flawed, but the programmers are desperate.
  4. There is no wrap-around. Pixels on the bottom are not adjacent to the top and the left is not adjacent to the right (unless, of course, there are only 2 rows or 2 columns)

Input
The first line of the input contains N, the dimension of the image. It is followed by N rows, each line containing N digits. Each digit is either 0 or 1.

Output
The output should be a single number specifying the number of unique Byte Warriors in the image.

Constraint:
1 <= N <= 500

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?