作者: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. NSDateFormatter 问题

    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init]; NSString *inputDateStr = @&quo ...

  2. windows消息钩子

    1.消息钩子的概念: Windows应用程序是基于消息驱动的,不论什么线程仅仅要注冊窗体类都会有一个消息队列用于接收用户输入的消息和系统消息.为了拦截消息,Windows提出了钩子的概念.钩子(Hoo ...

  3. C++多态的实现与局限性

    1.什么是多态? 父类指针指向子类对象,运行时期调用方法的时候,根据方法拥有者的真实类型,确定调用哪个方法. 2.如何实现多态? 要实现多态,需要加一个中间层,暴露父类的方法,内部根据指针的真实类型决 ...

  4. Android下实现GPS定位服务

    1.申请Google API Key,参考前面文章 2.实现GPS的功能需要使用模拟器进行经纬度的模拟设置,请参考前一篇文章进行设置 3.创建一个Build Target为Google APIs的项目 ...

  5. 学通javaweb 24堂课 学习笔记

    17.01:简单配置控制器 1.一个controller protected ModelAndView handleRequestInternal(HttpServletRequest request ...

  6. ExtJs 继承 和 插件 示例

    Ext.ns('Ext.ux'); function btn(){ alert(this.id); }; var panel_plugs = {//定义插件 init : function(panel ...

  7. How to install VXDIAG Honda, Toyota and JLR SDD software

    EOBD2 has newly launched Allscanner VXDIAG multi diagnostic tool with OEM diagnostic software: Honda ...

  8. Xcode常用快捷键总结

    Xcode常用快捷键 Xcode窗口快捷键 其他补充: 编译代码: command + B 将代码翻译为计算机能够识别的语言(0/1) 调试Xcode中程序: command + R 折叠与展开方法代 ...

  9. Android_listView

    package com.example.app5; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...

  10. java validator的原理与使用

    http://developer.51cto.com/art/201104/253257_1.htm ava EE 6核心特征:Bean Validation特性概述(2) 2011-04-02 14 ...