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-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
Source2013 multi-university Training Contest 4
Test instructions: There is a maze that contains walls, blank squares, starting s, end points E, direction lattices (LRUD) and key K. Requirements are as follows:
(1) Each turn can only be encountered when the wall (the choice of each turn and starting from the direction of the start of the choice is called an operation);
(2) for the direction of the lattice, if the grid, regardless of the surrounding is not a wall, must turn to the direction of the lattice indicator (this does not count as an operation);
(3) If there is no key in the maze, then the minimum number of operations S to E is found; If you have a key, you must walk through each key before you can go to E (in this process, E is not a barrier).
#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; Position in a fixed direction,You can mark return 1 directly; } else//encounter wall, back one, stop at current position {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++) Vis t[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) {//Find a stop point, or can't walk, or walk through, then jump out of 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)); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU Swipe Bo (bfs+ State compression) Many wrong questions