UVA Patrol Robert

Source: Internet
Author: User

Perfect for a A * problem.

More than the usual maze problem plus a message K indicates the number of obstacles currently passing through.

#include <cstdio>#include<cstring>#include<queue>using namespacestd;Const intMAX = +;intm,n,k;intC[max][max][max];intG[max][max];inttarx,tary;structnode{intg,h; intx, y;//The greater the distance from the end of the subject x+y the closer   intK; BOOL operator< (Constnode& RHS)Const{      if(g + H > rhs.g + rhs.h)return true; if(g + H < Rhs.h + RHS.G)return false; if(g > RHS.G)return true; if(G < RHS.G)return false; returnK >RHS.K; }   voidh () {h= tarx-x+ tary-y; }};node start;#defineC (n) c[n.x][n.y][n.k]#defineAdd (n) C (n) = TrueintDx[] = {0,1,0,-1};intDy[] = {1,0,-1,0};intBFs () {priority_queue<node>Q; memset (C,false,sizeof(C)); Q.push (start);   Add (start);   Node cur,nxt; intI,nx,ny,nk;  while(Q.size ()) {cur=q.top (); Q.pop (); if(cur.x = = Tarx && cur.y = = tary)returnCUR.G;  for(i =0; I <4; i++) {NX= Cur.x +Dx[i]; NY= Cur.y +Dy[i]; if(NX >= m | | | NX <0|| NY >= N | | NY <0)Continue; NK= G[nx][ny]? cur.k+1:0; if(C[nx][ny][nk] | | NK > K)Continue; Nxt.x= NX; Nxt.y = NY; NXT.G = cur.g+1; NXT.K =nk, NXT.         H (); Q.push (NXT);      Add (NXT); }   }   return-1;}intMain () {//freopen ("In.txt", "R", stdin);   intT; inti,j; scanf ("%d",&T);  while(t--) {scanf ("%d%d%d",&m,&n,&k);  for(i =0; i < m; i++)          for(j =0; J < N; J + +) scanf ("%d", g[i]+j); Tarx= M1; tary = n1; Start.x= Start.y = START.G = START.K =0; //start. H ();printf"%d\n", BFS ()); }   return 0;}

UVA Patrol Robert

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.