hdu4634 状压bfs

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




玛德   我现在特别想骂人      这道题真特么的坑爹   坑爹 坑爹    航电你就不能吧数据搞的好一点么        写了我几乎整整一天

我也不想多说什么了     真的   让我静静    这时间浪费的太不值了…………



ac code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

char g[220][220];
int sx,sy,ex,ey;
int row,col,keynum;
bool flag[202][202][1<<7][4];
int mve[][2] = {{-1,0},{0,-1},{1,0},{0,1}};
struct node
{
    int key,num;
    int x,y;
    node(){}
    node(int _x,int _y,int change=0,int status=0):x(_x),y(_y),key(status),num(change) {}
};
int bfs()
{
    queue<node> que;
    node tmp,now;
    que.push(node(sx,sy));
    memset(flag,false,sizeof(flag));
    while(!que.empty())
    {
        tmp = que.front();
        que.pop();
        for(int i = 0; i < 4; i++)
        {
            int x = tmp.x;
            int y = tmp.y;
            int ss = tmp.key;
            int dir=i;
            while(1)
            {
                if(g[x][y] =='L') dir=1;
                if(g[x][y] == 'U') dir=0;
                if(g[x][y] == 'D') dir=2;
                if(g[x][y] == 'R') dir=3;
                if(flag[x][y][ss][dir])break;
                flag[x][y][ss][dir] = true;
                if(g[x][y]>='0'&&g[x][y]<'8')
                 ss |= (1<<(g[x][y]-'0'));
                if( x == ex && y== ey && ss ==keynum)
                    return tmp.num+1;
                if(x+mve[dir][0]>=0 && x+mve[dir][0]<row&& y+mve[dir][1]>=0 && y+mve[dir][1]<col)
                    if( g[x+mve[dir][0]][y+mve[dir][1]]=='#' )
                    {
                        que.push(node(x,y,tmp.num+1,ss));
                        break;
                    }
                    else
                    {
                        x+=mve[dir][0];
                        y+=mve[dir][1];
                    }
                else  break;
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d",&row,&col)!=EOF)
    {
        keynum = 0;
        for(int i = 0; i < row; i++)
        {
            scanf("%s",g[i]);
            for(int j = 0; j < col; j++)
                if(g[i][j] == 'S')
                    sx = i,sy = j;
                else if(g[i][j] == 'E')
                    ex = i,ey = j;
                else if(g[i][j] == 'K')
                    g[i][j]='0'+keynum++;
        }
        keynum=(1<<keynum)-1;
        printf("%d\n",bfs());
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值