hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者: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+广搜的更多相关文章
- HDU 5025 Saving Tang Monk(状态转移, 广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- 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 ...
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5025 Saving Tang Monk(bfs+状态压缩)
Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Nove ...
- HDU 5025 Saving Tang Monk
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)
Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...
- 2014 网选 广州赛区 hdu 5025 Saving Tang Monk(bfs+四维数组记录状态)
/* 这是我做过的一道新类型的搜索题!从来没想过用四维数组记录状态! 以前做过的都是用二维的!自己的四维还是太狭隘了..... 题意:悟空救师傅 ! 在救师父之前要先把所有的钥匙找到! 每种钥匙有 k ...
- HDU 5025 Saving Tang Monk --BFS
题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...
随机推荐
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 找回丢失的SQL Server性能计数器
There was one time when I was delivering a Service using a tool that gathers performance data throug ...
- Java数据抓取经验【转载】
本人担任职友集的java工程师五年,其中抓取数据占主要的一部分,抓取的信息只要有两部分,职位和简历,其中职位的抓取量为日均插入量为30万,更新量 为60万,抓取全国300多个人才网站.职友集(现在改名 ...
- centos yum安装mysql
查看有没有安装包 # yum list mysql 安装mysql客户端 # yum install mysql # yum list mysql-server 安装mysql 服务器端 # yum ...
- Oracle Hints具体解释
在向大家具体介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家实用.基于代价的优化器是非常聪明的,在绝大多数情况下它会选 ...
- struts2 CRUD 入门 配置
本文介绍struts2在eclipse下的配置,实现一个具有CRUD功能的图书管理系统. 1 开发环境配置 1.1 在Eclipse中配置Struts2 1.1.1 ...
- JS与OC交互--简单使用
直接上代码 .m文件 #import "ViewController.h" @interface ViewController () <UIWebViewDelegate&g ...
- VB.NET中使用代表对方法异步调用
按照我们常规的思维方式,计算机应该是干完一件事,然后再干下一件.用术语来说,这种执行任务的方式叫做同步执行(Synchronous Execution).既然这样,那么为什么要引入异步执行的概念呢? ...
- Java基础知识强化之网络编程笔记13:TCP之TCP协议上传图片并给出反馈
1. TCP协议上传图片并给出反馈: (1)客户端: package cn.itcast_13; import java.io.BufferedInputStream; import java.io. ...
- jquery中的index方法
问题描述:灵活使用jquery中的index方法 方法签名:index([selector|element]) 用法概述:P1.index(P2) //调用者P1可以为对象或集合 参数为空,返回P1 ...