This one took me a looong time to figure out the solution, i used dynamic programming approach to modify the bfs traversal.
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <cstring> #include <fstream> #include <string> #include <map> #include <queue> #define INF 100000000 //#define ll long long #define PASS 2 #define NEED 1 #define NONEED 0 #define HORIZON 1 #define VERTICAL 2 using namespace std; struct PP{ int x,y; int rank,Rrank; int MINIMUM,rMINIMUM; }; vector<int> noOfwaysWithTurnsXenter[4500]; vector<int> noOfwaysWithTurnsYenter[4500]; vector<int> RnoOfwaysWithTurnsXenter[4500]; vector<int> RnoOfwaysWithTurnsYenter[4500]; int resTurns,resPathCount; bool visited[4500]; int indexMap[50][90]; PP MapIndex[4500]; string str[50]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int stx=-1,sty=-1,edx=-1,edy=-1; bool visit[50][90]; int PathLength; int height=0; int src,dst; int nodeCount; //bool NewLine=0; //ofstream out; void init() { int k=0; resTurns=-1; resPathCount=-1; PathLength=-1; for(int i=0;i<50;i++) { for(int j=0;j<90;j++) { visit[i][j]=0; indexMap[i][j]=-1; MapIndex[k].rank=-1; MapIndex[k].Rrank=-1; MapIndex[k].MINIMUM=INF; MapIndex[k].rMINIMUM=INF; MapIndex[k].x=-1; MapIndex[k].y=-1; while(noOfwaysWithTurnsXenter[k].size()!=0) noOfwaysWithTurnsXenter[k].pop_back(); while(noOfwaysWithTurnsYenter[k].size()!=0) noOfwaysWithTurnsYenter[k].pop_back(); while(RnoOfwaysWithTurnsXenter[k].size()!=0) RnoOfwaysWithTurnsXenter[k].pop_back(); while(RnoOfwaysWithTurnsYenter[k].size()!=0) RnoOfwaysWithTurnsYenter[k].pop_back(); k+=1; } } } void print() { for(int i=0;i<=height;i++) cout<<str[i]<<"\n"; } bool visitCount() { bool ret=0; int MX; queue<int> parentNodes; while(!parentNodes.empty()) parentNodes.pop(); int m,x,y,tx,ty,trnk; int n; int counter=0; int i; for(int jj=0;jj<4500;jj++) visited[jj]=0; indexMap[stx][sty]=counter; MapIndex[counter].x = stx; MapIndex[counter].y = sty; MapIndex[counter].rank = 0; visit[stx][sty]=1; visited[0]=1; noOfwaysWithTurnsXenter[counter].push_back(0); noOfwaysWithTurnsYenter[counter].push_back(0); for(int j=0;j<4;j++) { tx = stx+dx[j]; ty = sty+dy[j]; if(tx>=0 && ty>=0 && tx<height && ty<str[tx].size() && str[tx][ty]!='X' && visit[tx][ty]==0) { counter+=1; visit[tx][ty]=1; indexMap[tx][ty]=counter; MapIndex[counter].x = tx; MapIndex[counter].y = ty; MapIndex[counter].rank = 1; visited[indexMap[tx][ty]]=1; parentNodes.push(indexMap[tx][ty]); if(dx[j]==0) { noOfwaysWithTurnsYenter[indexMap[tx][ty]].push_back(1); noOfwaysWithTurnsXenter[indexMap[tx][ty]].push_back(0); MapIndex[indexMap[tx][ty]].MINIMUM=0; } else if(dy[j]==0) { noOfwaysWithTurnsXenter[indexMap[tx][ty]].push_back(1); noOfwaysWithTurnsYenter[indexMap[tx][ty]].push_back(0); MapIndex[indexMap[tx][ty]].MINIMUM=0; } } } while(!parentNodes.empty()) { i = parentNodes.front(); parentNodes.pop(); x = MapIndex[i].x; y = MapIndex[i].y; if(x==edx && y==edy) { ret = 1; break; } //find adjacexnt higher ranks and visit count //if it accessed from x axis modify all parents path count of yaxis of parents each time increasing one turn for(int j=0;j<4;j++) { tx = x+dx[j]; ty = y+dy[j]; if(tx>=0 && ty>=0 && tx<height && ty<str[tx].size() && str[tx][ty]!='X') { if(visit[tx][ty]==0) { counter+=1; visit[tx][ty]=1; indexMap[tx][ty]=counter; MapIndex[counter].x=tx; MapIndex[counter].y=ty; MapIndex[counter].rank=MapIndex[i].rank+1; for(int ii=0;ii<(MapIndex[counter].rank+5);ii++) { noOfwaysWithTurnsXenter[counter].push_back(0); noOfwaysWithTurnsYenter[counter].push_back(0); } visited[indexMap[tx][ty]]=1; parentNodes.push(indexMap[tx][ty]); } if(dx[j]==0 && MapIndex[i].rank+1==MapIndex[indexMap[tx][ty]].rank) { for(int jj=0;jj<noOfwaysWithTurnsYenter[i].size();jj++)//=MapIndex[i].rank { noOfwaysWithTurnsYenter[indexMap[tx][ty]][jj]=noOfwaysWithTurnsYenter[i][jj]+noOfwaysWithTurnsYenter[indexMap[tx][ty]][jj]; noOfwaysWithTurnsYenter[indexMap[tx][ty]][jj+1]=noOfwaysWithTurnsXenter[i][jj]+noOfwaysWithTurnsYenter[indexMap[tx][ty]][jj+1]; } } else if(dy[j]==0 && MapIndex[i].rank+1==MapIndex[indexMap[tx][ty]].rank) { for(int jj=0;jj<noOfwaysWithTurnsXenter[i].size();jj++)//=MapIndex[i].rank { noOfwaysWithTurnsXenter[indexMap[tx][ty]][jj]=noOfwaysWithTurnsXenter[i][jj]+noOfwaysWithTurnsXenter[indexMap[tx][ty]][jj]; noOfwaysWithTurnsXenter[indexMap[tx][ty]][jj+1]=noOfwaysWithTurnsYenter[i][jj]+noOfwaysWithTurnsXenter[indexMap[tx][ty]][jj+1]; } } } }//for ends } nodeCount = counter+1; return ret; } void visitCount2() { int x,y,tx,ty,trnk; int MX; queue<int> parentNodes; while(!parentNodes.empty()) parentNodes.pop(); int parent=indexMap[edx][edy]; MapIndex[parent].Rrank=0; int i; int rnk = MapIndex[parent].Rrank;//PathLength-1; for(int jj=0;jj<4500;jj++) visited[jj]=0; visited[indexMap[edx][edy]]=1; for(int j=0;j<4;j++) { tx = edx+dx[j]; ty = edy+dy[j]; if(tx>=0 && ty>=0 && tx<height && ty<str[tx].size() && str[tx][ty]!='X' && indexMap[tx][ty]!=-1)//&& MapIndex[indexMap[tx][ty]].rank==MapIndex[parent].rank-1 { MapIndex[indexMap[tx][ty]].Rrank=1; visited[indexMap[tx][ty]]=1; parentNodes.push(indexMap[tx][ty]); if(dx[j]==0) { RnoOfwaysWithTurnsYenter[indexMap[tx][ty]].push_back(1); RnoOfwaysWithTurnsXenter[indexMap[tx][ty]].push_back(0); MapIndex[indexMap[tx][ty]].rMINIMUM=0; } else if(dy[j]==0) { RnoOfwaysWithTurnsXenter[indexMap[tx][ty]].push_back(1); RnoOfwaysWithTurnsYenter[indexMap[tx][ty]].push_back(0); MapIndex[indexMap[tx][ty]].rMINIMUM=0;//vertical move x changes } } } while(!parentNodes.empty()) { i = parentNodes.front(); parentNodes.pop(); x = MapIndex[i].x; y = MapIndex[i].y; rnk = MapIndex[i].Rrank; if(x==stx && y==sty) break; for(int j=0;j<4;j++) { tx = x+dx[j]; ty = y+dy[j]; if(tx>=0 && ty>=0 && tx<height && ty<str[tx].size() && str[tx][ty]!='X' && indexMap[tx][ty]!=-1) { if(visited[indexMap[tx][ty]]==0) { visited[indexMap[tx][ty]]=1; parentNodes.push(indexMap[tx][ty]); MapIndex[indexMap[tx][ty]].Rrank=rnk+1; for(int ii=0;ii<(MapIndex[indexMap[tx][ty]].Rrank+5);ii++) { RnoOfwaysWithTurnsXenter[indexMap[tx][ty]].push_back(0); RnoOfwaysWithTurnsYenter[indexMap[tx][ty]].push_back(0); } } if(dx[j]==0 && MapIndex[i].Rrank+1==MapIndex[indexMap[tx][ty]].Rrank) { for(int jj=0;jj<RnoOfwaysWithTurnsXenter[i].size();jj++)//=MapIndex[i].Rrank { RnoOfwaysWithTurnsYenter[indexMap[tx][ty]][jj+1]+=RnoOfwaysWithTurnsXenter[i][jj]; RnoOfwaysWithTurnsYenter[indexMap[tx][ty]][jj]+=RnoOfwaysWithTurnsYenter[i][jj]; } } else if(dy[j]==0 && MapIndex[i].Rrank+1==MapIndex[indexMap[tx][ty]].Rrank) { for(int jj=0;jj<RnoOfwaysWithTurnsYenter[i].size();jj++)//=MapIndex[i].Rrank { RnoOfwaysWithTurnsXenter[indexMap[tx][ty]][jj+1]+=RnoOfwaysWithTurnsYenter[i][jj]; RnoOfwaysWithTurnsXenter[indexMap[tx][ty]][jj]+=RnoOfwaysWithTurnsXenter[i][jj]; } } } }//for ends } } void minfinder() { for(int i=0;i<nodeCount;i++) { for(int j=0;j<RnoOfwaysWithTurnsXenter[i].size();j++) { if(RnoOfwaysWithTurnsXenter[i][j]+RnoOfwaysWithTurnsYenter[i][j]>0) { MapIndex[i].rMINIMUM=j; break; } } for(int j=0;j<noOfwaysWithTurnsXenter[i].size();j++) { if(noOfwaysWithTurnsXenter[i][j]+noOfwaysWithTurnsYenter[i][j]>0) { MapIndex[i].MINIMUM=j; break; } } } } void func() { minfinder(); int pass,x,y; int temp_rank; PathLength=0; resTurns=0; resPathCount=0; int index = indexMap[edx][edy]; PathLength = MapIndex[index].rank; resTurns = MapIndex[index].MINIMUM; if(resTurns!=INF) { resPathCount=noOfwaysWithTurnsXenter[index][resTurns]+noOfwaysWithTurnsYenter[index][resTurns]; cout<<resPathCount<<" paths, "<<PathLength+1<<" points, "<<resTurns<<" turns\n"; } for(int i=1;i<indexMap[edx][edy];i++) { int cary=0; //check for is there a new turn at the point i or not. //cross zero-cross non-zero case if(MapIndex[i].MINIMUM==INF || MapIndex[i].rMINIMUM==INF) continue; if(noOfwaysWithTurnsXenter[i][MapIndex[i].MINIMUM]==0 && RnoOfwaysWithTurnsYenter[i][MapIndex[i].rMINIMUM]==0) { if(noOfwaysWithTurnsYenter[i][MapIndex[i].MINIMUM]!=0 && RnoOfwaysWithTurnsXenter[i][MapIndex[i].rMINIMUM]!=0) { cary=1; } } else if(noOfwaysWithTurnsYenter[i][MapIndex[i].MINIMUM]==0 && RnoOfwaysWithTurnsXenter[i][MapIndex[i].rMINIMUM]==0) { if(noOfwaysWithTurnsXenter[i][MapIndex[i].MINIMUM]!=0 && RnoOfwaysWithTurnsYenter[i][MapIndex[i].rMINIMUM]!=0) { cary=1; } } if(MapIndex[i].rank+MapIndex[i].Rrank==PathLength && MapIndex[i].MINIMUM+MapIndex[i].rMINIMUM+cary==resTurns) str[MapIndex[i].x][MapIndex[i].y]='#'; } print(); } int main() { int x,y,px,py,s,t; int cnt = 0; int k=0; bool ret; height=0; //out.open("out_my.txt"); init(); while(getline(cin,str[height])) { if(str[height][0]=='_') { ret=visitCount(); if(ret==0) { cout<<"0 paths, 0 points, 0 turns\n"; print(); } else { visitCount2(); func(); } height=0; cnt = 0; stx=-1;sty=-1;edx=-1;edy=-1; init(); continue; } for(int i=0;i<str[height].size();i++) { if(str[height][i]=='@') { if(stx==-1) { stx = height; sty = i; } else { edx = height; edy = i; } } } height+=1; } //out.close(); return 0; } |