作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html

题目链接:hdu 5025 Saving Tang Monk 状态压缩dp+广搜

使用dp[x][y][key][s]来记录孙悟空的坐标(x,y)、当前获取到的钥匙key和打死的蛇s。由于钥匙具有先后顺序,因此在钥匙维度中只需开辟大小为10的长度来保存当前获取的最大编号的钥匙即可。蛇没有先后顺序,其中s的二进制的第i位等于1表示打死了该蛇,否则表示没打死。然后进行广度优先搜索即可,需要注意的是即使目前没有拿到第k-1把钥匙,也可以经过房间k或唐僧,只不过无法获取钥匙k和救唐僧而已。

代码如下:

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <queue>
#include <limits.h>
#define MAXN 101
#define MAXM 10
using namespace std;
int dir[][]={{, }, {, -}, {-, }, {, }};
char map[MAXN][MAXN];
int bin[] = {, , , , , , , , , , , , };
int dp[MAXN][MAXN][][];
int n, m;
class state
{
public:
int x, y, step, key, s;
};
state start, ed;
void solve()
{
memset(dp, -, sizeof(dp));
queue<state> qu;
qu.push(start);
int res = INT_MAX;
while( qu.size() > )
{
state cur = qu.front();
qu.pop();
for( int i = ; i < ; i++ )
{
if( cur.x == ed.x && cur.y == ed.y && cur.key== m )
{
res = min(res, cur.step);
continue;
}
state next;
next.x = cur.x+dir[i][];
next.y = cur.y+dir[i][];
next.key = cur.key;
next.s = cur.s;
next.step = cur.step;
if( next.x < || next.x >= n || next.y < || next.y >= n )//出界
{
continue;
}
char tmp = map[next.x][next.y];
if( tmp == '#' )//陷阱
{
continue;
}
if( tmp >= '' && tmp <= '' && cur.key >= tmp-''- )//有钥匙
{
next.step = cur.step+;
next.key = max(cur.key, tmp - '');
next.s = cur.s;
}
else if( tmp >= 'A' && tmp <= 'F')//蛇
{
if( cur.s/bin[tmp-'A']% == )
{
next.step = cur.step + ;
}
else
{
next.step = cur.step + ;
}
next.key = cur.key;
next.s = cur.s | bin[tmp-'A'];
}
else
{
next.key = cur.key;
next.step = cur.step+;
next.s = cur.s;
}
int dptmp = dp[next.x][next.y][next.key][next.s];
if( dptmp < )
{
dp[next.x][next.y][next.key][next.s] = next.step;
qu.push(next);
}
else if(dptmp > next.step)
{
dp[next.x][next.y][next.key][next.s] = next.step;
qu.push(next);
}
}
}
if( res == INT_MAX )
{
printf("impossible\n");
return;
}
printf("%d\n", res);
}
int main(int argc, char *argv[])
{
char tmp[MAXN+];
while()
{
scanf("%d%d", &n, &m);
if( n == && m == ) return ;
int sn = ;
for( int i = ; i < n ; i++ )
{
scanf("%s", tmp);
for( int j = ; j < n ; j++ )
{
if( tmp[j] == 'K' )
{
start.x = i;
start.y = j;
start.key = ;
start.step = ;
start.s = ;
}
else if( tmp[j] == 'T' )
{
ed.x = i;
ed.y = j;
}
else if( tmp[j] == 'S' )
{
map[i][j] = 'A'+sn;
sn++;
continue;
}
map[i][j] = tmp[j];
}
}
solve();
}
}

hdu 5025 Saving Tang Monk 状态压缩dp+广搜的更多相关文章

  1. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...

  2. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  3. HDU 5025 Saving Tang Monk 【状态压缩BFS】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...

  4. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. hdu 5025 Saving Tang Monk(bfs+状态压缩)

    Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...

  6. HDU 5025 Saving Tang Monk

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  7. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  8. 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)

    /* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...

  9. HDU 5025 Saving Tang Monk --BFS

    题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...

随机推荐

  1. Genymotion - 强大好用高性能的 Android 模拟器 (在电脑流畅运行APK安卓软件游戏的利器)

    随着 Android 系统的应用和游戏越来越丰富,甚至有些比起Windows.Mac上的软件更加好用好玩,因此很多人都希望能在电脑上也能玩到安卓的游戏或APP. 我们曾推荐过 BlueStacks,而 ...

  2. 【转】phpmyadmin万能密码漏洞

    影响版本:2.11.3 / 2.11.4 利用方法:用户名处写入‘localhost’@'@”则登录成功. (注意全部是英文标点符号,最后一个为英文双引号) 附上几个php爆绝对路径的办法: phpM ...

  3. Windows Server 2012配置开机启动项

    1.运行 shell:startup 命令,如下:

  4. Print2flash在.NET(C#)64位中的使用,即文档在线预览

    转:http://www.cnblogs.com/flowwind/p/3411106.html Print2flash在.NET(C#)中的使用,即文档在线预览   office文档(word,ex ...

  5. [xml解析]rapidxml读取文件

    因为项目需要读取xml配置文件,在原来调查一番后,项目组使用了tinyxml. tinyxml确实简单,非常清楚的就把读取方案写出来了.但是,由于后期xml文件越来越大(2.5M,大概1w多行数据), ...

  6. [006]为什么C++会被叫做是C++?

    先了解一下自增和自减的运算符: 自增(++)和自减(--)操作符为对象提供加1或减1操作: int i = 0, j; j = ++i; // j = 1, i = 1: prefix yields ...

  7. java专业规划(转载)

    1. Java语言基础     谈到Java语言基础学习的书籍,大家肯定会推荐Bruce Eckel的<Thinking in Java>.它是一本写的相当深刻的技术书籍,Java语言基础 ...

  8. Android_ProgressBar

    xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  9. Sqlserver的触发器的简单使用

    1,触发器有两种 (1)After触发器(之后触发) 触发器有个好处:就是你之前有过什么操作他会将你的操作的数据信息完整的保存下来,比如你删过什么信息,如果用触发器,那么删除后就会显示两行受影响,那么 ...

  10. 练习题之CyclicBarrier与CountDownLatch

    一.CyclicBarrier使用 new Thread().start(); new Thread(new Runnable() {},"XXX").start(); publi ...