HDU 4634 Swipe Bo (2013多校4 1003 搜索)

Swipe Bo

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 455    Accepted Submission(s): 111

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.  

 

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 3 2 7 -1  

 

Source
2013 Multi-University Training Contest 4  

 

Recommend zhuyuanchen520  

 

 

 

 

我是暴力搜索搞的,调试了一下竟然AC了。

 

注意判重。

走的过程中也要去判重。

有好几个细节

/*
 *  Author:kuangbin
 *  1003.cpp
 */

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;

char g[220][220];
int sx,sy,ex,ey;
int n,m;
int keynum;
int key_s[220][220];
int move[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
    int key;//钥匙的状态
    int num;//移动数
    int x,y;
};
queue<Node>q;
bool used[202][202][1<<7];
bool used2[202][202][1<<7][4];
int bfs()
{
    while(!q.empty())q.pop();
    Node tmp,now;
    tmp.key = 0;
    tmp.num = 0;
    tmp.x = sx;
    tmp.y = sy;
    q.push(tmp);
    memset(used,false,sizeof(used));
    memset(used2,false,sizeof(used2));
    used[sx][sy][0] = true;
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        for(int i = 0;i < 4;i++)
        {
            int mx = move[i][0];
            int my = move[i][1];
            int x = tmp.x;
            int y = tmp.y;
            int ss = tmp.key;
            while(1)
            {
                if(g[x][y] =='L')
                {
                    mx = 0; my = -1;
                }
                if(g[x][y] == 'U')
                {
                    mx = -1;my = 0;
                }
                if(g[x][y] == 'D')
                {
                    mx = 1;my = 0;
                }
                if(g[x][y] == 'R')
                {
                    mx = 0; my = 1;
                }
                int dir;
                if(mx==-1&&my==0)dir=0;
                else if(mx==1&&my==0)dir=1;
                else if(mx==0&&my==1)dir=2;
                else if(mx==0&&my==-1)dir=3;
                if(used2[x][y][ss][dir])break;
                used2[x][y][ss][dir] = true;
                x += mx;
                y += my;
                if(x < 0 || y < 0 || x >= n || y >= m)break;
                if(g[x][y] =='#')break;
                if( x == ex && y== ey && ss ==((1<<keynum)-1) )
                    return tmp.num+1;
                if(g[x][y] =='L')
                {
                    mx = 0; my = -1;
                }
                if(g[x][y] == 'U')
                {
                    mx = -1;my = 0;
                }
                if(g[x][y] == 'D')
                {
                    mx = 1;my = 0;
                }
                if(g[x][y] == 'R')
                {
                    mx = 0; my = 1;
                }
                if(g[x][y] == 'K')
                    ss |= key_s[x][y];
                if(x+mx >=0 && x+mx < n && y+my>=0 && y+my < m && g[x+mx][y+my]=='#')
                {
                    if(used[x][y][ss])break;
                    now.x = x;now.y = y;
                    now.key = ss;
                    now.num = tmp.num + 1;
                    q.push(now);
                    used[x][y][ss] = true;
                    break;
                }
            }
        }
    }
    return -1;
}


int main()
{
    //freopen("1003.in","r",stdin);
   // freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&m)==2)
    {
        keynum = 0;
        for(int i = 0;i < n;i++)
        {
            scanf("%s",g[i]);
            for(int j = 0;j < m;j++)
            {
                if(g[i][j] == 'S')
                {
                    sx = i;sy = j;
                }
                if(g[i][j] == 'E')
                {
                    ex = i;ey = j;
                }
                if(g[i][j] == 'K')
                {
                    key_s[i][j] = (1<<keynum);
                    keynum++;
                }
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3234126.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞