题目链接

题意

给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作。

思路

和上次比赛做的一道字符串题目有点类似。

定义状态dp[x][y][d]代表在(x,y)这个点执行到了第d个操作。因此有三种情况:不变,增加,删除。

  1. 不变:就按照原来的序列走,如果走到不合法,就原地不动。转移:dp[x][y][d] = min(dp[x][y][d], dp[nx][ny][d+1])。

  2. 其实增加和删除操作是一样的,因为删除操作就相当于你执行走到另一个点,然后增加一个操作,走回来,因此可以一起讨论为增加操作。转移: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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  4. ZOJ 3596Digit Number(BFS+DP)

    一道比较不错的BFS+DP题目 题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数. 很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方 ...

  5. Codeforces Gym100502H:Clock Pictures(KMP算法)

    http://codeforces.com/gym/100502/attachments 题意:有两个时钟上面有n个指针,给出的数字代表指针的角度.问能否在某一时刻使得两个时钟的指针重合. 思路:容易 ...

  6. Codeforces 777E:Hanoi Factory(贪心+栈)

    http://codeforces.com/problemset/problem/777/E 题意:给出n个环状圆柱,每个圆环有一个内半径a,外半径b,和高度h,只有外半径bj <= bi并且b ...

  7. Codeforces 758C:Unfair Poll(思维+模拟)

    http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...

  8. codeforces 486 D. Valid Sets(树形dp)

    题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...

  9. 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)

    \(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...

随机推荐

  1. Swift - 关于 Optional 的一点唠叨

    Optional 是 Swift 的一个非常重要的特性,它除了提供类型安全的机制,也是 Swift 中很多语言特性的核心.当然,使用 Optional 时也要了解很多坑,这样能帮助我们更好的运用它. ...

  2. Xamarin 设置可接受的版本

    一共分三个版本,编译版本.最小版本.目标版本(最适应) 一般编译使用最新的版本,目标版本选择最主流的 参考资料 https://docs.microsoft.com/en-us/xamarin/and ...

  3. JS正则--非负整数或小数[小数最多精确到小数点后两位]

    function ValidPrice(obj) { s = obj.value; //var reg = /^[0-9]*\.[0-9]{0,2}$/; var reg = /^[0-9]+([.] ...

  4. 关于 IIS 上运行 ASP.NET Core 站点的“HTTP 错误 500.19”错误

    昨天回答了博问中的一个问题 —— “HTTP 错误 500.19 - Internal Server Error dotnetcore”,今天在这篇随笔中时候事后诸葛亮地小结一下. 服务器是 Wind ...

  5. js 指向表格行变色,离开恢复

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  6. WPF 流打印

    原文:WPF 流打印 PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { Syst ...

  7. JavaScript eval() 函数,计算某个字符串,并执行其中的的 JavaScript 代码。

    JavaScript eval() 函数,计算某个字符串,并执行其中的的 JavaScript 代码. 适合用于计算器的计算,等. 例子: eval("x=10;y=20;document. ...

  8. 【转】 C#后台调用前台javascript的五种方法

    第一种,OnClientClick    (vs2003不支持这个方法)<asp:ButtonID="Button1" runat="server" Te ...

  9. python 安装 win 下的exe结尾的文件操作

    1.首先下载相关的模块 2.把下载的相关模块放到python 安装目录下 3.cmd 切换到python的安装目录下 例如:{PIL-1.1.7.win32-py2.7.exe} 4.执行 pip i ...

  10. iOS Touch ID使用

    1.首先导入头文件 #import <LocalAuthentication/LocalAuthentication.h> 2.关键代码 - (void)validateTouchID { ...