20160526:BFS solve eight digital problem (nine problem) by brushing an algorithm problem every day

Source: Internet
Author: User
Tags int size

All rights reserved. All rights reserved.

Welcome reprint, please indicate the source when reproduced:

http://blog.csdn.net/xiaofei_it/article/details/51524864


To prevent rigid thinking, brush an algorithmic question every day. It has been brushed for a few days now, send point code.

I have built an open source project, and every day the topic is in it:

Https://github.com/Xiaofei-it/Algorithms

Most of the algorithms are written by myself, without reference to the online generic code. The reader may find the code obscure because it is my own understanding.

The last few days have been writing something original, mostly non-recursive. In the future, ready to brush points DP, greedy and other problems.

The following is the BFS solve eight digital problems, this should actually use a * algorithm, but I practice today BFS, and then write a A * version.

/p>

/** * * Xiaofei * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License.  * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warra Nties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * */package xiaofei.algorithm;import java.util.arraylist;import java.util.hashset;/** * Created by Xiaofei on 16/5/26. * Eight digital problems, that is, nine problem. It was supposed to be a * algorithm, which was written a long time ago. I'll write it later. * * Today just want to practice BFS. * * This English do not know how to say, Eight digit maze is my own blind writing. * * 9 of 9 Square, can be expressed in int. int is 4 bytes. * * If you use other means, such as a byte to represent two, it doesn't seem to have an int save space. * * so just use int.    */public class Eightdigitmaze {private static int[] dx = new int[]{0, 0,-1, 1}; private static int[] dy = new int[]{-1, 1, 0, 0};        private static int arraytoint (int[][] array) {int result = 1;            for (int i = 0; i < 3; ++i) {final int[] Subarray = array[i];            for (int j = 0; j < 3; ++j) {result = result * 9 + subarray[j];    }} return result;        } private static int[][] Inttoarray (int x) {int[][] result = new INT[3][3];                for (int i = 2; I >= 0; i.) {for (int j = 2; J >= 0;--j) {result[i][j] = x% 9;            x/= 9;    }} return result;        } private static void output (int x) {int[][] array = Inttoarray (x);                    for (int i = 0, i < 3; ++i) {for (int j = 0; j < 3; ++j) {if (array[i][j] = = 0) {                System.out.print (");                } else {System.out.print (array[i][j]);        }} System.out.println ();    }    }    /**     * * The following function is very long, because I put everything in it. Why not become a sub-function?     It hurts because it becomes a sub-function.     * * My project is an algorithm, not a project, so do not care so much code style.            */public static void Solve (int[][] source, int[][] dest) {class Element {int state;            int last;                Element (int state) {this.state = state;            This.last =-1;                } Element (int, int last) {this.state = state;            This.last = Last;        }} hashset<integer> set = new hashset<> ();        Final int sourceint = arraytoint (source);        Final int destint = Arraytoint (dest);        Set.add (Sourceint);        If using queue, I do not know how to save the previous data, so here directly with ArrayList arraylist<element> queue = new arraylist<> ();        int head = 0;        int tail = 1;        Queue.add (New Element (Sourceint));            while (Head < tail) {element element = Queue.get (head);      if (element.state = = Destint) {//output result, exit loop.          arraylist<integer> tmp = new arraylist<> ();                int index = head;                    while (Index! =-1) {Tmp.add (index);                index = queue.get (index). Last;                } final int size = Tmp.size ();                    for (int i = size-1; I >= 0; i.) {SYSTEM.OUT.PRINTLN ("Step" + integer.tostring (size-i));                Output (Queue.get (Tmp.get (i)). State);            } break;            } int[][] Array = Inttoarray (element.state);            Boolean flag = false;            int x =-1, y =-1; for (int i = 0, i < 3; ++i) {for (int j = 0; j < 3; ++j) {if (array[i][j] = = 0)                        {flag = true;                        x = i;                        y = j;                    Break        }} if (flag) {break;        }} for (int i = 0; i < 4; ++i) {int nextx = x + dx[i];                int nexty = y + dy[i];  if (0 <= nextx && nextx <= 2 && 0 <= nexty && nexty <= 2) {int tmp                    = Array[nextx][nexty];                    Array[nextx][nexty] = 0;                    Array[x][y] = tmp;                    int state = Arraytoint (array);                        if (!set.contains (state)) {Queue.add (The new Element (state, head));                        Set.add (state);                    ++tail;                    } Array[nextx][nexty] = tmp;                Array[x][y] = 0;        }} ++head; }    }}


20160526:BFS solve eight digital problem (nine problem) by brushing an algorithm problem every day

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.