Waterloo Cup Foundation practice back-form fetching number

Source: Internet
Author: User

* Recursion is too many layers, can not meet the requirements of the Waterloo Cup, so use the loop.

Import Java.util.Scanner; public class main{/** * To determine if the direction can be counted * @param m array of rows * @param n array of columns * @param dire number of directions * param x Number of current number of rows * @param y number of current columns * @param flags FETCH MARK * @return True indicates that the direction can be counted, false vice versa * Tatic boolean judge (int m,int n,int dire,int x,int y,boolean[][] flags) {switch (dire) {case 0:return m &G T
        x + 1 &&!flags[x + 1][y];
        Case 1:return n > y + 1 &&!flags[x][y + 1];
        Case 2:return x-1 >= 0 &&!flags[x-1][y];
        Case 3:return y-1 >= 0 &&!flags[x][y-1];
        Default:return false;
        }/** * Non-recursive row fetching * @param arr input data/public static void Huixingqushu (int[][) arr) {
        Initialize int m = arr.length;
        int n = arr[0].length;
        Boolean[][] flags = new Boolean[m][n];
        int dire = 0;
        int x,y;

        x = y = 0; System.out.print (Arr[x][y]);//First number flags[x][y] = true;
        Boolean resflag = true;
                    while (Resflag) {switch (dire) {case 0:{if (judge (M,n,0,x,y,flags)) {
                    If you can take a number down, then continue to take System.out.print ("" "+arr[++x][y]);
                Flags[x][y] = true; else if (judge (M,n,1,x,y,flags)) {//If you can take a number to the right, change the direction to the right to take the System.out.
                    Print ("" "+arr[x][++y]);
                    Flags[x][y] = true;
                Dire = (dire + 1)% 4;
            else Resflag = false;//below and right are not counted, take number to complete the break;
                    Case 1:{if (judge (M,n,1,x,y,flags)) {//If you can take a number to the right, continue to the right
                    System.out.print ("" "+arr[x][++y]);
                Flags[x][y] = true; else if (judge (M,n,2,x,y,flags)) {//If the number can be fetched, change direction to take SyStem.out.print ("" "+arr[--x][y]);
                    Flags[x][y] = true;
                Dire = (dire + 1)% 4;
            else Resflag = false;//to the right and above, take the number to complete the break;
                    Case 2:{if (judge (M,n,2,x,y,flags)) {//If the number can be taken up, proceed to the top
                    System.out.print ("" "+arr[--x][y]);
                Flags[x][y] = true; else if (judge (M,n,3,x,y,flags)) {//If you can take a number to the left, change the direction to the left System.out.
                    Print ("" "+arr[x][--y]);
                    Flags[x][y] = true;
                Dire = (dire + 1)% 4;
            else Resflag = false;//is not available above and to the left, take number to complete the break;
                    Case 3:{if (judge (M,n,3,x,y,flags)) {//If you can take a number to the left, continue to the left
                    System.out.print ("" "+arr[x][--y]);
                Flags[x][y] = true;
     }           else if (judge (M,n,0,x,y,flags)) {//If you can take a number down, change the direction and take the System.out.print ("
                    "+arr[++x][y]);
                    Flags[x][y] = true;
                Dire = (dire + 1)% 4;
            else Resflag = false;//to the left and below can not be taken, take number to complete the break; /** * Recursive row fetch number * @param arr input data * @param dest take a number of directions 0:down 1:right 2:u P 3:left * @param x fetching the current number of rows * @param y fetching the number of current columns * @param flags fetch marks/public static void Huixingq
        Ushu (int[][] arr,int dire,int x,int y,boolean[][] flags) {int res = arr[x][y];
        System.out.print ("" +res);
        System.out.flush ();
        Flags[x][y] = true;
        int m = arr.length;
        int n = arr[0].length;
                    Switch (dire) {case 0:{if (judge (m,n,0,x,y,flags))//If you can take a few down, continue down
      Huixingqushu (arr,dire,x + 1,y,flags);          else if (judge (m,n,1,x,y,flags))//If you can take a number to the right, change the direction to the right to take Huixingqushu (arr, dir
                E + 1)% 4,x,y + 1,flags); 
                    Else return;//is not available below and to the right, take the number complete} case 1:{if (judge (M,n,1,x,y,flags))
                If you can take a number to the right, then continue to the right to take Huixingqushu (Arr,dire,x,y + 1,flags); else if (judge (m,n,2,x,y,flags))//If the number can be taken up, change the direction to fetch Huixingqushu (arr, (dire + 1)% 4
                , x-1,y,flags); 
                    else return;//not to the right and top of the number, take the number complete} case 2:{if (judge (M,n,2,x,y,flags))
                If the number can be taken up, then continue to fetch Huixingqushu (arr,dire,x-1,y,flags); else if (judge (m,n,3,x,y,flags))//If you can take a number to the left, change the direction to the left Huixingqushu (arr, (dire + 1)% 4
                , x,y-1,flags); Else return;//can not be taken above and to the left, the number is completed} Case 3:{if (jUdge (M,n,3,x,y,flags))//If the left can be counted, continue to the left to take Huixingqushu (arr,dire,x,y-1,flags); else if (judge (m,n,0,x,y,flags))//If you can take a number down, change the direction to take Huixingqushu (arr,
                (dire + 1)% 4,x + 1,y,flags); Else return;//to the left and below are not available, number complete}} public static void Main (string[] args) {Scanner s
        can = new Scanner (system.in);
        int m = Scan.nextint ();
        int n = scan.nextint ();
        int[][] arr = new Int[m][n];
        for (int i = 0; i < m. i++) for (int j = 0; J < N; j +) Arr[i][j] = Scan.nextint ();

        Scan.close (); /*************** Recursive line Fetch number program section **************///System.out.print (arr[0][0]);//First Take out number one//boolean[][] flags = new Bo
Olean[m][n];
Flags[0][0] = true; 
if (M <= 1 && n <= 1) {}//If there is a maximum of only one number, the number is completed, do not do anything//else if (M <= 1)//////If Down is not available, then right Huixingqushu(Arr,1,0,1,flags);
        else////Otherwise down//Huixingqushu (arr,0,1,0,flags); 
        /*****************ned*********************//***************-**************/Huixingqushu (arr);
 /*****************ned*********************/}}

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.