【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处理 然后就是移动的法规问题 订 ...
随机推荐
- 被我误解的max_connect_errors【转】
实为吾之愚见,望诸君酌之!闻过则喜,与君共勉 第一节 什么是max_connect_errors 一开始接触这个参数的时候,感觉他和max_connections的含义差不多,字面意思简单明了,这个 ...
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- python面向对象(六)之元类
元类 1. 类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍然成立: In [13]: class ObjectCreator(object): . ...
- 获取随机字符串的方法 GetRandomString
方法1:推荐方便. System.Hash 单元 Memo1.Lines.Add(THash.GetRandomString(50)); 方法二(自己写的): function TStrApi.Sui ...
- VFS,super_block,inode,dentry—结构体图解
总结: VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销. VFS的作用就是屏蔽各类文件系统的差异,给用户.应用程序.甚至Linux其他管理模块提供统一的接口集合. 管理VFS数据结构的组成 ...
- poj 3270(置换群+贪心)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6993 Accepted: 2754 Descr ...
- 利用Octopress在github pages上搭建个人博客
利用Octopress在github pages上搭建个人博客 SEP 29TH, 2013 在GitHub Pages上用Octopress搭建博客,需要安装ruby环境.git环境等.本人在Fed ...
- Ubuntu 使用命令更新 Ubuntu 系统
我们都知道 Ubuntu 是一款 Linux 系统,是开源的系统,随时都在更新,所以人们都说 Linux 系统要比 Windows 系统安全.那么为了我们的电脑安全,我们如何利用 Ubuntu 命令来 ...
- 【洛谷】P2000 拯救世界
题解 小迪的blog : https://www.cnblogs.com/RabbitHu/p/9178645.html 请大家点推荐并在sigongzi的评论下面点支持谢谢! 掌握了小迪生成函数的有 ...
- 三 oracle 用户管理一
一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码; ...