二维矩阵int[][] tips

Leave a comment

May 24, 2017 by oneOokay

一个二维矩阵int[][] : m X n

在不另建class的情况下:

Queue<int[]> queue = new LinkedList<int[]>();  queue里面直接放一个Int[2]代表一个点

queue.offer(new int[]{x, y});
queue.offer(start); //if start 是一个int[2]

boolean[][] visited = new boolean[m][n]: 用一个二维矩阵来代替HashSet<>.

  • visited[x][y] = true;

上下左右走用向量坐标表示:

int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; 下,右,上,左

for (int i = 0; i < dir.length; i ++){
 int x = cur[0], y = cur[1];
//这里的while loop里面就只能用x,y自身来做判定,不能另外用x+1,y+1
 while(x >= 0 && x < m && y >= 0 && y < n && maze[x][y] != 1){
 x += dir[i][0];
 y += dir[i][1];
 }//while loop结束之后要退一步
 x -= dir[i][0];
 y -= dir[i][1];
 }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: