作者: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. 授予普通域用户远程桌面连接DC/客户端权限

    普通域用户通过远程桌面登录DC: 1)将该用户加入到 Remote Desktop Users 组中

  2. [置顶] 用Wireshark保存RTP的负载码流

    这段时间工作太忙,有些日子没写文章了,今天准备了一篇Wireshark工具的一个小功能,在验证码流的时候非常好用,闲话不说,直接说步骤: 1.打开Wireshark抓取流媒体码流,然后用RTP过滤: ...

  3. 学习Java设计模式的10条建议

    设计模式在整个Java的学习路线图中扮演着承上启下的作用. 在整个软件生命周期中,唯一不变的就是变化.设计模式就是要在软件设计.编码中对现有问题的一种总结,并从中寻求应对变化的策略. 自己初次接触设计 ...

  4. Android 文件读写的例子

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

  5. MFC——AfxParseURL用法

    1.功能: 该函数解析URL字符串并返回服务的类型及组件,包含在 afxinet.h 头文件中. 2.定义 BOOL AFXAPI AfxParseURL(LPCTSTRpstrURL,DWORD&a ...

  6. MySQL计划任务(事件调度器)(Event Scheduler)

    http://www.cnblogs.com/c840136/articles/2388512.html https://dev.mysql.com/doc/refman/5.7/en/events- ...

  7. 解决Windows2008Server上PLSQL登录时报ORA-12557

    公司的Oracle服务端是安装在一台Linux服务器上,版本号为11.1.0.7.0.我们开发的系统部署在Windows 2008 Server(x64),因为偶尔需要调用Oracle数据库,所以最开 ...

  8. 文件I/O(不带缓冲)之open函数

    调用open函数可以打开或创建一个文件. #include <fcntl.h> int open( const char *pathname, int oflag, ... /* mode ...

  9. vs2010问题 error MSB8008: 指定的平台工具集(v110)未安装或无效

    vs2012上的项目导入到vs2010中,编译出现 >------ 已启动生成: 项目: HelloGame.win32, 配置: Debug Win32 ------ >生成启动时间为 ...

  10. C#_delegate和事件 - 如果金额小于0则触发事件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...