Skip to content
Extraits de code Groupes Projets
Valider 2886f323 rédigé par Martin Gyselinck's avatar Martin Gyselinck
Parcourir les fichiers

Never committed, idk what is it

parent d8ef4a79
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
package graphs;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.*;
/**
* Consider this class, BreadthFirstShortestPaths, which computes the shortest path between
......@@ -54,22 +51,21 @@ public class BreadthFirstShortestPaths {
// Breadth-first search from multiple sources
private void bfs(Graph G, Iterable<Integer> sources) {
Queue<Integer> queue = new LinkedList<>();
for (int s: sources) {
marked[s] = true;
for (Integer s: sources) {
queue.add(s);
marked[s] = true;
distTo[s] = 0;
}
while(!queue.isEmpty()) {
int v = queue.remove();
for (int w: G.adj(v)) {
if (!marked[w]) {
distTo[w] = distTo[v] + 1;
marked[w] = true;
queue.add(w);
while (!queue.isEmpty()) {
int c = queue.poll();
for (int v: G.adj(c)) {
if (!marked[v]) {
marked[v] = true;
distTo[v] = distTo[c] + 1;
queue.add(v);
}
}
}
}
/**
......
......@@ -67,19 +67,19 @@ public class GlobalWarmingPaths {
public List<Point> shortestPath(Point p1, Point p2) {
LinkedList<Point> path = new LinkedList<>();
if (p1.equals(p2)) {
if (altitude[p1.getX()][p2.getY()] > waterLevel) {
if (altitude[p1.getX()][p1.getY()] > waterLevel) {
path.add(p1);
return path;
}
return null;
}
boolean[][] marked = new boolean[altitude.length][altitude[0].length];
Point[][] edgeTo = new Point[altitude.length][altitude[0].length];
boolean[] marked = new boolean[altitude.length * altitude[0].length];
Point[] edgeTo = new Point[altitude.length * altitude[0].length];
final int[][] pos = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
LinkedList<Point> queue = new LinkedList<>();
queue.add(p1);
// Shortest => BFS
marked[p1.getX()][p1.getY()] = true;
marked[p1.getX() * cols + p1.getY()] = true;
while(!queue.isEmpty()) {
Point current = queue.remove();
if (current.getX() == p2.getX() && current.getY() == p2.getY()){
......@@ -91,16 +91,16 @@ public class GlobalWarmingPaths {
int nextX = current.getX() + x;
int nextY = current.getY() + y;
if ((0 <= nextX && nextX < altitude.length) && (0 <= nextY && nextY < altitude[0].length) && (altitude[nextX][nextY] > waterLevel)) {
if (!marked[nextX][nextY]) {
marked[nextX][nextY] = true;
edgeTo[nextX][nextY] = current;
if (!marked[nextX * cols + nextY]) {
marked[nextX* cols + nextY] = true;
edgeTo[nextX * cols + nextY] = current;
queue.add(new Point(nextX, nextY));
}
}
}
}
if (!marked[p2.getX()][p2.getY()]) return path;
for (Point i = p2; !i.equals(p1); i = edgeTo[i.getX()][i.getY()]) {
if (!marked[p2.getX() * cols + p2.getY()]) return path;
for (Point i = p2; !i.equals(p1); i = edgeTo[i.getX() * cols + i.getY()]) {
path.add(i);
}
path.add(p1);
......
......@@ -47,29 +47,25 @@ public class MineClimbing {
* 0 <= startY, endY < m
*/
public static int best_distance(int[][] map, int startX, int startY, int endX, int endY) {
int[][] dist = new int[map.length][map[0].length];
for(int i = 0; i < map.length; i++) {
Arrays.fill(dist[i], Integer.MAX_VALUE);
}
int[] dist = new int[map.length * map[0].length];
Arrays.fill(dist, Integer.MAX_VALUE);
PriorityQueue<Integer[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o[2]));
queue.add(new Integer[]{startX, startY, 0});
dist[startX][startY] = 0;
dist[startX * map[0].length + startY] = 0;
final int[][] pos = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
while (!queue.isEmpty()) {
Integer[] current = queue.poll();
if (current[0] == endX && current[1] == endY) break;
final int[][] pos = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
for (int[] posi: pos) {
int x = posi[0];
int y = posi[1];
int nextX = (map.length + current[0] + x) % map.length;
int nextY = (map[0].length + current[1] + y) % map[0].length;
int nextX = (map.length + current[0] + posi[0]) % map.length;
int nextY = (map[0].length + current[1] + posi[1]) % map[0].length;
int cost = Math.abs(map[nextX][nextY] - map[current[0]][current[1]]);
if (dist[nextX][nextY] > cost + dist[current[0]][current[1]]){
dist[nextX][nextY] = cost + dist[current[0]][current[1]];
queue.add(new Integer[]{nextX, nextY, dist[nextX][nextY]});
if (dist[nextX * map[0].length + nextY] > cost + dist[current[0]* map[0].length +current[1]]){
dist[nextX* map[0].length +nextY] = cost + dist[current[0]* map[0].length +current[1]];
queue.add(new Integer[]{nextX, nextY, dist[nextX* map[0].length +nextY]});
}
}
}
return dist[endX][endY];
return dist[endX* map[0].length +endY];
}
}
......@@ -81,27 +81,25 @@ public class LinearProbingHashST<Key, Value> {
}
/**
* TODO
*
* Resizes the hash table to the given capacity by re-hashing all of the keys
*
* @param capacity the capacity
*/
protected void resize(int capacity) {
Key[] oldKeys = keys;
Value[] oldVals = vals;
m = capacity; // obligé de le faire avant pcq sinon put marche pas correctement
// (quelle honte d'avoir galéré sur ça)
n = 0;
keys = (Key[]) new Object[capacity];
vals = (Value[]) new Object[capacity];
for (int i = 0; i < oldKeys.length; i++) {
if (oldKeys[i] != null)
put(oldKeys[i], oldVals[i]);
LinearProbingHashST<Key, Value> t = new LinearProbingHashST<>(capacity);
for (int i = 0; i < capacity(); i ++) {
if (keys[i] != null)
t.put(keys[i], vals[i]);
}
keys = t.keys;
vals = t.vals;
m = t.capacity();
}
/**
* TODO
*
* Inserts the specified key-value pair into the symbol table, overwriting the old
* value with the new value if the symbol table already contains the specified key.
* The load factor should never exceed 50% so make sure to resize correctly
......@@ -114,13 +112,16 @@ public class LinearProbingHashST<Key, Value> {
if (key == null) throw new IllegalArgumentException("BOUUUH");
if (n + 1 > m/2)
resize(2*m);
int hash = hash(key);
while(keys[hash] != key && keys[hash] != null)
hash = (hash+1)%m;
if (keys[hash] == null)
n += 1;
keys[hash] = key;
vals[hash] = val;
int i;
for (i = hash(key); keys[i] != null; i = (i+ 1)%m) {
if (keys[i].equals(key)) {
vals[i] = val;
return;
}
}
keys[i] = key;
vals[i] = val;
n++;
}
/**
......@@ -133,11 +134,9 @@ public class LinearProbingHashST<Key, Value> {
*/
public Value get(Key key) {
if (key == null) throw new IllegalArgumentException("BOUUUH");
for (int i = 0; i < capacity(); i++) {
if (keys[(hash(key) + i) % m] == null)
return null;
if (key.equals(keys[(hash(key) +i) % m]))
return vals[(hash(key) +i) % m];
for (int i = hash(key); keys[i] != null; i = (i+ 1)%m) {
if (keys[i].equals(key))
return vals[i];
}
return null;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter