转载自: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≤ 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的更多相关文章

  1. bzoj 3875 骑士游戏 - spfa - 动态规划

    Description  [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,J ...

  2. NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序

    原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...

  3. CCF地铁修建

    问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁. 地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通 ...

  4. Bzoj1486/洛谷P3199 最小圈(0/1分数规划+spfa)/(动态规划+结论)

    题面 Bzoj 洛谷 题解(0/1分数规划+spfa) 考虑\(0/1\)分数规划,设当前枚举到的答案为\(ans\) 则我们要使(其中\(\forall b_i=1\)) \[ \frac{\sum ...

  5. BZOJ3875--骑士游戏(SPFA处理带后效性的动态规划)

    3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 181  Solved: 91[Submit][Status] ...

  6. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  7. 【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans

    预处理cost[a][b] 表示第a天到第b天用同一条线路的成本. 具体转移看代码. #include<cstdio> #include<algorithm> #include ...

  8. 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠

    <法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...

  9. BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA

    标题效果:给定一个无向图.输送n日,有一天的某一时刻不能去,更换行考虑k,求总成本 一阶cost[i][j]用于第一i为了天j天正在同一航线的最低消费 这种利用SPFA处理 然后就是移动的法规问题 订 ...

随机推荐

  1. shell变量$#,$@,$0,$1,$2的含义

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  2. bind系统调用

    /* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...

  3. Linux设备驱动程序学习 高级字符驱动程序操作[阻塞型I/O和非阻塞I/O]【转】

    转自:http://blog.csdn.net/jacobywu/article/details/7475432 阻塞型I/O和非阻塞I/O 阻塞:休眠 非阻塞:异步通知 一 休眠 安全地进入休眠的两 ...

  4. oracle用plsql查询死锁

    1. 点击plsql 工具(tool),点击会话(session) 2.点击锁,可以看到锁的session

  5. Vue 3.0 的生命周期

    new Vue() new一个vue实例化对象 init Event & Lifecycle 执行一些初始化和生命周期相关的操作 beforeCreate 组件实例刚刚被创建出来 执行一些初始 ...

  6. Django中的QuerySet

    一.QuerySet 查询集,类似一个列表,包含了满足查询条件的所有项.QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只有你查询的时候才真正的操作数据库.意味 ...

  7. 微信小程序入坑之自定义组件

    前言 最近接触微信小程序,再次之前公司用的前端框架是vue ,然后对比发现,开发小程序是各种限制,对于开发者非常不友好.各种槽点太多,完全吐槽不过来,所以在此不多说,打算下次专门写一篇文章吐槽一下.本 ...

  8. 深度学习在美团点评推荐平台排序中的应用&& wide&&deep推荐系统模型--学习笔记

    写在前面:据说下周就要xxxxxxxx, 吓得本宝宝赶紧找些广告的东西看看 gbdt+lr的模型之前是知道怎么搞的,dnn+lr的模型也是知道的,但是都没有试验过 深度学习在美团点评推荐平台排序中的运 ...

  9. java 多线程总结篇2之——Thread类及常用函数

    此片文章主要总结的是Thread类及相关的基础概念和API,首先需要厘清线程调度中的几个基本概念: 一.线程调度的基本方法 1.调整线程优先级:Java线程有优先级,优先级高的线程会获得较多的运行机会 ...

  10. Codeforces 264B Good Sequences(DP+素数筛)

    题目链接:http://codeforces.com/problemset/problem/264/B 题目大意:给出n个单调递增的数,让你找出最长的好序列,好序列是一种单调递增的并且相邻元素的最大公 ...