"Programming Marathon algorithm Directory"
" 014-Red and Black" "Project download >>>"
1 Topic Description
There is a rectangular house, the ground is paved with red, black two color square tile. You stand on one of the black tiles and only move to the adjacent (up and down four directions) black tiles.
Please write a program that calculates how many blocks of black tiles you can reach in total.
1.1 Input Description:
The input contains multiple sets of data.
The first row of each group of data is two integers m and n (1≤m, n≤20). Immediately following the M line, each line consists of n characters. Each character represents the color of a tile, and the rules are as follows:
1. "." : Black tiles;
2. "#": White tiles;
3. "@": black tile, and you stand on this tile. The character appears only once in each data collection.
1.2 Output Description:
For each set of data, the output can reach a total of how many blocks of black tiles.
1.3 Input Example:
9 6....#......#..............................#@...#.#..#.
1.4 Output Example:
45
2 ideas for solving problems
You can view the red floor as an obstacle, starting at the @ floor and trying different ways to find the solution with the largest number of black floor steps. Since the floor can be traversed repeatedly, so long as the starting point to do a breadth-first traversal, records can be accessed by the number of black floors can be achieved.
3 Algorithm Implementation
ImportJava.util.ArrayDeque;ImportJava.util.Queue;ImportJava.util.Scanner;/** * Improvement Plan * <p> * Author: Wang Junshu * time:2016-05-11 09:34 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https:/ /github.com/wang-jun-chao * declaration:all rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {introw = Scanner.nextint ();intcol = Scanner.nextint ();int[] Floor =New int[Row] [Col]; for(inti =0; i < row; i++) {Floor[i] =New int[Col]; String line = Scanner.next (); for(intj =0; J < Col; J + +) {Floor[i][j] = Line.charat (j); }} System.out.println (Maxstep (floor)); } scanner.close (); }/** * Ask for the maximum number of steps you can take * * @param floor * @return Steps * * Private Static int Maxstep(int[] floor) {intx =0;inty =0;introw = Floor.length;intCol = floor[0].length;//Find the starting position for(inti =0; i < row; i++) { for(intj =0; J < Col; J + +) {if(Floor[i][j] = =' @ ') {x = i; y = j; } } }//Output floor information//For (int[] line:floor) {//for (int e:line) {//System.out.print ((char) e);// }//System.out.println ();// } returnFindpath (floor, x, y); }/** * The maximum number of steps for a black floor that can go * * @param Floor floors * @param x start coordinates * @param y Starting coordinates * / Private Static int Findpath(int[] Floor,intXintY) {introw = Floor.length;intCol = floor[0].length;if(X <0|| X >= Row | | Y <0|| Y >= Col | | Floor[x][y] = =' # ') {return 0; }//Record the location to be visited, two a groupqueue<integer> queue =Newarraydeque<> (Row * col *2);//Can be moved in four directions, two a group int[] D = {1,0,0,1, -1,0,0, -1}; Queue.add (x); Queue.add (y);//maximum number of black floors to walk intresult =1; while(!queue.isempty ()) {x = Queue.remove (); y = Queue.remove (); for(inti =0; i < d.length; i + =2) {intt = x + d[i];intv = y + d[i +1];if(T >=0&& T < row && v >=0&& v < col && floor[t][v] = ='. ') {//tag has been visitedFLOOR[T][V] =' # ';//Counter increasedresult++;//access to the location added to the queueQueue.add (t); Queue.add (v); } } }returnResult }}
4 Test Results
5 Other information
Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.
"Programming Marathon" "014-red and black"