GYM - 101201B Buggy Robot(BFS+DP)

You are trying to program a robot to navigate through a 2-dimensional maze and find the exit.The maze can be represented as a grid with n rows and m columns. Some grid cells have obstaclesthat the robot cannot pass. The other cells are empty, which the robot can freely pass. Exactly oneof the empty cells in the grid is marked as the exit, and the robot will exit the maze immediatelyonce it reaches there.You can program the robot by sending it a command string. A command string consists of characters‘L’, ‘U’, ‘R’, ‘D’, corresponding to the directions left, up, right, down, respectively. The robotwill then start executing the commands, by moving to an adjacent cell in the directions specifiedby the command string. If the robot would run into an obstacle or off the edge of the grid, it willignore the command, but it will continue on to remaining commands. The robot will also ignoreall commands after reaching the exit cell.Your friend sent you a draft of a command string, but you quickly realize that the command stringwill not necessarily take the robot to the exit. You would like to fix the string so that the robotwill reach the exit square. In one second, you can delete an arbitrary character, or add an arbitrarycharacter at an arbitrary position. Find how quickly you can fix your friend’s command string.You do not care how long it takes the robot to find the exit, but only how long it takes to repairthe command string.


Input

The first line of input contains the two integers n and m (1 ≤ n, m ≤ 50).Each of the next n lines contains m characters, describing the corresponding row of the grid. Emptycells are denoted as ‘.’, the robot’s initial position is denoted as ‘R’, obstacles are denoted as ‘#’,and the exit is denoted as ‘E’.The next and final line of input contains your friend’s command string, consisting of between 1 and50 characters, inclusive.It is guaranteed that the grid contains exactly one ‘R’ and one ‘E’, and that there is always a pathfrom ‘R’ to ‘E’.


Output

Print, on a single line, a single integer indicating the minimum amount of time to fix the program.


Sample Input 

3 3

R..

.#.

..E

LRDD


Sample Output

1



Sample Input 

2 4

R.#.

#..E

RRUUDDRRUUUU


Sample Output

0


题意:给你一张地图,和一串指令,要你用最少的修改次数,使得这串指令能正确走到终点。遇到障碍则无视当前指令。如果走到了终点,则无视后面的指令。


解题思路:BFS+DP,详见代码注释。


#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#define MAXV 2000005
#define INF (1<<30)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long int ll;

struct point{
    int x;
    int y;
    int pos;
    point(int a=0,int b=0,int c=0){
        x=a;
        y=b;
        pos=c;
    }
};

string commend;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
char maze[100][100];
int n,m;
int dp[100][100][100];//走到x,y,只考虑前k个字符的最少修改次数
int mp[255];

bool check(int i,int j) {
    return i>0&&i<=n&&j>0&&j<=m&&maze[i][j]!='#';
}

void bfs(point s){
    queue<point> que;
    que.push(s);

    while(!que.empty()){
        point tp=que.front();
        que.pop();

        //添加一个字符
        for(int i=0;i<4;i++){
            int x=tp.x+dx[i];//新地点
            int y=tp.y+dy[i];
            int p=tp.pos;
            if(check(x,y)){
                //如果从旧地点,添加一个字符的dp值,比当前新地点的要小,那么状态转移
                if(dp[x][y][p]>dp[tp.x][tp.y][p]+1){
                    dp[x][y][p]=dp[tp.x][tp.y][p]+1;
                    que.push(point(x,y,p));
                }
            }
        }

        int x=tp.x;
        int y=tp.y;
        int p=tp.pos;

        if(p<commend.length()){
            //删除一个字符,删除相当于考虑下一个字符
            if(dp[x][y][p+1]>dp[x][y][p]+1){
                dp[x][y][p+1]=dp[x][y][p]+1;
                que.push(point(x,y,p+1));
            }


            //正常走
            x=tp.x+dx[mp[commend[p]]];
            y=tp.y+dy[mp[commend[p]]];
            if(!check(x,y)){
                x=tp.x;
                y=tp.y;
            }
            //如果从旧地点,正常走的dp值,比当前新地点的要小(p要+1),那么状态转移
            if(dp[x][y][p+1]>dp[tp.x][tp.y][p]){
                dp[x][y][p+1]=dp[tp.x][tp.y][p];
                que.push(point(x,y,p+1));
            }
        }

    }

}


int main()
{

    mp['D']=0;
    mp['R']=1;
    mp['U']=2;
    mp['L']=3;

    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>maze[i][j];
    cin>>commend;

    int sx,sy;
    int ex,ey;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(maze[i][j]=='R')sx=i,sy=j;
            if(maze[i][j]=='E')ex=i,ey=j;
        }

    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++)
            for(int k=0;k<=commend.length();k++)
                dp[i][j][k]=INF;

    
    dp[sx][sy][0]=0;//初始状态,即走到起点用0个字符,肯定是一步到位,所以为0
    bfs(point(sx,sy,0));

    int ans=INF;
    for(int i=0;i<=commend.length();i++)
        ans=min(ans,dp[ex][ey][i]);

    printf("%d\n",ans);

    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值