【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处理 然后就是移动的法规问题 订 ... 
随机推荐
- 【划水闲谈】Terraria 1.3.5更新
			我知道这本应是一个算法博客,但又有谁规定了不能发点其他内容呢? Terraria,一个有趣的沙盒游戏.在这里,你可以建造,挖掘,开始一次又一次新的冒险. 4月19日,Re-Logic承诺的官方中文版终 ... 
- Redis—数据结构之sds
			Redis是一个Key Value数据库.Redis有5种数据类型:字符串.列表.哈希.集合.有序集合.而字符串的底层实现方法之一就是使用sds.以下描述中请读者注意区分sds是指简单动态字符串这一数 ... 
- 查看linux服务器内存信息
			查看服务器内存信息 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size [root@localhost home]# dmideco ... 
- spring单元测试的基本配置
			@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:trade.ap ... 
- MySQL 删除数据
			删除数据的语句有三种:DELETE.DROP.TRUNCATE. 1.DELETE语句 DELETE 语句用于删除表中的行. 语法 DELETE FROM 表名称 WHERE 列名称 = 值 例如 - ... 
- http请求与传参
			这并不算是文章,暂时只做粗略地记录,以免忘记,因此会显得杂乱无章,随便抓了几个包和对postman截图,日后有空再完善 1.get方式 只有一种方式,那就是在url后面跟参数 2.post方式 1)表 ... 
- Flask: Quickstart解读
			Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 从示例代码说起: from flask import Flask app = Flask(__name__) @app ... 
- java基础42 File类的构造函数及其方法
			本文知识点(目录): 1.IO流(Input Output) 2.IO流技术的运用场景 3.File类的构造函数(方法) 4.File类的一些常用方法 5.实例(解析File类 ... 
- java基础21 System类和Runtime类
			一.System系统类 1.1.System系统类 主要用于获取系统信息 1.2.System类的常用方法 arraycopy(Object src, int srcPos, Object dest, ... 
- (一)SpringMVC
			第一章 问候SpringMVC 第一节 SpringMVC简介 SpringMVC是一套功能强大,性能强悍,使用方便的优秀的MVC框架 下载和安装Spring框架: 登录http://repo.spr ... 
