Codeforces Gym101201B:Buggy Robot(BFS + DP)
题意
给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作。
思路
和上次比赛做的一道字符串题目有点类似。
定义状态dp[x][y][d]代表在(x,y)这个点执行到了第d个操作。因此有三种情况:不变,增加,删除。
不变:就按照原来的序列走,如果走到不合法,就原地不动。转移:dp[x][y][d] = min(dp[x][y][d], dp[nx][ny][d+1])。
其实增加和删除操作是一样的,因为删除操作就相当于你执行走到另一个点,然后增加一个操作,走回来,因此可以一起讨论为增加操作。转移:dp[x][y][d] = min(dp[x][y][d], dp[nx][ny][d])。注意这里的d是不变的,因为只是增加操作,并不影响原来的操作序列。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int N = 50 + 11;
struct Node {
int x, y, d;
Node () {}
Node (int _x, int _y, int _d) : x(_x), y(_y), d(_d) {}
} ;
char mp[N][N], s[N];
int dp[N][N][2*N], dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
map<char, int> ying;
queue<Node> que;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf(" %s", mp[i] + 1);
int sx, sy, ex, ey;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(mp[i][j] == 'R') sx = i, sy = j;
else if(mp[i][j] == 'E') ex = i, ey = j;
scanf(" %s", s + 1);
int len = strlen(s + 1);
ying['D'] = 0, ying['U'] = 1, ying['R'] = 2, ying['L'] = 3;
memset(dp, INF, sizeof(dp));
Node now = Node(sx, sy, 1);
que.push(now); dp[sx][sy][1] = 0;
int dep = 0;
while(!que.empty()) {
now = que.front(); que.pop();
int x = now.x, y = now.y, d = now.d, nx, ny, nd;
if(d > dep) dep = d;
if(d <= len) {
int c = ying[s[d]];
nx = x + dx[c], ny = y + dy[c], nd = d + 1;
if(nx < 1 || nx > n || ny < 1 || ny > m || mp[nx][ny] == '#') nx = x, ny = y;
if(dp[nx][ny][nd] > dp[x][y][d])
dp[nx][ny][nd] = dp[x][y][d], que.push(Node(nx, ny, nd));
}
for(int c = 0; c < 4; c++) {
nx = x + dx[c], ny = y + dy[c], nd = d;
if(nx < 1 || nx > n || ny < 1 || ny > m || mp[nx][ny] == '#') nx = x, ny = y;
if(dp[nx][ny][nd] > dp[x][y][d] + 1)
dp[nx][ny][nd] = dp[x][y][d] + 1, que.push(Node(nx, ny, nd));
}
}
int ans = INF;
for(int i = 1; i <= dep; i++) ans = ans > dp[ex][ey][i] ? dp[ex][ey][i] : ans;
printf("%d\n", ans);
}
Codeforces Gym101201B:Buggy Robot(BFS + DP)的更多相关文章
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- ZOJ 3596Digit Number(BFS+DP)
一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...
- Codeforces Gym100502H:Clock Pictures(KMP算法)
http://codeforces.com/gym/100502/attachments 题意:有两个时钟上面有n个指针,给出的数字代表指针的角度.问能否在某一时刻使得两个时钟的指针重合. 思路:容易 ...
- Codeforces 777E:Hanoi Factory(贪心+栈)
http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
- codeforces 486 D. Valid Sets(树形dp)
题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...
- 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
\(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...
随机推荐
- 关于Qt的事件循环以及QEventLoop的简单使用(QEventLoop::quit()能够终止事件循环,事件循环是可以嵌套的)
http://www.cnblogs.com/-wang-cheng/p/4973021.html 1.一般我们的事件循环都是由exec()来开启的,例如下面的例子: 1 QCoreApplicato ...
- C++ CGI开发环境备录
1. 安装apache2: apt-get install apache2 2. 配置用户目录 在/etc/apache2/apache2.conf中配置用户目录 <Directory /hom ...
- HTTP协议学习 - 9 Method Definitions
# 前言 官方文档简略翻译.9 不是代表第九篇,而是在 RFC2616 中是第九篇.重要加粗,龟速翻译. # Method 9.3 GET The GET method means retrieve ...
- 微服务架构之「 下一代微服务 Service Mesh 」
Service Mesh 被大家称为下一代的微服务,是微服务领域的一颗新星,被大家讨论的非常多. 我在大家的讨论中,还看到有人说 “目前的微服务架构我都没学会呢,现在又来一个下一代微服务,真学不动了” ...
- kettle设计器连接oracle RAC时的连接字符串
1.不需要填写主机名2.数据库名写如下连接字符串:(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = IP地址)(PORT = 1521))(CONNEC ...
- Android 动画基础——视图动画(View Animation)
本篇讲android 3.0之前被广泛的动画框架——ViewAnimation. 目录 我将分为六部分来讲: 概述 Alpha透明动画 Rotate旋转动画 Translate位移动画 Scale放缩 ...
- win32内存调用图
https://msdn.microsoft.com/en-us/library/ms810603.aspxhttps://www.codeproject.com/Articles/14525/Hea ...
- MySQL InnoDB缓冲池(Buffer Pool)
InnoDB缓冲池并不仅仅缓存索引,它还会缓存行的数据.自适应哈希索引.插入缓冲(Insert Buffer).锁,以及其他内部数据结构. InnoDB还使用缓冲池来帮助延迟写入,这样就能合并多个写入 ...
- 网络软件,BA File,Disk,Photo,BackupTools等等(Mac版)
Auto FTP Manager 6.01Crossworld CrossFTP Enterprise v1.97.7 http://www.airexplorer.net/en/index.phpC ...
- 使用C++还是QML(QML容易使用和维护,效果好)
本质上,Qt 是一个C++类库.在引入 QML 以前,所有的开发都是基于 C++ 的,但到了 Qt 5,QML 和 Qt Quick 成为了 Qt 的核心之一,导致很多初学者在犹豫是否还需要学习 C+ ...