【spfa】【动态规划】zoj3847 Collect Chars
转载自:http://blog.csdn.net/madaidao/article/details/42616743
Collect Chars
Time Limit: 2 Seconds Memory Limit: 65536 KB
Bob was playing MC and was punished by Alice. Bob was trapped in a maze and there were some characters on some specific cells. When he walks on a specific point, he will pick up the character automatically (he cannot refuse to do that) and push back the character in his bag (You can treat it as adding a character to the end of a string and initially, the string is empty.). Alice has set several goal strings and when Bob's bag contains any one of the strings, Bob is enabled to return to the true world.
Bob can walk to the adjacent cell (up, down, left, right) at one time, and it costs no time to pick up the character and the character won't disappear, which means if Bob want's he can get endless characters once he walked on the certain cell but he must pick up at least one character.
Bob wants to leave the maze as soon as possible, and he asks you for the shortest time that he can go out of that maze.
Input
The first line is an integer n, indicating the number of testcases.
In each case, the first line is two integers L and C (0 < L, C ≤ 20). After this, L lines follow, and each has Ccharacters. For each character, '.' stands for an open space, '#' stands for a wall which cannot be crossed, '@' stands for the beginning point of the maze. All capital letters 'A' to 'Z' stand for the characters which can be picked up.
After that, there is one integer W, which means the number of goal strings (0 < W ≤ 200). And W lines following, each line has a string, containing letters 'A' to 'Z' only, which length is less than 100.
Output
For each case, you should output an integer. The answer is the minimum time that Bob must use to get out of the maze. If he can't do that, you should output -1.
Sample Input
2
5 5
A.DB#
@#.##
.#..C
.#...
.....
3
AB
AC
BC
5 5
A.DB#
@#.##
.#..C
.#...
.....
2
AADBBBDCC
AC
Sample Output
11
9
题意:BOb在一个L*C的格子迷宫中,有些特别的格子里面有一个大写字母,每次Bob走到这个格子,至少要添加一个该字母到包里,相当于添加一个字母到字符串的末尾,初始的时候字符串为空。可以添加无数个(字母拿了不会消失,拿字母不花时间)。Alice有一些字符串,若有一个字符串为Bob包里的串的子串,Bob就可以走出迷宫。求Bpb走出迷宫的最小时间。
题解:枚举Alice的串,求出靠每个Alice的串走出迷宫的最小时间。对于每个串s,我们用dp[i][j][k] 表示当前在点(i,j),背包里的串的长度为j的后缀和s的长度为k的前缀相同,走到该状态的最短时间。用spfa的方法转移即可。代码如下:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 110000
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
int c,l;
int w;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
char tu[50][50];
LL dp[50][50][1100];
bool inque[50][50][1100];
struct node
{
int x,y,zt;
node(){}
node(int xx,int yy,int zzt)
{
x=xx,y=yy,zt=zzt;
}
}sta,tem;
queue<node>que;
void gengxin(int x,int y,int zt,int val)
{
if(dp[sta.x][sta.y][sta.zt]+val<dp[x][y][zt])
{
dp[x][y][zt]=dp[sta.x][sta.y][sta.zt]+val;
if(!inque[x][y][zt])
{
inque[x][y][zt]=true;
que.push(node(x,y,zt));
}
}
}
LL solve(string s)
{
int ls=s.size();
int i,j,k;
for(i=0;i<c;i++)
{
for(j=0;j<l;j++)
{
for(k=0;k<=ls;k++)
{
inque[i][j][k]=false;
dp[i][j][k]=inf64;
}
}
}
while(que.size())
que.pop();
for(i=0;i<c;i++)
{
for(j=0;j<l;j++)
{
if(tu[i][j]=='@')
{
dp[i][j][0]=0;
inque[i][j][0]=true;
que.push(node(i,j,0));
}
}
}
int x,y;
LL re=inf64;
while(que.size())
{
sta=que.front();
que.pop();
inque[sta.x][sta.y][sta.zt]=false;
if(sta.zt==ls)
{
re=min(re,dp[sta.x][sta.y][sta.zt]);
continue;
}
if(tu[sta.x][sta.y]==s[sta.zt])
{
gengxin(sta.x,sta.y,sta.zt+1,0);
}
for(i=0;i<4;i++)
{
x=sta.x+dir[i][0];
y=sta.y+dir[i][1];
if(x>=0&&x<c&&y>=0&&y<l)
{
if(tu[x][y]==s[sta.zt])
{
gengxin(x,y,sta.zt+1,1);
}
if(tu[x][y]=='@'||tu[x][y]=='.')
{
gengxin(x,y,sta.zt,1);
}
if(tu[x][y]>='A'&&tu[x][y]<='Z'&&tu[x][y]!=s[sta.zt])
{
gengxin(x,y,0,1);
}
}
}
}
return re;
}
char sr[1100];
int main()
{
int t,i;
string s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&c,&l);
for(i=0;i<c;i++)
{
scanf("%s",tu[i]);
}
scanf("%d",&w);
getchar();
LL ans=inf64;
for(i=1;i<=w;i++)
{
gets(sr);
s=sr;
ans=min(ans,solve(s));
}
if(ans==inf64)
printf("%d\n",-1);
else
printf("%lld\n",ans);
}
return 0;
}
【spfa】【动态规划】zoj3847 Collect Chars的更多相关文章
- bzoj 3875 骑士游戏 - spfa - 动态规划
Description [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,J ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- CCF地铁修建
问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁. 地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通 ...
- Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)
题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...
- BZOJ3875--骑士游戏(SPFA处理带后效性的动态规划)
3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 181 Solved: 91[Submit][Status] ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- 【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans
预处理cost[a][b] 表示第a天到第b天用同一条线路的成本. 具体转移看代码. #include<cstdio> #include<algorithm> #include ...
- 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠
<法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...
- BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA
标题效果:给定一个无向图.输送n日,有一天的某一时刻不能去,更换行考虑k,求总成本 一阶cost[i][j]用于第一i为了天j天正在同一航线的最低消费 这种利用SPFA处理 然后就是移动的法规问题 订 ...
随机推荐
- No.6 selenium学习之路之下拉框Select
HTML中,标签显示为select,有option下拉属性的为Select弹框 1.Xpath定位 Xpath语法,顺序是从1开始,编程语言中是0开始
- 使用html+css+js实现魔性的舞蹈
使用html+css+js实现魔性的舞蹈,让我们燥起来!!! 效果图: 代码如下,复制代码即可使用: <!DOCTYPE html> <html > <head> ...
- TImage 显示 资源中 的图片、TResourceStream、资源文件
unit Unit5; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- DevExpress 去除皮肤的方法
我从不用皮肤,方法如下:
- dpr 与 dproj 有什么区别
- EFK收集Kubernetes应用日志
本节内容: EFK介绍 安装配置EFK 配置efk-rbac.yaml文件 配置 es-controller.yaml 配置 es-service.yaml 配置 fluentd-es-ds.yaml ...
- Kubernetes1.6集群上(开启了TLS)安装Dashboard
本节内容: 配置dashboard 执行所有定义的文件 检查执行结果 访问dashboard 这是接着上一篇<二进制方式部署Kubernetes 1.6.0集群(开启TLS)>写的.Kub ...
- JavaScript工程师都应懂的33个概念
最近福利发的有点多啊,各种硬干货,小伙伴们是不是觉得很爽啊.Github真的蕴含着各种各样的宝藏,难怪各个大厂也都纷纷贡献自己的代码到Github上. 所以各种干货还是会源源不断的po给大家,觉得有帮 ...
- Metronic 5.0.5 bootstrap后台管理模板
演示地址:http://keenthemes.com/preview/metronic/ 下载 Dashboard Table
- SQL group 分组查询
1.使用group by进行分组查询 在使用group by关键字时,在select列表中可以指定的项目是有限制的,select语句中仅许以下几项: 被分组的列 为每个分组返回一个值得表达式,例如 ...