I get such a headache when trying to develop an appropriate algorithm to move from the START position to the EXIT position in the maze. For what it's worth, the maze is rectangular , maxsize 500x500 and, theoretically, can be resolved by DFS using some branching and binding methods ...
10 3 4
7 6
3 3 1 2 2 1 0
2 2 2 4 2 2 5
2 2 1 3 0 2 2
2 2 1 3 3 4 2
3 4 4 3 1 1 3
1 2 2 4 2 2 1
Output:
5 1 4 2
Explanation:
Our agent loses energy every time he takes a step, and he can only move up, down, left and right. In addition, if an agent arrives with a remaining energy of zero or less, he dies, so we print something like "Impossible."
, 10 , 3 4 - START (.. 3, 4) 7x6. , , ( ).
, , , , .
, DFS 500x500 , , .
The output means that the agent came with the remaining energy = 5 to the output pos 1 4 in 2 steps. If we look carefully, in this maze you can also go to pos 3 1 (column 3, line 1) with the same energy, but with 3 steps, so we choose the best.
With these considerations, can someone help me with some kind of code or pseudocode? I am having problems with a 2D array and how to save the remaining energy, path (or number of steps) ....
EDIT:
Larry, as I said, is a bit puzzled by the code. Here is what I have tried so far, only to determine the shortest path with smaller steps from START to EXIT, fixing EXIT in the meantime ...public class exitFromMaze {
int energy, startY, startX, xMax, yMax;
int adjMatrix[][];
boolean visited[][];
ArrayList<Cell> neighbours;
Cell start;
Stack<Cell> stack;
public exM() {
Scanner cin = new Scanner(System.in);
int nrTests = cin.nextInt();
for (int i = 0; i < nrTests; i++) {
energy = cin.nextInt();
startY = cin.nextInt()-1;
startX = cin.nextInt()-1;
xMax = cin.nextInt();
yMax = cin.nextInt();
adjMatrix = new int[yMax][xMax];
visited = new boolean[yMax][xMax];
this.stack = new Stack<Cell>();
for (int r = 0; r < yMax; r++) {
for (int c = 0; c < xMax; c++) {
adjMatrix[r][c] = cin.nextInt();
visited[r][c] = false;
}
}
start= new Cell(startX, startY, 0);
stack.push(start);
findBestExit();
}
}
private void findBestExit() {
Cell curCell;
while (!(stack.empty())) {
curCell = (Cell) (stack.pop());
if (curCell.row==0 && curCell.col== 4) {
System.out.println("Arrived at pos: "+curCell.row+","+curCell.col+" with E= "+(energy-curCell.getEnergy())+" with "+curCell.getSteps()+" steps");
break;
} else {
visited[curCell.row][curCell.col] = true;
}
this.neighbours = (ArrayList<Cell>) curCell.getNeighbours(this.xMax, this.yMax);
for (Cell neighbourCell: neighbours) {
if ( curCell.getEnergy() + neighbourCell.getEnergy() < this.energy && !visited[neighbourCell.row][neighbourCell.col]){
neighbourCell.energy+= curCell.energy;
neighbourCell.setSteps(curCell.getSteps()+1);
neighbourCell.setPrevious(curCell);
stack.push(neighbourCell);
}
}
}
}
class Cell {
int row;
int col;
int energy;
int steps;
Cell previous;
public Cell(int x, int y, int steps) {
this.row = x;
this.col = y;
this.energy = adjMatrix[x][y];
this.steps = steps;
this.previous = null;
}
public Cell(int x, int y, Cell prev) {
this.row = x;
this.col = y;
this.steps = 0;
this.energy = adjMatrix[x][y];
this.previous = prev;
}
@Override
public String toString() {
return "(,"+this.getRow()+","+this.getCol()+")";
}
public int getEnergy() {
return energy;
}
public void setEnergy(int energy) {
this.energy = energy;
}
public Cell getPrevious() {
return previous;
}
public void setPrevious(Cell previous) {
this.previous = previous;
}
public int getRow() {
return row;
}
public void setRow(int x) {
this.row = x;
}
public int getCol() {
return col;
}
public void setCol(int y) {
this.col = y;
}
public int getSteps() {
return steps;
}
public void setSteps(int steps) {
this.steps = steps;
}
public Cell south(int verticalLimit) {
Cell ret = null;
if (row < (verticalLimit - 1)) {
ret = new Cell(row+1, col, this);
}
return ret;
}
public Cell north() {
Cell ret = null;
if (row > 0) {
ret = new Cell(row-1, col ,this);
}
return ret;
}
public Cell west() {
Cell ret = null;
if (col > 0) {
ret = new Cell(row, col-1,this);
}
return ret;
}
public Cell east(int horizontalLimit) {
Cell ret = null;
if (col < (horizontalLimit - 1)) {
ret = new Cell(row , col+1, this);
}
return ret;
}
public List getNeighbours(int xlimit, int ylimit) {
ArrayList<Cell> res = new ArrayList<Cell>(4);
Cell n;
n = south(ylimit);
if (n != null) {
res.add(n);
}
n = north();
if (n != null) {
res.add(n);
}
n = east(xlimit);
if (n != null) {
res.add(n);
}
n = west();
if (n != null) {
res.add(n);
}
return res;
}
}
private void printArray(int h, int w) {
int i, j;
System.out.print(" ");
for (i = 0; i < w; i++) {
System.out.print("\t" + i);
}
System.out.println();
for (int r = 0; r < h; r++) {
System.out.print(" " + r);
for (int c = 0; c < w; c++) {
System.out.print("\t" + adjMatrix[r][c]);
}
System.out.println("");
}
System.out.println();
}
public static void main(String args[]) {
new exM();
}
}
To enter:
1
40 3 3
7 8
12 11 12 11 3 12 12
12 11 11 12 2 1 13
11 11 12 2 13 2 14
10 11 13 3 2 1 12
10 11 13 13 11 12 13
12 12 11 13 11 13 12
13 12 12 11 11 11 11
13 13 10 10 13 11 12
It should print:
12 5 1 8
those. agent output in the best yield (0.4), with remaining energy = 12 and in just 8 steps.
, , , ?
... - ...
/ ( ( > 0), ).
3
40 3 3
7 8
12 11 12 11 3 12 12
12 11 11 12 2 1 13
11 11 12 2 13 2 14
10 11 13 3 2 1 12
10 11 13 13 11 12 13
12 12 11 13 11 13 12
13 12 12 11 11 11 11
13 13 10 10 13 11 12
8 3 4
7 6
4 3 3 2 2 3 2
2 5 2 2 2 3 3
2 1 2 2 3 2 2
4 3 3 2 2 4 1
3 1 4 3 2 3 1
2 2 3 3 0 3 4
10 3 4
7 6
3 3 1 2 2 1 0
2 2 2 4 2 2 5
2 2 1 3 0 2 2
2 2 1 3 3 4 2
3 4 4 3 1 1 3
1 2 2 4 2 2 1
Output
12 5 1 8
Goodbye cruel world!
5 1 4 2