HDU 1429 Victory Grand Escape (continued)

Source: Internet
Author: User

Speech

Do a lot of search for the basic problem, today began to brush the search of advanced topics, become a search for the great God.

Problem analysis

The meaning of the topic I will not say that you can certainly understand, here the main solution to pick the key and open the door, pay attention here to pick up the key so before we can go back to walk, because the previous road may have not opened the door, then how to say, here to use the state compression.

State compression means that the state is represented by a number, and here I use 2 notation, for example, 1001 means I got the first key and the 4th key, 1101 means I got the first, the third, the fourth key. Therefore, the question of the judgment of the reuse of vis[20][20][1024], the first 2-dimensional representation of position coordinates, the latter one represents the number of keys obtained, the specific look at the code will understand.

#include <cstdio>#include <queue>#include <algorithm>#include <iostream>#include <cstring>using namespace STD;Const intMAXN = -;intN,m,limit;//Maze length, width, and time limitstructnode{intX,y,t,state; Node () {} node (intAintBintCintd): X (a), Y (b), T (c), state (d) {}//Construction};BOOLvis[maxn][maxn][1<<Ten];//3, the last dimension to collect the number of keys, a total of 9 keys, so need to 1<<10;CharMAZE[MAXN][MAXN];intdir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};intBFsintSxintSY) { queue <Node>Que Node Pre;memset(Vis,false,sizeof(VIS)); Que.push (SX, SY,0,0)); vis[sx][sy][0] =1;intState while(!que.empty ()) {pre = Que.front (); Que.pop ();if(Maze[pre.x][pre.y] = =' ^ ')//Search to the end{if(pre.t >= limit)//exceeding limit                return-1;Else                returnPRE.T; } for(inti =0; I <4; i++) {intxx = pre.x + dir[i][0];intyy = Pre.y + dir[i][1];if(XX <0|| XX >= N | | yy <0|| yy >= m | | MAZE[XX][YY] = =' * ')//Judging if it is out of bounds and whether it is a wall                Continue;if(Maze[xx][yy] <=' Z '&& Maze[xx][yy] >=' A ')//Maze This position is the key{state = pre.state| (1<< (Maze[xx][yy]-' A '));//Pick up the key, which is 2 binary and |                if(!vis[xx][yy][state]) {Vis[xx][yy][state] =1; Que.push (Node (xx, yy, pre.t+1, state)); }            }Else if(Maze[xx][yy] <=' Z '&& Maze[xx][yy] >=' A ')//Maze the position for the door, note that as long as you have the key, the door can always be opened{state = pre.state& (1<< (Maze[xx][yy]-' A '));//Whether the key is 2 in the binary and, if not, the value is 0                if(State &&!vis[xx][yy][pre.state]) {Vis[xx][yy][pre.state] =1; Que.push (Node (xx,yy,pre.t+1, pre.state)); }            }Else if(!vis[xx][yy][pre.state])//Maze here for road, start, or end{Vis[xx][yy][pre.state] =1; Que.push (Node (xx, yy, pre.t+1, pre.state)); }        }    }return-1;}intMain () { while(scanf(" %d%d%d", &n, &m, &limit)! = EOF) { for(inti =0; I < n; i++)scanf('%s ', Maze[i]);intSx,sy; for(inti =0; I < n; i++) for(intj =0; J < M; J + +)if(Maze[i][j] = =' @ 'SX = i, sy = j;printf("%d\n", BFS (SX, SY)); }return 0;}

HDU 1429 Victory Grand Escape (continued)

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.