hdu 5094 Maze (bfs+状态压缩)

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)

Total Submission(s): 173    Accepted Submission(s): 71

Problem Description

This story happened on the background of Star Trek.

Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS.

The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly.

The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs
1 second. And he is able to move to next location if and only if:

Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions)

Open door is passable, but locked door is not.

Kirk cannot pass a wall

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.

Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.

Input

The input contains many test cases.

Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10).

Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500).

There are 5 integers in the following k lines, represents xi1, yi1, xi2, yi2, gi; when gi >=1, represents there is a gate of type gi between location (xi1, yi1) and (xi2,
yi2); when gi = 0, represents there is a wall between location (xi1, yi1) and (xi2, yi2), ( | xi1 - xi2 | + | yi1 - yi2 |=1, 0<= gi <=p
)

Following line is an integer S, represent the total number of keys in maze. (0<= S <=50).

There are three integers in the following S lines, represents xi1, yi1 and qi respectively. That means the key type of qi locates on location (xi1, yi1), (1<= qi<=p).

Output

Output the possible minimal second that Kirk could reach Spock.

If there is no possible plan, output -1.

Sample Input

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1

Sample Output

14

题意:给出n*m矩阵和p中门的类型(最多10种),其中有k个障碍,两坐标之间存在墙或门

给出s个钥匙位置及编号,相应的钥匙开相应的门(有可能同一个位置有多个钥匙)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
const int maxn = 55 ;
const int N = 11 ;

struct node
{
    int x,y;
    int step,sta;
    node() {}
    node(int _x,int _y,int _step,int _sta): x(_x),y(_y),step(_step),sta(_sta) {}
};

bool visited[maxn][maxn][1<<N];
int a[maxn][maxn][maxn][maxn],key[maxn][maxn];
int n,m,p;

void initial()
{
    memset(visited,0,sizeof(visited));
    memset(a,-1,sizeof(a));
    memset(key,0,sizeof(key));
}

void input()
{
    int k,s,x,y,dx,dy,g;
    scanf("%d",&k);
    for(int i=0;i<k;i++)
    {
         scanf("%d %d %d %d %d",&x,&y,&dx,&dy,&g);
         a[x][y][dx][dy]=g;
         a[dx][dy][x][y]=g;
    }
    scanf("%d",&s);
    for(int i=0;i<s;i++)
    {
         scanf("%d %d %d",&x,&y,&g);
         int t=1<<(g-1);
         if((key[x][y] & t)==0)   key[x][y] = key[x][y] | t;
    }
}

bool judge(int xx,int yy)
{
    if(xx>=1 && xx<=n && yy>=1 && yy<=m)  return true;
    return false;
}

int bfs()
{
     queue <node> q;
     q.push(node(1,1,0,key[1][1]));
     visited[1][1][key[1][1]]=1;
     while(!q.empty())
     {
          node c=q.front(); q.pop();
          if(c.x==n && c.y==m)  return c.step;
          for(int i=0;i<4;i++)
          {
               int xx=c.x+d[i][0],yy=c.y+d[i][1],sp = c.sta | key[xx][yy];
               int t=a[c.x][c.y][xx][yy];
               int w=1<<(t-1);
               if(judge(xx,yy) && ((t==-1) || (t!=0 && (c.sta & w))) && !visited[xx][yy][sp])
               {
                   visited[xx][yy][sp]=1;
                   q.push(node(xx,yy,c.step+1,sp));
               }
          }
     }
     return -1;
}

int main()
{
    while(scanf("%d %d %d",&n,&m,&p)!=EOF)
    {
        initial();
        input();
        printf("%d\n",bfs());
    }
    return 0;
}

题意:给出n*m矩阵和p中门的类型(最多10种),其中有k个障碍,两坐标之间存在墙或门

给出s个钥匙位置及编号,相应的钥匙开相应的门(有可能同一个位置有多个钥匙)。

时间: 2024-11-13 09:00:39

hdu 5094 Maze (bfs+状态压缩)的相关文章

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

hdu 5094 Maze bfs

传送门:上海邀请赛E 给定一个n×m的迷宫,给出相邻格子之间的墙或者门的信息,墙说明不可走,如果是门则需要有对应的钥匙才能通过,问是否能够从(1,1)到达(n,m) 一个带状态的bfs,再另记一个状态表示所带钥匙的种类,钥匙种类数最多只有10,因此可以用位来表示钥匙的状态. /****************************************************** * File Name: 5094.cpp * Author: kojimai * Create Time: 2

hdu 3001 Travelling (bfs+状态压缩)

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3905    Accepted Submission(s): 1234 Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is

hdu 1429 (bfs+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5283    Accepted Submission(s): 1815 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了

HDU 1885Key Task(bfs + 状态压缩)

Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1529 Accepted Submission(s): 631 Problem Description The Czech Technical University is rather old - you already know that it celebrates 300

hdu 5094 Maze (BFS+状压)

题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi.gi=0:墙   1,2,3.... :第1种门,第2种门,第3种门..... 有S把钥匙.给出S个信息:x1,y1,qi    含义是位置(x1,y1)上有一把第qi种的钥匙. 问多多最少花多少秒到达(n,m).若无法到达输出-1. 数据范围: (1<= n, m <=50, 0<= p

hdu 1429(BFS+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8379    Accepted Submission(s): 3008 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)--这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 901    Accepted Submission(s): 314 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.能够优化非常多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> us