日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA复习(二维数组——快排——迷宫)

發布時間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA复习(二维数组——快排——迷宫) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二維數組: (了解)

?

定義: 一維數組:? int[] array=new int[3] ;開辟了3個空間的int類型的數組

?

? ???? ??二維數組: int[][]

?

對于二維數組的初始化定義:

?

數組的動態初始化:

數據類型 數組名稱[][]=new 數據類型[行][列];

?

數組的靜態初始化

?

數據類型 數組名稱[][]=new 數據類型[][]{{1,2,3},{4,5,6},{7,8,9}};

?

范例:定義二維數組

//靜態初始化 二維數組

????????? int data[][]=new int[][] {{1,2,3},{4,5,6},{7,8,9}};

?????????

????????? System.out.println(data[1][2]);

對于二維數組的訪問第一個[] 代表外圍的數組? 第二個[] 里層數組的元素

?

范例:二維數組的遍歷

public static void main(String[] args) {

?????????

?????????

????????? //靜態初始化 二維數組

?????????

????????? int data[][]=new int[][] {{1,2,3},{4,5,6},{7,8,9}};

?????????

?????????

????????? for(int i=0;i<data.length;i++) {

??????????????

??????????????

?????????????? for(int x=0;x<data[i].length;x++) {

???????????????????

??????????????????? System.out.print(data[i][x]+"");

?????????????? }

?????????????? System.out.println();

????????? }

???? }

?

用二維數組定義一個迷宮地圖 ,找出迷宮地圖中存在的出路

?

二維數組地圖的代碼:

int[][] maze={

??????????????????????????????????????? {2,2,2,2,2,2,2,2,2},

??????????????????????????????????????? {2,0,0,0,0,0,0,0,2},

??????????????????????????????????????? {2,0,2,2,0,2,2,0,2},

??????????????????????????????????????? {2,0,2,0,0,2,0,0,2},

??????????????????????????????????????? {2,0,2,0,2,0,2,0,2},

??????????????????????????????????????? {2,0,0,0,0,0,2,0,2},

??????????????????????????????????????? {2,2,0,2,2,0,2,2,2},

??????????????????????????????????????? {2,0,0,0,0,0,0,0,2},

??????????????????????????????????????? {2,2,2,2,2,2,2,2,2}

??????????????????? };

?

?

?

快速排序:

?

分治算法? 分半??? 找到一個中間值

????????? key

13? 45? 6? 9? 43? 12??? key 左邊的 都比key 小?? 右邊的都比key大

?

?

范例:實現快速排序

?

package org.arry;

import java.util.Arrays;

public class QuickSort {

?

???? public static void main(String[] args) {

?????????

?????????

????????? int[]array=new int[] {49,38,65,97,76,13,27,49};

?????????

????????? ?quickSort(array, 0, array.length-1);

?????????

???? }

????

???? public static void quickSort(int[]array,int low,int high) {

?????????

?????????

????????? if(low<high) {

??????????????

?????????????? int mid=partition(array, low, high);? //取得分區結果

??????????????

?????????????? quickSort(array, low, mid-1);

????????? ????

?????????????? quickSort(array, mid+1, high);

????????? }

???? }

????

????

???? //分區

????

???? public static int partition(int[]array ,int low,int high) {

?????????

????????? //1 取得關鍵key

?????????

????????? //2 設置 左右下標? i=0??? j=length-1;

?????????

????????? // 把數組中的第一個元素作為關鍵字

?????????

????????? int key=array[low];

?????????

????????? int i=low, j=high;

?????????

?????????

????????? if(low<high) {

??????????????

?????????????? //J開始向左找

?????????????? while(i<j) {

???????????????????

???????????????????

??????????????????? while(i<j&&array[j]>=key) {

???????????????????????? j--;

??????????????????? }

???????????????????

??????????????????? if(i<j) {

????????????????????????

???????????????????????? array[i]=array[j];

???????????????????????? i++;

??????????????????? }

???????????????????

??????????????????? while(i<j&&array[i]<=key) {

????????????????????????

???????????????????????? i++;

??????????????????? }

??????????????????? if(i<j) {

????????????????????????

???????????????????????? array[j]=array[i];

????????????????????????

???????????????????????? j--;

??????????????????? }

?????????????? }

??????????????

?????????????? array[i]=key;

??????????????

?????????????? System.out.println("每次排序的結果="+Arrays.toString(array));

????????? }

?????????

????????? return i;

???? }

}

?

?

?

迷宮出口:

package org.arry;

?

public class Maze {

?

????

???? //入口坐標和出口坐標

????

???? private static int? startPosI;

????

???? private static int? startPosJ;

????

???? //出口的坐標

????

???? private static int endPosI;

????

???? private static int endPosJ;

????

???? //設置入口的坐標

???? public void setStart(int startPosI,int startPosJ) {

?????????

????????? this.startPosI=startPosI;

?????????

????????? this.startPosJ=startPosJ;

???? }

????

???? //設置出口的坐標

????

???? public void setEnd(int endPosI,int endPosJ) {

?????????

????????? this.endPosI=endPosI;

?????????

????????? this.endPosJ=endPosJ;

???? }

????

????

???? public static void visPos(int[][] cell,int i,int j) {

?????????

????????? cell[i][j]=1;

?????????

????????? if(i==endPosI&&j==endPosJ) { //找到出口了

??????????????

?????????????? System.out.println("找到了一條出口");

??????????????

??????????????

?????????????? for(int m=0;m<cell.length;m++) {

???????????????????

???????????????????

??????????????????? for(int n=0;n<cell[0].length;n++) {

?????????????? ?????????

???????????????????????? if(cell[m][n]==2) {

?????????????????????????????

????????????????????????????? System.out.print("2");

?????????????????????????????

???????????????????????? }else if(cell[m][n]==1) {

?????????????????????????????

????????????????????????????? System.out.print("*");

???????????????????????? }else {

?????????????????????????????

????????????????????????????? System.out.print(" ");

???????????????????????? }

??????????????????? }

??????????????????? System.out.println();

?????????????? }

????????? }

????????? //向左邊尋找通路

????????? if(cell[i][j-1]==0) {

??????????????

?????????????? visPos(cell, i, j-1);

????????? }

????????? //向右邊尋找通路

?????????

????????? if(cell[i][j+1]==0) {

??????????????

?????????????? visPos(cell, i, j+1);

????????? }

?????????

????????? //向上找

????????? if(cell[i-1][j]==0) {

??????????????

?????????????? visPos(cell, i-1, j);

????????? }

?????????

????????? //向下找

?????????

????????? if(cell[i+1][j]==0) {

??????????????

?????????????? visPos(cell, i+1, j);

????????? }

?????????

????????? cell[i][j]=0;

???? }

????

????

????

????

???? public static void main(String[] args) {

?????????

????????? int[][] maze={

??????????????????? {2,2,2,2,2,2,2,2,2},

??????????????????? {2,0,0,0,0,0,0,0,2},

??????????????????? {2,0,2,2,0,2,2,0,2},

??????????????????? {2,0,2,0,0,2,0,0,2},

??????????????????? {2,0,2,0,2,0,2,0,2},

??????????????????? {2,0,0,0,0,0,2,0,2},

??????????????????? {2,2,0,2,2,0,2,2,2},

??????????????????? {2,0,0,0,0,0,0,0,2},

??????????????????? {2,2,2,2,2,2,2,2,2}

????????? };

?????????

????????? Maze m=new Maze();

???? ????

????????? m.setStart(1, 1);

?????????

????????? m.setEnd(7, 7);

?????????

????????? m.visPos(maze, 1, 1);

???? }

}

?

?

總結

以上是生活随笔為你收集整理的JAVA复习(二维数组——快排——迷宫)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。