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-help Bo find the the the-the-out.
The picture A shows this we needs three steps to swipe Bo to the exit (swipe-up, swipe-left, swipe-down). In a similar-on-the-need, we have only a. Steps to make Bo disappear from the world (swipe left, swipe up)!
Look at the picture B. The exit is locked, so we had to swipe Bo to get all the keys to unlock the exit. When Bo get all the keys, the exit would unlock automatically. The exit is considered inexistent if locked. Notice that there is some turning signs, Bo would make a turn as soon as it meets a
Turning signs. For example, if we swipe Bo up, it'll go along the Purple Line.
Now, your task was 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 all test case contains, 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 all 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 is 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 that input you has to calculate the minimal amount of moves which is necessary to make Bo mov E from the starting point to the exit. If Bo cannot reach the exit, Output-1. The answer must is written on a.
Sample Input
5 6#######....#. e...#. s.##.#### #5 6#######....# ... #SEK. ##.#### #5 6#######....# .... k#sek.##.#### #5 6####### .... #D ... E#s ... l#.#####
Sample Output
327-1
Test instructions: Give a maze, did not encounter a special point, has been walking along the same direction, once encountered the wall stopped, and then can freely choose the direction of progress, urld a few is the direction of forced conversion, E is the end point, if the figure has k, then after all the K to get the key, the door will open, to leave
Idea: State compression records the status of all accesses
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 205#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8#d efine rank rank1const int mod = 10007;struct node{int x,y,step,key;}; int N,m,sx,sy,knum,kk[n][n];char G[n][n];bool Vis[n][n][4][1<<7];bool used[n][n][1<<7];int to[4][2]= { 1,0,0,1,-1,0,0,-1};int check (int x,int y) {if (x<0| | y<0| | x>=n| | Y>=M) return 1; return 0;} int BFS () {int i,j; MEM (vis,0); MEM (used,0); Queue<node> Q; Node A,next; a.x = SX; A.y= sy; A.step = 0; A.key = 0; Q.push (a); Used[a.x][a.y][0] = 1; while (! Q.empty ()) {a = Q.front (); Q.pop (); for (i = 0; i<4; i++) {int mx=to[i][0],my=to[i][1]; int x,y,k,d; x = A.x,y=a.y,k=a.key; while (1) {if (g[x][y]== ' D ') mx=1,my=0; if (g[x][y]== ' R ') Mx=-0,my=1; if (g[x][y]== ' U ') mx=-1,my=0; if (g[x][y]== ' L ') mx=0,my=-1; if (mx==1&&my==0) d=0; if (mx==0&&my==1) d=1; if (mx==-1&&my==0) d=2; if (mx==0&&my==-1) d=3; if (vis[x][y][d][k]) break; Vis[x][y][d][k] = 1; X+=mx,y+=my; if (check (x, y)) break; if (g[x] [y]== ' # ') break; if (g[x][y]== ' E ' &&k== (1<<knum)-1) return a.step+1; if (g[x][y]== ' D ') mx=1,my=0; if (g[x][y]== ' R ') Mx=-0,my=1; if (g[x][y]== ' U ') mx=-1,my=0; if (g[x][y]== ' L ') mx=0,my=-1; if (g[x][y]== ' K ') k|=kk[x][y]; if (!check (x+mx,y+my) &&g[x+mx][y+my]== ' # ') {if (Used[x][y][k]) Break Used[x][y][k] = 1; Next.x = x; Next.y = y; Next.key = k; next.step=a.step+1; Q.push (next); Break }}}} return-1;} int main () {int i,j; while (~SCANF ("%d%d", &n,&m)) {knum = 0; for (i = 0; i<n; i++) { scanf ("%s", G[i]); for (j = 0; j<m; j + +) {if (g[i][j]== ' S ') {sx=i,sy=j; } if (g[i][j]== ' K ') {kk[i][j]=1<<knum; knum++; }}} printf ("%d\n", BFS ()); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu4634:swipe Bo (bfs+-like pressure)