hiho 166 逃离迷宫

描述

小Hi正处在由 N × M 个房间组成的矩阵迷宫中。为了描述方便,我们把左上角的房间的坐标定为(0, 0),右下角房间的坐标定为(N-1, M-1)。每个房间可能是3种状态之一:开放的、关闭的、或者上锁的。

开放房间用'.'表示。小Hi可以从一个开放房间到达另一个相邻的(上下左右)开放房间。

关闭房间用'#'表示。小Hi永远不能进入一个关闭的房间。

上锁的房间用大写字母('A', 'B', 'C' ...)表示。小Hi在取得相应的钥匙前不能进入上锁的房间,而一旦取得钥匙就可以反复进入上锁的房间。每个房间的锁都是不同的,相应的钥匙在迷宫中的某一房间里,小Hi进入该房间就可以取得钥匙。

小Hi一开始处于一个开放房间,坐标(a, b)。迷宫的出口是一个开放或者上锁的房间,坐标(c, d)。假设小Hi每移动到一个相邻房间需要花费单位1的时间,那么小Hi到达出口最少需要花费多少时间?

输入
  第一行包含7个整数: N , M , K , a , b , c , d . 其中N , M是矩阵的行列数;K 是上锁的房间数目,(a, b)是起始位置,(c, d)是出口位置。(1 ≤ N, M ≤ 100, 0 ≤ K ≤ 5, 0 ≤ a, c < N, 0 ≤ b, d < M)

以下 N 行每行包含 M 个字符,表示迷宫矩阵。

再以下 K 行每行两个整数 x, y,依次表示上锁房间A , B , C ....的钥匙所在房间坐标。(0 ≤ x < N, 0 ≤ y < M)

输出
  输出到达出口的最短时间。如果小Hi永远到达不了出口,输出-1。

样例输入
  4 4 2 0 0 0 3
  .A.B
  .#..
  .#..
  .#..
  3 0
  3 3 
样例输出
  15

代码

/* ***********************************************
Author        :Tisuama
Created Time  :2017年09月09日 星期六 09时52分28秒
File Name     :hiho逃离迷宫.cpp
************************************************ */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
int n,m,k;
int a,b,c,d;
int num;
char mp[110][110];
int vis[110][110][1<<6];
int pos[10][2];
struct node{
   int x,y;
   int step;
   int status;
};
int dr[][2]={{-1,0},{1,0},{0,-1},{0,1}};
int ok(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return 0;
    if(mp[x][y]=='#')
        return 0;
    return 1;
}
int getnum(char x)
{
    return x-'A';
}
int haha(int x,int y)
{
    for(int i=0;iif(x==pos[i][0]&&y==pos[i][1])
            return i;
    return  -1;
}

int BFS()
{
    queueq;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    node st,ed;
    st.x=a;st.y=b;
    st.step=0;
    st.status=0;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(st.x==c&&st.y==d)
            return st.step;
        for(int i=0;i<4;i++)
        {
            int x=st.x+dr[i][0];
            int y=st.y+dr[i][1];
            int status=st.status;
            int step=st.step;
            if(!ok(x,y))
                continue;
            if(mp[x][y]!='.')   
            {
                num=getnum(mp[x][y]);
                if((status&(1<0)
                {
                    if(haha(x,y)!=-1)
                    {
                        if((status&(1<0)
                        {
                            status=status|(1<if(!vis[x][y][status])
                            {
                               vis[x][y][status]=1;
                               ed.x=x;ed.y=y;
                               ed.step=step+1;
                               ed.status=status;
                               q.push(ed);
                            }
                        }
                        else
                        {

                            if(!vis[x][y][status])
                            {
                               vis[x][y][status]=1;
                               ed.x=x;ed.y=y;
                               ed.step=step+1;
                               ed.status=status;
                               q.push(ed);
                            }
                        }
                    }
                    else   
                    {
                        if(!vis[x][y][status])
                        {
                            vis[x][y][status]=1;
                            ed.x=x;ed.y=y;
                            ed.status=st.status;
                            ed.step=step+1;
                            q.push(ed);
                        }
                    }
                }
            }
            else
            {
                    if(haha(x,y)!=-1) 
                    {
                        if((status&(1<0)
                        {
                            status=status|(1<if(!vis[x][y][status])
                            {
                               vis[x][y][status]=1;
                               ed.x=x;ed.y=y;
                               ed.step=step+1;
                               ed.status=status;
                               q.push(ed);
                            }
                        }
                        else
                        {

                            if(!vis[x][y][status])
                            {
                               vis[x][y][status]=1;
                               ed.x=x;ed.y=y;
                               ed.step=step+1;
                               ed.status=status;
                               q.push(ed);
                            }
                        }
                    }
                    else   
                    {
                        if(!vis[x][y][status])
                        {
                            vis[x][y][status]=1;
                            ed.x=x;ed.y=y;
                            ed.status=st.status;
                            ed.step=step+1;
                            q.push(ed);
                        }
                    }
            }
        }
    }
    return -1;
}


int main()
{
   while(~scanf("%d%d%d%d%d%d%d",&n,&m,&k,&a,&b,&c,&d))
   {
       for(int i=0;iscanf("%s",mp[i]);
       for(int i=0;iscanf("%d%d",&pos[i][0],&pos[i][1]);

       printf("%d\n",BFS());
   } 
}

你可能感兴趣的:(hiho刷题日记)