hdu1241 detailed Java wide search done

Source: Internet
Author: User

Import Java.util.scanner;public class main{public static void Main (string[] args) {Scanner sc = new Scanner (system.in); whi Le (Sc.hasnext ()) {int m = Sc.nextint ();//Enter the number of rows of the map int n = sc.nextint ();//Enter the number of columns of the map if (M = = 0) {//if m=0 exits the program break;}  Initialization diagram plot[][] plots = new plot[m][n];//The information used to represent each point in the map for (int i = 0; i < m; i++) {String str = sc.next (); Used to accept the map for (int j = 0; J < N; j + +) {//assign each point the horizontal axis, ordinate, and geological conditions plots[i][j] = new Plot ();p lots[i][j].x = I;plots[i][j].y = J ;p lots[i][j].c = Str.charat (j);}} Search, view a few blocks of int count = 0;//Indicates the number of slices for the detected field for (int i = 0; i < m; i++) {for (int j = 0; J < N; j + +) {//Find oilfield location and breadth search if (plots[i][j].c = = ' @ ' &&!plots[i][j].isvisited) {//address for oilfield and not searched//Wide search Plotque queue = new Plotque (); Queue.add (P LOTS[I][J]);//Add the oil field found above into the queue BFs (plots, queue); count++;}} System.out.println (count);//output field of the number of slices, that is, the final result}}private static void BFs (plot[][] plots, plotque queue) {int px, py;int m = PLO Ts.length;int n = plots[0].length;while (!queue.isempty ()) {//guaranteed queue is not empty plot plot= Queue.pop (); Pop-up the elements of the queue//the breadth around the element to search for (int i = 0; i < 8; i++) {px = plot.x + dir[i][0];p y = plot.y + dir[i][1];//cannot cross over, geology is oilfield and has not been searched if (px >= 0 && px < m && py >= 0 && py < n && plots[px][py].c = = ' @ ' &&amp ;!plots[px][py].isvisited) {plots[px][py].isvisited = True;queue.add (Plots[px][py]);}}}} Final static int dir[][] = {{0,-1}, {0, 1}, {-1, 0}, {1, 0},{-1,-1}, {1,-1}, {-1, 1}, {1, 1}};//, lower, left  , right, top left, upper right, lower left, lower right}//used for each point class Plot {int x, y;//to indicate the position of the point Char C; Represents a point geological condition Boolean isvisited = false;   Indicates whether the point has been searched}//Queue class Plotque {Plot plots[];  The element loaded in the queue final int FRONT = 0;//represents the first element of the queue int end; Used to represent the number of elements in the queue, pointing to the last element of the queue//constructor public Plotque () {plots = new plot[100];end = 0;}  add element public void Add (Plot p) {Plots[end] = p;//add p to queue end++;//element number plus one}//popup element public Plot Pop () {//If the queue is empty, return directly to Nullif (end <= 0) {return null;} POPs the first element plot p = plots[front];//The position of the ejected element, all the queue elements move forward one if (end > 1) {for (int i = 0; i < end; i++) {Plots[i] = plots[i + 1];}} end--;//number of elements minus one return p; Returns the element p}//determines whether the queue is empty public boolean isEmpty () {if (end = = 0) {return true;} return false;}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1241 detailed Java wide search done

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.