【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处理 然后就是移动的法规问题 订 ...
随机推荐
- SolrJ查询条件组合查询实现——(十六)
带查询条件的实现原理: 查询按钮被包在一个大表单,表单还有三个隐藏域,一个商品筛选,一个 价格,一个排序,每次点击查询时候清空三个隐藏域,就带着一个大条件去查询;点击下面的筛选条件时,给隐藏域的筛选条 ...
- 【codeforces】【比赛题解】#872 CF Round #440 (Div.2)
链接. [A]寻找漂亮数字 题意: 给定了两列非零数字.我们说一个数是漂亮的,当它的十进制表达中有至少一个数从数列一中取出,至少有一个数从数列二中取出.最小的漂亮数字是多少? 输入: 第一行两个数\( ...
- Shell-输入密码转换为*
Code: read -p "请输入使用者都名称:" USER echo -e "请输入使用者密码: \c" while : ;do char=` #这里是反引 ...
- 一步一步搭建oracle 11gR2 rac+dg之环境准备(二)【转】
一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之环境准备 (二) 一步一步搭建oracle 11gR2 rac+dg之环境准备 ...
- 如何提高单片机Flash的擦写次数
所谓提高flash的擦写次数,并不是真正的提高flash擦写次数,而是通过以"空间换时间"概念,在软件上实现“操作的次数大于其寿命”.详见链接: http://bbs.eeworl ...
- sql loader 控制文件使用十六进制分隔符
最近项目中使用到了sql loader加载数据文件至数据库,提供的文件中使用了十六进制 7F5E 分隔符,在sql loader中如何加载呢? 经过查询实验后,控制文件ctl内容如下: load da ...
- 02 Go 1.2 Release Notes
Go 1.2 Release Notes Introduction to Go 1.2 Changes to the language Use of nil Three-index slices Ch ...
- git内部原理
Git 内部原理 无论是从之前的章节直接跳到本章,还是读完了其余章节一直到这——你都将在本章见识到 Git 的内部工作原理 和实现方式. 我们发现学习这部分内容对于理解 Git 的用途和强大至关重要. ...
- MySQL触发器Trigger实例篇
定义: 何为MySQL触发器? 在MySQL Server里面也就是对某一个表的一定的操作,触发某种条件(Insert,Update,Delete 等),从而自动执行的一段程序.从这种意义上讲触发器是 ...
- linux 安装 Elasticsearch6.4.0详细步骤以及问题解决方案
1.jdk 安装 参考资料:https://www.cnblogs.com/shihaiming/p/5809553.html 2.elasticsearch 安装 下载:https://artifa ...