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\)的数据都跑得飞快,不过数据 ...
随机推荐
- WPF中的Application类。
原文:WPF中的Application类. Application对象用的名称空间是system.windows 1.手动创建Application对象步骤. 1.1).把项目中的App.Xaml文件 ...
- js 动态生成button 并设置click事件
<div id="MyDiv"></div> <script> function AddButton() { var MyDiv =docume ...
- v-charts显示标题
使用v-charts的时候,如果要显示标题需要以下操作 1. 加入:title props <ve-pie :title="chartTitle" :data="c ...
- WPF编游戏系列 之八 银行界面及金额校验
原文:WPF编游戏系列 之八 银行界面及金额校验 在前面<WPF编游戏系列 之四 用户控件>一文中通过用户控件创建了"My Shop"中物品列表框.本篇继 ...
- WCF的几个注意事项
wcf托管服务注意的问题 加上项目分为客户端-WCF服务-逻辑层-数据库三层wcf一直出现异常,说没有初始化啊之类的,如果你的逻辑代码确定没有问题的话,思考是不是wcf的配置文件(app.config ...
- LeetCode - 4 - Longest Substring Without Repeating Characters
题目 URL:https://leetcode.com/problems/median-of-two-sorted-arrays/ 解法 二分法. 总的思想是将 2 个数组用 2 个指针“整体”二分. ...
- MVC EF 导航属性
@model IQueryable<EFExam.Models.CategoryProductViewModel>@{ Layout = null;}<!DOCTYPE htm ...
- android Camera2 API使用详解
原文:android Camera2 API使用详解 由于最近需要使用相机拍照等功能,鉴于老旧的相机API问题多多,而且新的设备都是基于安卓5.0以上的,于是本人决定研究一下安卓5.0新引入的Came ...
- Win8Metro(C#)数字图像处理--2.15图像霓虹效果
原文:Win8Metro(C#)数字图像处理--2.15图像霓虹效果 [函数名称] 图像霓虹效果函数NeonProcess(WriteableBitmap src) 上述公式进行开方即可. [函 ...
- SQLServer 进程无法向表进行大容量复制(错误号: 22018 20253)
原文:SQLServer 进程无法向表进行大容量复制 我的环境:SQL SERVER 2008 R2:发布者 ->SQL SERVER 2017 订阅者 进程无法向表“"dbo&quo ...