refactored Djikstra's algorithm's implementation to use map cells

This commit is contained in:
devZoGok
2023-09-03 09:55:23 +03:00
parent 9ca9bf8e09
commit 4e5c539519
5 changed files with 39 additions and 39 deletions
+5 -12
View File
@@ -2,7 +2,6 @@
#include <iostream>
#include "pathfinder.h"
#include "map.h"
namespace battleship{
using namespace std;
@@ -20,21 +19,15 @@ namespace battleship{
Pathfinder::Pathfinder(){
}
vector<int> Pathfinder::findPath(u32 **weights, int size, int source, int dest){
vector<int> Pathfinder::findPath(vector<Map::Cell> &cells, int source, int dest){
int size = cells.size();
u32 distances[size];
vector<int> paths[size];
paths[source].push_back(source);
for(int i = 0; i < size; i++){
for(int i = 0; i < size; i++)
distances[i] = (i == source ? 0 : impassibleNodeVal);
for(int j = 0; j < size; j++)
if(weights[i][j] > impassibleNodeVal){
cout << "Node val higher than allowed max value\n";
exit(-1);
}
}
vector<int> checkedNodes;
while(find(checkedNodes.begin(), checkedNodes.end(), dest) == checkedNodes.end()){
@@ -60,8 +53,8 @@ namespace battleship{
for(int i = 0; i < size; i++){
bool isChecked = find(checkedNodes.begin(), checkedNodes.end(), i) != checkedNodes.end();
if(!isChecked && (distances[vertStrich] + weights[vertStrich][i] < distances[i])){
distances[i] = distances[vertStrich] + weights[vertStrich][i];
if(!isChecked && (distances[vertStrich] + cells[vertStrich].getEdgeWeight(i) < distances[i])){
distances[i] = distances[vertStrich] + cells[vertStrich].getEdgeWeight(i);
paths[i] = paths[vertStrich];
paths[i].push_back(i);
}