2011 Waterloo Cup Java final connectivity problem

Source: Internet
Author: User

This Friday will be the final of the Waterloo Cup, the last year's title was done, the fifth question connectivity: share:

Topic:

BMP is a common format for image storage.
If you use a Black-and-white image (color depth =1), its information is easier to read.

data associated with: (The

following offsets start at the file header)
offset: 10 bytes, length 4 bytes: Where the image data really begins.
Offset: 18 Bytes, length 4 bytes: bitmap width, in pixels.
offset: 22 bytes, length 4 bytes: bitmap height, in pixels.

at the beginning of the image data, each pixel is represented by 1 bits.
from the bottom row of the picture, one line is stored up.

windows requires that a scanned row in the image file must be a multiple of 4 bytes, and that
insufficient bits are filled with 0. For example, a picture has a width of 45 pixels and actually occupies
8 bytes per line.

you can generate and edit binary images from the drawing tools that are brought in by Windows.
you need to select Black and white in properties and specify a two-valued image.
you may need to view | zoom | Customize ... Make the image larger and easier to
operate.

the lower-left corner of the image is the starting position of the image data. White corresponds to 1, black corresponds to 0

we can define: two point distance if less than 2 pixels, the two points are considered connected. That
is to say: In a nine lattice centered on a point, the 8 points around it are connected to it.
as shown in T1.bmp, the points in the lower left corner form a connected group,
while the points in the upper-right corner are isolated.

The goal of the program is: according to the given black and white bitmap, analyze all the independent connected groups,
output the area of each connected group. The so-called area is the number of pixels it contains.

the input data is fixed in the in.bmp.

as in the case of In.bmp, the program should output: I-i-i

The output said: There are 4 connected groups. The
output of the total area between the order can be arbitrary.

Please program to solve the above problem.

when we test the program, we will use a different in.bmp file.


candidates are required to write all classes in one file.
after debugging, save and the examinee folder under the corresponding title number of "solution. txt" can be.
do not copy the related engineering documents. Please do not use the package statement. In

addition, only the syntax or calls allowed in the JDK1.5 can appear in the source program. Cannot use version 1.6 or later.







Source:

Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;

Import java.util.List;

Import Javax.imageio.ImageIO;
	Class Dian {public Boolean value = FALSE;
public Boolean visited = false;
	public class Liantong {public static dian ds[][] = null;
	public static int width, height;
	public static int result = 0;
	public static int visitednum = 0;
	public static list<integer> nums = new arraylist<integer> ();

	public static int num =-1;
		public static void Scanbmp (int x, int y) {if (x < 0 | | Y < 0 | | | x >= Width | | y >= height) {return;
			} if (!ds[x][y].visited) {ds[x][y].visited = true;
			visitednum++;

				if (ds[x][y].value) {result++;
				int tempnum = Nums.get (num) + 1;
				Nums.set (num, tempnum);
				Scanbmp (x-1, y);
				Scanbmp (X-1, y-1);
				Scanbmp (x-1, y + 1);
				Scanbmp (x + 1, y);
				Scanbmp (x + 1, y-1);
				Scanbmp (x + 1, y + 1);
				Scanbmp (x, y-1); ScanbmP (x, y + 1);
		}} public static void Main (string[] args) throws IOException {File File = new file ("In.bmp");
		BufferedImage bi = imageio.read (file);
		width = Bi.getwidth ();
		Height = bi.getheight ();
		ds = new Dian[width][height];
				for (int x = 0; x < width; x + +) {for (int y = 0; y < height; y++) {Ds[x][y] = new Dian ();
				if (Bi.getrgb (x, y) = = 1) {Ds[x][y].value = false;
				else {Ds[x][y].value = true;
				(int y = 0; y < height; y++) {for (int x = 0; x < width, x + +) {result = 0;
				num++;
				Nums.add (0);
				Scanbmp (x, y);
					if (result = = 0) {num--;
					if (Visitednum = = Height * width) {break;
		for (int i = 0; i < num + 1; i++) {System.out.println (Nums.get (i));
 }
	}
}
Improved version: [Code=java] Improved version of import Java.awt.image.BufferedImage;
Import Java.io.File;

Import java.io.IOException;

Import Javax.imageio.ImageIO;
	Class Dian {public Boolean value = FALSE;
public Boolean visited = false;
	public class Liantong {public static dian ds[][] = null;
	public static int width, height;
	public static int result = 0;

	public static int visitednum = 0;
		public static void Scanbmp (int x, int y) {if (x < 0 | | Y < 0 | | | x >= Width | | y >= height) {return;
			} if (!ds[x][y].visited) {ds[x][y].visited = true;
			visitednum++;
				if (ds[x][y].value) {result++;
				Scanbmp (x-1, y);
				Scanbmp (X-1, y-1);
				Scanbmp (x-1, y + 1);
				Scanbmp (x + 1, y);
				Scanbmp (x + 1, y-1);
				Scanbmp (x + 1, y + 1);
				Scanbmp (x, y-1);
			Scanbmp (x, y + 1);
		}} public static void Main (string[] args) throws IOException {File File = new file ("In.bmp");
		BufferedImage bi = imageio.read (file);
		width = Bi.getwidth (); Height =Bi.getheight ();
		ds = new Dian[width][height];
				for (int x = 0; x < width; x + +) {for (int y = 0; y < height; y++) {Ds[x][y] = new Dian ();
				if (Bi.getrgb (x, y) = = 1) {Ds[x][y].value = false;
				else {Ds[x][y].value = true;
				(int y = 0; y < height; y++) {for (int x = 0; x < width, x + +) {result = 0;
				Scanbmp (x, y);
					if (result = = 0) {if (Visitednum = = Height * width) {break;
				} else {System.out.println (result); }}} [/code]



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.