33,008
社区成员
发帖
与我相关
我的任务
分享
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());
}
}