Hdu Swipe Bo (bfs + state compression) Error multiple times

Source: Internet
Author: User

Hdu Swipe Bo (bfs + state compression) Error multiple times
Swipe BoTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1549 Accepted Submission (s): 315



Problem Description "Swipe Bo" is a puzzle game that requires foresight and skill.
The main character of this game is a square blue tofu called Bo. we can swipe up/down/left/right to move Bo up/down/left/right. bo always moves in a straight line and nothing can stop it before t a wall. you need to help Bo find the way out.
The picture A shows that we needs three steps to swipe Bo to the exit (swipe up, swipe left, swipe down ). in a similar way, we need only two steps to make Bo disappear from the world (swipe left, swipe up )!


Look at the picture B. the exit is locked, so we have to swipe Bo to get all the keys to unlock the exit. when Bo get all the keys, the exit will unlock automatically. the exit is considered inexistent if locked. and you may notice that there are some turning signs, Bo will make a turn as soon as it meets

Turning signs. For example, if we swipe Bo up, it will go along the purple line.
Now, your task is to write a program to calculate the minimum number of moves needed for us to swipe Bo to the exit.
Input The input contains multiple cases, no more than 40.
The first line of each test case contains two integers N and M (1 ≤ N, M ≤ 200), which denote the sizes of the map. the next N lines give the map's layout, with each line containing M characters. A character is one of the following: '#': represents the wall;'s 'represents the start point of the Bo; 'E' represents the exit ;'. 'represents an empty block; 'K' represents the key, and there are no more than 7 keys in the map; 'l', 'U', 'D ', 'R' represents the turning sign with the direction of left, up, down, right.
Output For each test case of the input you have to calculate the minimal amount of moves which are necessary to make Bo move from the starting point to the exit. if Bo cannot reach the exit, output-1. the answer must be written on a single line.
Sample Input
5 6#######....#.E...#..S.##.#####5 6#######....#.....#SEK.##.#####5 6#######....#....K#SEK.##.#####5 6#######....#D...E#S...L#.#####

Sample Output
327-1

Source 2013 Multi-University Training Contest 4

There is a maze, including a wall, a blank grid, a start point S, an end point E, a direction grid (LRUD), and a key K. The requirements are as follows:

(1) Each turn can only be performed when it hits the wall (the choice of each turn and the direction selected from S at the beginning are called an operation );

(2) For the direction grid, if it reaches the grid, no matter whether the surrounding area is a wall or not, you must turn to the direction indicated by the grid (this is not an operation );

(3) If no key exists in the maze, the minimum number of operations from S to E is obtained. If a key exists, then you must traverse each key before you can go to E (in this process, you can go through E, that is, E is not a barrier ).

#include
 
  #include
  
   #include
   
    using namespace std;const int N  = 225;const int inf = 1<<29;struct node{    int x,y,sta,stp;};int n,m,k;char mapt[N][N];int K[N][N];bool vist[N][N][1<<7];int dir[4][2]={0,-1,0,1,-1,0,1,0};int judge1(node& now,int &e){    int flag=0;        if(now.x<0||now.x>=n||now.y<0||now.y>=m)            return 0;        if(mapt[now.x][now.y]!='#'){            if(mapt[now.x][now.y]=='L')                    e=0,flag=1;            else if(mapt[now.x][now.y]=='R')                        e=1,flag=1;            else if(mapt[now.x][now.y]=='U')                        e=2,flag=1;            else if(mapt[now.x][now.y]=='D')                        e=3,flag=1;             else if(mapt[now.x][now.y]=='K')                        now.sta|=(1<
    
     q;    node now,pre;    for(int i=0; i
     
      0){            k=0;        for(int i=0; i
      
       

 

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.