Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Zitat von »"dot"«
ist über die zahlen was bekannt (z.B. alle positiv, nur in bestimmten wertbereich, bestimmte verteilung...) oder sind es irgendwelche zahlen?
Zitat von »"dot"«
kannst du die wegkosten von einem punkt zum zielpunkt irgendwie abschätzen (wenn ja, bietet sich evtl. A* an, sonst nicht).
Zitat von »"dot"«
was genau bezweckst du damit?
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
public int[][] getShortestPath(int destinationX, int destinationY){ if((destinationX < 0) || (destinationX > map.length) || (destinationY < 0) || (destinationY > map[0].length)) throw new IllegalArgumentException("Coordinates invalid!"); InitBest(); for(int i = 0; i < map.length; i++) { min = 0; ShortestPathOrigin(i, 0, destinationX, destinationY); } return result; } private void ShortestPathOrigin(int orig_x, int orig_y, int dest_x, int dest_y) { int[] coords = {orig_x, orig_y}; min += map[orig_x][orig_y]; if((min > bestSolution) || (tmp.contains(coords))) { min -= map[orig_x][orig_y]; return; } tmp.add(coords); if((Math.abs(orig_x - dest_x) <= 1) && (Math.abs(orig_y - dest_y) <= 1)) { min += map[dest_x][dest_y]; if(min < bestSolution) { bestSolution = min; int counter = 0; result = new int[tmp.size()][2]; while(!tmp.isEmpty()) { int[] value = (int[])tmp.firstElement(); result[counter][0] = value[0]; result[counter][1] = value[1]; counter++; } result[counter][0] = dest_x; result[counter][1] = dest_y; } min -= map[dest_x][dest_y]; return; } for(int x = -1; x <= 1; x++) { for(int y = -1; y <= 1; y++) { if(ValidCoords(orig_x + x, orig_y + y)) ShortestPathOrigin(orig_x + x, orig_y + y, dest_x, dest_y); } } min -= map[orig_x][orig_y]; tmp.setSize(tmp.size() - 1); } private void InitBest() { bestSolution = 0; for(int x = 0; x < map.length; x++) { for(int y = 0; y < map.length; y++) { bestSolution += map[x][y]; } } } private boolean ValidCoords(int x, int y) { if((x < 0) || (x > map.length) || (y < 0) || ((x == 0) && (y == 0))) return false; return true; } |
Werbeanzeige