private static HashDequeue.Node fromLeft(HashDequeue.Node n) { int i = 0; while (n.next != null) { n = n.next; ++i; } return(new HashDequeue.Node(n, null, i)); }
private static HashDequeue.Node fromRight(HashDequeue.Node n) { int i = 0; while (n.prev != null) { n = n.prev; ++i; } return(new HashDequeue.Node(n, null, i)); }
// does everything - finds closest node to the one we want to be next too and places it there private static void findBest(HashDequeue.Node n, HashDequeue.Node connect) { HashDequeue.Node t = n; while (t != null) { if (t.data == connect.data) { return; } else { t = t.next; } } t = n; while (t != null) { if (t.data == connect.data) { return; } else { t = t.prev; } } HashDequeue.Node Lconnect = fromLeft(connect); HashDequeue.Node Rconnect = fromRight(connect); HashDequeue.Node L = fromLeft(n); HashDequeue.Node R = fromRight(n); answer.insert(connect); if (L.data + Rconnect.data < R.data + Lconnect.data) { L.next.next = Rconnect.next; Rconnect.next.prev = L.next; return; } R.next.prev = Lconnect.next; Lconnect.next.next = R.next; }
private static List <int> solveProblem(int[,] matrix) { int row; int col; while ((row = findMostConnections(matrix)) >= 0) { col = getRowHigh(row, matrix); if (answer.Contains(col)) { if (answer.Contains(row)) { findBest(answer.getInt(col), answer.getInt(row)); } else { findBest(answer.getInt(col), new HashDequeue.Node(null, null, row)); } } else { HashDequeue.Node n = new HashDequeue.Node(null, null, col); answer.insert(n); if (answer.Contains(row)) { findBest(n, answer.getInt(row)); } else { findBest(n, new HashDequeue.Node(null, null, row)); } } zero(row, col, matrix); } return(answer.toList()); }
private static List<int> solveProblem(int[,] matrix) { int row; int col; while ((row = findMostConnections(matrix)) >= 0) { col = getRowHigh(row, matrix); if (answer.Contains(col)) { if (answer.Contains(row)) findBest(answer.getInt(col), answer.getInt(row)); else findBest(answer.getInt(col), new HashDequeue.Node(null, null, row)); } else { HashDequeue.Node n = new HashDequeue.Node(null, null, col); answer.insert(n); if (answer.Contains(row)) findBest(n, answer.getInt(row)); else findBest(n, new HashDequeue.Node(null, null, row)); } zero(row, col, matrix); } return answer.toList(); }