寻找网格连线中最长的一条路线

Jobernowl 2016-06-27 05:27:00
网格内对于相邻格子假如数字相同则可以相连,如图所示

要求连线所需要的点不重复 也就是连成一条线的点是不重复的
自己写了一个遍历方法 但是毫无疑问比较消耗内存,并且当网格内的内容重复比较多的时候,会消耗很多时间
现在我把代码贴出来希望各位可以指点一下 算法之类的我实在是不会 但是给我说一下的话我会去认真思考的

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class Grid {

static class Cell {
int index;
int num;
int column;
int row;
int columns;
int rows;
int[][] grid;
Cell pre;
ArrayList<Cell> result;

public Cell(int index, int num, int column, int row, int columns, int rows, int[][] grid, Cell pre, ArrayList<Cell> result) {
this.index = index;
this.num = num;
this.column = column;
this.row = row;
this.columns = columns;
this.rows = rows;
this.grid = grid;
this.pre = pre;
this.result = result;
}

public void search() {
HashMap<String, Object> keys = null;
Cell pre = this.pre;
if (null != pre) {
keys = new HashMap<String, Object>();
while (null != pre) {
keys.put(getCellKey(pre.column, pre.row), null);
pre = pre.pre;
}
}
ArrayList<int[]> around = getArounds(num, column, row, columns, rows, grid, keys);
if (null != keys) {
keys.clear();
keys = null;
}
int count = around.size();
if (count > 0) {
int i = 0;
int index = this.index + 1;
while (i < count) {
int[] cell = around.get(i);
Cell ac = new Cell(index, num, cell[0], cell[1], columns, rows, grid, this, result);
ac.search();
i++;
}
} else {
result.add(this);
}
}
}

public static ArrayList<int[]> getArounds(int num, int column, int row, int columns, int rows, int[][] grid, HashMap<String, Object> keys) {
ArrayList<int[]> list = new ArrayList<int[]>();
int[][] cells = new int[][] { { column, row + 1 }, { column + 1, row + 1 }, { column + 1, row }, { column + 1, row - 1 }, { column, row - 1 }, { column - 1, row - 1 }, { column - 1, row }, { column - 1, row + 1 } };
int i = 0;
int count = cells.length;
while (i < count) {
int[] cell = cells[i];
int c = cell[0];
int r = cell[1];
if (c < 0 || r < 0 || c >= columns || r >= rows) {
i++;
continue;
}
if (grid[c][r] == num) {
if (null == keys || !keys.containsKey(getCellKey(c, r))) {
list.add(cell);
}
}
i++;
}
return list;
}

public static String getCellKey(int column, int row) {
StringBuilder sb = new StringBuilder();
int len = sb.length();
if (len > 0) {
sb.delete(0, len);
}
sb.append(column).append(",").append(row);
return sb.toString();
}

public static void main(String[] args) {
int columns = 6;
int rows = 5;
int[][] grid = new int[columns][rows];
int[] nums = new int[] { 1, 2, 3 };
int count = nums.length;
Random randm = new Random(System.currentTimeMillis());
for (int column = 0; column < columns; column++) {
for (int row = 0; row < rows; row++) {
int num = nums[randm.nextInt(count)];
grid[column][row] = num;
}
}
HashMap<Integer, ArrayList<Cell>> result = new HashMap<Integer, ArrayList<Cell>>();
for (int i = 0; i < count; i++) {
int num = nums[i];
ArrayList<Cell> list = new ArrayList<Cell>();
for (int column = 0; column < columns; column++) {
for (int row = 0; row < rows; row++) {
if (grid[column][row] == num) {
Cell cell = new Cell(1, num, column, row, columns, rows, grid, null, list);
cell.search();
}
}
}
if (list.size() > 0) {
result.put(num, list);
}
}
int maxIndex = 0;
Cell cell = null;
for (ArrayList<Cell> list : result.values()) {
for (Cell c : list) {
if (c.index > maxIndex) {
maxIndex = c.index;
cell = c;
}
}
}
int[][] pos = new int[maxIndex][];
StringBuilder sb = new StringBuilder();
for (int i = maxIndex - 1; i > -1; i--) {
pos[i] = new int[] { cell.column, cell.row };
sb.append(getCellKey(cell.column, cell.row));
sb.append("#");
cell = cell.pre;
}
sb.delete(sb.length() - 1, sb.length());
System.out.println(sb.toString());
}

}

...全文
326 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanta 2016-07-07
  • 打赏
  • 举报
回复
写思路吧,估计大牛都没时间分析你的代码。
Jobernowl 2016-06-27
  • 打赏
  • 举报
回复
@飞跃颠峰 好久不上来了 希望各位可以看看

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧