-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
125 lines (81 loc) · 2.5 KB
/
main.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <fstream>
#include <ostream>
#include <iostream>
#include <sstream>
#include <queue>
#include <iterator>
#include "Node.h"
using namespace std;
template <class Container>
void split1(const string& str, Container& cont)
{
istringstream iss(str);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(cont));
}
int main(int argc, char* argv[]) {
if(argc!=3){
cout<<"Run the code with the following command: ./project4 [input_file] [output_file]"<<endl;
return 1;
}
ifstream infile(argv[1]);
string line;
vector<string> input;
getline(infile, line);
vector<string> word;
split1(line, word);
int M=stoi(word[0]);
int N=stoi(word[1]);
int graph[M+1][N+1];
bool visited[1001][1001]={false};
for(int i=0;i<M;i++){
getline(infile, line);
vector<string> words;
split1(line, words);
for(int j=0; j<N; j++){
graph[i+1][j+1]=stoi(words[j]);
}
}
getline(infile, line);
getline(infile, line);
vector<string> wordd;
split1(line, wordd);
int startX=stoi(wordd[0]);
int startY=stoi(wordd[1]);
int targetX=stoi(wordd[2]);
int targetY=stoi(wordd[3]);
priority_queue<Node*, vector<Node*>, Node::compareNode> dist;
Node *a=new Node(startX, startY, 0);
dist.push(a);
while(!dist.empty()) {
Node *n=dist.top();
dist.pop();
if(n->X==targetX && n->Y==targetY){
ofstream myfile;
myfile.open(argv[2]);
myfile<<n->distTo;
myfile.close();
cout<<n->distTo<<endl;
break;
}
if(n->Y+1 <=N && visited[n->X][n->Y+1]==0) {
Node* n1=new Node(n->X, n->Y+1, max(n->distTo, abs(graph[n->X][n->Y+1]-graph[n->X][n->Y])));
dist.push(n1);
}
if(n->X+1 <=M && visited[n->X+1][n->Y]==0) {
Node* n2=new Node(n->X+1, n->Y, max(n->distTo, abs(graph[n->X+1][n->Y]-graph[n->X][n->Y])));
dist.push(n2);
}
if(n->Y-1 > 0 && visited[n->X][n->Y-1]==0) {
Node* n3=new Node(n->X, n->Y-1, max(n->distTo, abs(graph[n->X][n->Y-1]-graph[n->X][n->Y])));
dist.push(n3);
}
if(n->X-1 > 0 && visited[n->X-1][n->Y]==0) {
Node* n3=new Node(n->X-1, n->Y, max(n->distTo, abs(graph[n->X-1][n->Y]-graph[n->X][n->Y])));
dist.push(n3);
}
visited[n->X][n->Y]=true;
}
return 0;
}