- Code: Select all
-1
-1
-1
10
5
6
1
11
11
8
Moderator: Board moderators
-1
-1
-1
10
5
6
1
11
11
8import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
class Main {
public static int COMBINATIONS = 10000;
static class Graph {
public EdgeNode[] edges;
public boolean[] invalid;
public boolean[] discovered;
public int[] parent;
class EdgeNode {
public Integer y;
public EdgeNode next;
public EdgeNode(Integer y) {
this.y = y;
}
}
public Graph() {
edges = new EdgeNode[COMBINATIONS];
parent = new int[COMBINATIONS];
invalid = new boolean[COMBINATIONS];
discovered = new boolean[COMBINATIONS];
for (int i = 0; i < COMBINATIONS; i++) {
List<Integer> neighbors = getNeighbors(i);
Iterator<Integer> it = neighbors.iterator();
while (it.hasNext()) {
insertEdge(i, it.next(), true);
}
}
}
public void bfs(int start) {
int v, y;
EdgeNode p;
for (int i = 0; i < COMBINATIONS; i++) {
parent[i] = -1;
discovered[i] = false;
}
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(start);
discovered[start] = true;
while (!queue.isEmpty()) {
v = queue.pollLast();
p = this.edges[v];
while (p != null) {
y = p.y;
if (!invalid[y] && !discovered[y]) {
queue.push(y);
discovered[y] = true;
parent[y] = v;
}
p = p.next;
}
}
}
public int findPath(int start, int end) {
if ((start == end) || (end == -1)) {
return 0;
} else {
return 1 + findPath(start, parent[end]);
}
}
private void insertEdge(int x, int y, boolean directed) {
EdgeNode node = new EdgeNode(y);
node.next = this.edges[x];
this.edges[x] = node;
}
private List<Integer> getNeighbors(Integer combination) {
List<Integer> neighbors = new LinkedList<Integer>();
int digit, temp = combination;
for (int i = 0; i < 4; i++) {
digit = temp % 10;
temp = temp / 10;
if (digit == 0) {
neighbors.add(combination + 9 * (int) Math.pow(10, i));
neighbors.add(combination + (int) Math.pow(10, i));
} else if (digit == 9) {
neighbors.add(combination - 9 * (int) Math.pow(10, i));
neighbors.add(combination + (int) -Math.pow(10, i));
} else {
neighbors.add(combination + (int) -Math.pow(10, i));
neighbors.add(combination + (int) Math.pow(10, i));
}
}
return neighbors;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
int cases = Integer.parseInt(br.readLine().trim()), invalidCases = 0, source = 0, destination = 0;
Graph graph = new Graph();
for (int y = 0; y < cases; y++) {
source = Integer.parseInt(
br.readLine().replaceAll("[^0-9]", ""));
destination = Integer.parseInt(
br.readLine().replaceAll("[^0-9]", ""));
invalidCases = Integer.parseInt(
br.readLine().replaceAll("[^0-9]", ""));
for (int i = 0; i < invalidCases; i++) {
graph.invalid[Integer.parseInt(br.readLine().replaceAll("[^0-9]", ""))] = true;
}
graph.bfs(source);
if (graph.parent[destination] == -1) {
sb.append("-1\n");
} else {
sb.append(graph.findPath(source, destination) + "\n");
}
if (y != (cases-1)) {
br.readLine();
}
}
System.out.print(sb);
}
} private static int parseNumber(BufferedReader in) throws IOException {
String row;
while ((row = in.readLine()).trim().length() == 0);
return Integer.parseInt(row.replaceAll(" ", ""), 10);
}
brianfry713 wrote:On this case:
1
9 9 9 9
0 0 0 0
1
9 9 9 9
The code I just submitted and got AC with returns -1, if you'd rather believe your WA code and UVAtoolkit's 4 that's up to you, let us know if you can get AC with that logic. Others in this thread that have got AC agreed that it should be -1.
Users browsing this forum: No registered users and 0 guests