hdu Swipe Bo(bfs+狀態壓縮)錯了多次的題,hdubfs+

來源:互聯網
上載者:User

hdu Swipe Bo(bfs+狀態壓縮)錯了多次的題,hdubfs+
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 except 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 a 

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. 
InputThe 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. 
OutputFor 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
 
Source2013 Multi-University Training Contest 4 

題意:有一個迷宮,包含牆、空白格子、起點S、終點E、方向格子(LRUD)和鑰匙K。要求如下:

(1)每次轉彎只能在碰到牆壁時(每次轉彎的選擇和初始時從S出發的方向選擇均稱為一次操作);

(2)對於方向格子,若到達該格子,不管周圍是不是牆,必須轉向該格子指示的方向(這個不算一次操作);

(3)若迷宮中沒有鑰匙存在,則求出S到E的最少操作次數;若有鑰匙,則必須先遍曆到每個鑰匙之後才能去E(在這個過程中可以經過E也就是E不算做障礙)。

#include<stdio.h>#include<string.h>#include<queue>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<<K[now.x][now.y]);            if(flag&&vist[now.x][now.y][now.sta])                return 0;            else if(flag) vist[now.x][now.y][now.sta]=1;  //固定方向的位置,可以直接標記            return 1;        }        else    //遇到牆,退一格,在當前位置停止        {            now.x-=dir[e][0];            now.y-=dir[e][1];            return 2;        }}int bfs(int sx,int sy){    queue<node>q;    node now,pre;    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)         for(int sta=0; sta<(1<<k); sta++)          vist[i][j][sta]=0;        now.x=sx,now.y=sy,now.sta=0,now.stp=0;        q.push(now);        vist[now.x][now.y][now.sta]=1;    while(!q.empty()){        pre=q.front(); q.pop();        pre.stp++;        for(int te=0; te<4; te++){            int e=te;            now=pre;            while(1){   //找到一個停止點,或不能走,或走過了,則跳出                now.x+=dir[e][0];                now.y+=dir[e][1];                int flag=judge1(now,e);                if(flag==0)break;                if(flag==1&&mapt[now.x][now.y]=='E'&&now.sta==(1<<k)-1){                    return now.stp;                }                if(flag==2){                    if(vist[now.x][now.y][now.sta])break;                    vist[now.x][now.y][now.sta]=1;                    q.push(now);                    break;                }            }        }    }    return -1;}int main(){    int sx,sy;    while(scanf("%d%d",&n,&m)>0){            k=0;        for(int i=0; i<n; i++){            scanf("%s",mapt[i]);            for(int j=0; j<m; j++)            if(mapt[i][j]=='S')             sx=i,sy=j;            else if(mapt[i][j]=='K')                K[i][j]=k++;        }        printf("%d\n",bfs(sx,sy));    }}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.