-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashcode2018.cc
195 lines (148 loc) · 3.82 KB
/
hashcode2018.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using namespace std;
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#include <queue>
struct macchina{
int tempo = 0;
int rowCella = 0;
int colCella = 0;
queue<int> coda;
//bool operator() (int i,int j) { return (i<j);}
};
struct corsa{
int rowPart;
int colPart;
int rowArr;
int colArr;
int minPart;
int maxArr;
bool val = true;
bool vis = false;
};
vector<corsa> corse;
vector<macchina> macchine;
int distanza(int rowPart, int colPart, int rowArr, int colArr);
int scegli(int row, int col, int tempo,int &timePartenza){
int percorso=-1;
int min=INT_MAX;
int temp;
int dist;
int soglia;
for(int i=0; i<corse.size(); i++){
if(corse[i].vis==false && corse[i].val==true){ //se non è gia stato visitato
dist = distanza(row,col,corse[i].rowPart,corse[i].colPart);
if(corse[i].minPart>=tempo+dist)
soglia=corse[i].minPart;
else
soglia=tempo+dist;
if (soglia<min){
min=soglia;
percorso=i;
}
}
}
//cout << min << endl;
timePartenza=min;
return percorso;
}
bool finito(){
for(int i=0; i<corse.size(); i++){
if(corse[i].vis==false)
return false;
}
return true;
}
void printCoda(){
ofstream out("output5.txt");
for(int i=0; i<macchine.size();i++){
out << macchine[i].coda.size() << " ";
while(!macchine[i].coda.empty()){
out << macchine[i].coda.front() << " ";
macchine[i].coda.pop();
}
out << endl;
}
}
void ordina(vector <macchina> &macchine){
int alto;
for (alto = macchine.size() - 1; alto > 0; alto— )
{
for (int i=0; i<alto; i++)
{
if (macchine[i].tempo>macchine[i+1].tempo) /* sostituire ">" con "<" per avere un ordinamento decrescente */
{
macchina tmp = macchine[i];
macchine[i] = macchine[i+1];
macchine[i+1] = tmp;
}
}
}
}
int scegliMacchina(){
int res;
int minTempo=INT_MAX;
for(int i=0;i<macchine.size();i++){
if(minTempo>macchine[i].tempo){
res=i;
minTempo=macchine[i].tempo;
}
}
return res;
}
int main(){
int nRighe;
int nColonne;
int nVeicoli;
int nCorse;
int bonus;
int nStep;
ifstream in("input5.txt");
in»nRighe»nColonne»nVeicoli»nCorse»bonus»nStep;
corse.resize(nCorse);
macchine.resize(nVeicoli);
for(int i=0; i<nCorse; i++){
in » corse[i].rowPart » corse[i].colPart
» corse[i].rowArr » corse[i].colArr
» corse[i].minPart » corse[i].maxArr;
if(corse[i].maxArr < corse[i].minPart + distanza(corse[i].rowPart, corse[i].colPart, corse[i].rowArr, corse[i].colArr)) corse[i].val = false;
}
int m;
while(!finito()){
m=scegliMacchina();
int timePartenza;
int way = scegli(macchine[m].rowCella,macchine[m].colCella,macchine[m].tempo,timePartenza);
if(way!=-1){
macchine[m].tempo=timePartenza+distanza(corse[way].rowPart,corse[way].colPart,corse[way].rowArr,corse[way].colArr);
macchine[m].rowCella=corse[way].rowArr;
macchine[m].colCella=corse[way].colArr;
macchine[m].coda.push(way);
corse[way].vis=true;
}
}
printCoda();
// for(int i=0; i<nCorse; i++){
//
// cout<<" corsa "<<i<<endl;
//
// cout << corse[i].rowPart << corse[i].colPart
// << corse[i].rowArr << corse[i].colArr
// << corse[i].minPart << corse[i].maxArr <<corse[i].val;
//
// cout<<endl;
//
// }
return 0;
}
int distanza(int rowPart, int colPart, int rowArr, int colArr){
int rowDist=0, colDist=0, distanza=0;
rowDist = rowArr-rowPart;
colDist = colArr-colPart;
if(rowDist<0)
rowDist= -rowDist;
if(colDist<0)
colDist=-colDist;
distanza=rowDist+colDist;
return distanza;
}