UVa Patrol Robot (exercise 6-5)

Source: Internet
Author: User

Portal: Https://uva.onlinejudge.org/external/16/1600.pdf

Multi-State wide search

Online puzzle: To the VIS array plus one-dimensional state, indicating the number of times left to traverse the wall, each encounter wall, the current K minus 1, hit 0, the current K into the original K.

Vis[x][y][z] x, y for coordinates z means K when True indicates that the point of the remaining number of times the wall is K has already appeared

1#include <bits/stdc++.h>2 using namespacestd;3 4typedefstructnode{5     intx, y, step, K;6 } node;7 Const intMAXN = -;8 Const intINF =0x3f3f3f3f;9 Const intCx[] = {-1,1,0,0};Ten Const intCy[] = {0,0, -1,1}; One intN, M, K, ans; A intMAZE[MAXN][MAXN]; - intVIS[MAXN][MAXN][MAXN]; -  the voidBFs () { -Queue<node>Q; -      while(!q.empty ()) Q.pop (); - node cur; +cur.x = Cur.y =1; -Cur.step =0; +CUR.K =K; A Q.push (cur); atVIS[CUR.X][CUR.Y][CUR.K] =1; -      while(!Q.empty ()) { -Cur =Q.front (); - Q.pop (); -         intx = cur.x, y =cur.y; -         if(x = = N && y = =m) { inAns =Cur.step; -             return ; to         } + node now; -         if(CUR.K >=0){ the              for(inti =0; I <4; ++i) { *now.x = x + cx[i], now.y = y +Cy[i]; $Now.step = Cur.step +1;Panax NotoginsengNOW.K = Maze[now.x][now.y]? CUR.K-1: K; -                 if(Now.x <1|| N < now.x | | Now.y <1|| M < NOW.Y | | VIS[NOW.X][NOW.Y][NOW.K])Continue; the                 if(NOW.K >=0){ +VIS[NOW.X][NOW.Y][NOW.K] =1; A Q.push (now); the                 } +             } -         } $     } $ } -  - intMain () { the     intT; -CIN >>T;Wuyi      while(t--){ theCIN >> n >> M >>K; -memset (Vis,0,sizeof(Vis)); WuMemset (Maze,0,sizeof(Maze)); -          for(inti =1; I <= N; ++i) About              for(intj =1; J <= M; ++j) $CIN >>Maze[i][j]; -Ans =-1; - BFS (); -cout << ans <<Endl; A     } +     return 0; the}

UVa Patrol Robot (exercise 6-5)

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.