Hello,
I am having errors to public my solution to this problem. Server returned "NZEC" or "Wrong Answer" to my code. Please look my code:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args)
{
Main m = new Main();
m.processa();
System.exit(0);
}
public static void processa()
{
Scanner s = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(s.nextLine());
int sizeX = Integer.parseInt(st.nextToken());
int sizeY = Integer.parseInt(st.nextToken());
int[][] map = new int[sizeX][sizeY];
for(int i = 0; i < sizeX; i++)
{
s = new Scanner(System.in);
for(int j = 0; j < sizeY; j++)
{
map[i][j] = s.nextInt();
}
}
int iniX = -1;
int iniY = -1;
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
{
if(map[i][j] == 3)
{
iniX = i;
iniY = j;
}
}
}
ArrayList<Node> list = new ArrayList<>();
int index = 0;
list.add(new Node(iniX,iniY,3));
Node node = list.get(index);
sizeX = sizeX - 1;
sizeY = sizeY - 1;
while(node.getValue() != 0)
{
int x = node.getX();
int y = node.getY();
if(!(y-1 < 0) && map[x][y-1] != 2)
{
list.add(new Node(node, x, y-1, map[x][y-1]));
}
if(!(x-1 < 0) && map[x-1][y] != 2)
{
list.add(new Node(node, x-1, y, map[x-1][y]));
}
if(!(y+1 > sizeY) && map[x][y+1] != 2)
{
list.add(new Node(node, x, y+1, map[x][y+1]));
}
if(!(x+1 > sizeX) && map[x+1][y] != 2)
{
list.add(new Node(node, x+1, y, map[x+1][y]));
}
map[x][y] = 2;
index++;
node = list.get(index);
}
int count = 1;
while(node.getParent().getValue() != 3)
{
count++;
node = node.getParent();
}
System.out.println(count);
}
}
class Node
{
private Node parent = null;
private int value = -1;
int x;
int y;
boolean did = false;
public Node(int x, int y, int value)
{
this.x = x;
this.y = y;
this.value = value;
}
public Node(Node parent, int x, int y, int value)
{
this.parent = parent;
this.value = value;
this.x = x;
this.y = y;
}
public Node getParent()
{
return parent;
}
public void setParent(Node parent)
{
this.parent = parent;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
public boolean isDid()
{
return did;
}
public void setDid(boolean did)
{
this.did = did;
}
}
I already tried the try catch clause among my code to avoid receive the exceptions. I already tested my code but I am still having errors. Please help me! Look the problem:
http://br.spoj.com/problems/DUENDE/