hdu 5094 Maze(水搜索)

  题目意思:说有一个人在(1,1) 他的目标点在(n,m) 每次是4方向的移动;

      限制条件:有的各自之间有墙 或者门,强不可通过,有对应的要钥匙可以开启这个类型的所有门;

      问题:求最少步骤数(和);

  类似于poj 2935;

    解:很明显的搜索 只要建图弄得好就非常好写:我还是 很推荐自己的建图 方法,不经常建图的可以看一看:(特别是 一个地方可以有多种钥匙,一开始我就Wa了)

  

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=50*50*(1<<10)+10;
bool flag[50][50][(1<<10)+10];
struct info
{
    int x,y,S,ans;
    info (){}
    info(int x,int y,int S,int ans):x(x),y(y),S(S),ans(ans){}
    void input()
    {
        scanf("%d%d",&x,&y);
    }
    void FLAG()
    {
        flag[x][y][S]=true;
    }
};
int n,m,p;
int Map[51][51];
int direct[51][51][4];
info que[maxn];
const int dd[][2]={1,0,-1,0  ,0,-1,0,1};
bool jude(int & x,int & y)
{
    return x>=1&&y>=1&&x<=n&&y<=m;
}
void inint()
{
    memset(direct,0,sizeof direct);
    memset(Map,0,sizeof Map);
    memset(flag,false,sizeof flag);
}
void  work()
{
    info  a,b;
    a.input();b.input();
    int key;
    scanf("%d",&key);
    for(int  i=0;i<4;i++)
    {
        if((a.x+dd[i][0]==b.x) && (a.y+dd[i][1]==b.y))
        {
            direct[a.x][a.y][i]=  (key==0 ? -1:key);
            direct[b.x][b.y][i^1]=(key==0 ? -1:key);
            break;
        }
    }

}
bool ok(const info & tmp,const int &i)
{
    if(direct[tmp.x][tmp.y][i] ==  0) return true;
    if(direct[tmp.x][tmp.y][i] == -1) return false;
    return ( 1<<(direct[tmp.x][tmp.y][i]-1) ) & tmp.S;
}
int solve()
{
    int l,r;l=r=0;
    que[r++]=info(1,1,Map[1][1],0);
    while(l<r)
    {
        info tmp=que[l++];
        if(tmp.x==n&&tmp.y==m) return tmp.ans;
        for(int i=0;i<4;i++)
        {
            if( !ok(tmp,i) ) continue;
            int x=tmp.x+dd[i][0];
            int y=tmp.y+dd[i][1];
            if(!jude(x,y)) continue;

            info t=info(x,y ,tmp.S|Map[x][y],tmp.ans+1);

            if(!flag[x][y][t.S])
            {
                que[r++]=t;
                t.FLAG();
                if(t.x==n&&t.y==m) return t.ans;
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&p))
    {
        inint();
        scanf("%d",&p);
        for(int i=1;i<=p;i++) work();
        scanf("%d",&p);
        for(int i=1;i<=p;i++){
            int x,y,key;
            scanf("%d%d%d",&x,&y,&key);
            Map[x][y]|=(1<<(key-1));
        }
        printf("%d\n",solve());
    }
    return 0;
}
时间: 2024-11-08 22:45:06

hdu 5094 Maze(水搜索)的相关文章

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 5094 Maze bfs

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

HDU 5094 --Maze【BFS &amp;&amp; 状态压缩】

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

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 Sta

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 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

HDU 4937 Lucky Number 搜索

题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大,所以直接枚举必定会超时. 下面有两种剪枝的方法: 1. 先枚举最后一位的情况. 假设数字n在base进制下表示为 a[n]...a[0],即 n = a[0] + a[1]*base^1 + ... + a[n]*base^n. 则 n - a[0] = a[1]*base^1 + ... + a[

HDU 1078 记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4575 Accepted Submission(s): 1829 Problem Description FatMouse has stored some cheese in a city. The city can be considered as a

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,