2020训练赛3

D题:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
const int maxn=100;
const int inf=0x3f3f3f3f;
int n,m;
char mp[maxn][maxn];
bool vis[maxn][maxn];
string instruction;
int dir[4][2]={-1,0,1,0,0,-1,0,1};//上下左右
int dp[maxn][maxn][maxn];//i,j的前k个指令所需要的最小操作次数
int opx,opy,edx,edy;

struct node{
    int x,y;
    int step;
    node(int a,int b,int c=0):x(a),y(b),step(c){}
};

bool is_legal(int x,int y){
    return x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#';
}

void bfs(){
    queue<node>q;
    q.push(node(opx,opy,0));

    while(!q.empty()){
        node cur=q.front();
        q.pop();
        int x=cur.x,y=cur.y,curstep=cur.step;
        for (int i=0;i<4;i++){//加一指令,往四周扩散
            int tx=x+dir[i][0],ty=y+dir[i][1];
            if(is_legal(tx,ty)){
                if(dp[tx][ty][curstep]>dp[x][y][curstep]+1){//到周围的次数少,更新
                    dp[tx][ty][curstep]=dp[x][y][curstep]+1;
                    q.push(node(tx,ty,curstep));
                }    
            }
        }

        if(curstep<instruction.length()){
            if(dp[x][y][curstep+1]>dp[x][y][curstep]+1){//删除
                dp[x][y][curstep+1]=dp[x][y][curstep]+1;
                q.push(node(x,y,curstep+1));
            }

            //往下走一步
            char c=instruction[curstep];
            int tx,ty;
            if(c=='U'){
                tx=x-1;
                ty=y;
            }
            else if(c=='D'){
                tx=x+1;
                ty=y;
            }
            else if(c=='L'){
                tx=x;
                ty=y-1;
            }
            else if(c=='R'){
                tx=x;
                ty=y+1;
            }

            if(!is_legal(tx,ty)){
                tx=x;
                ty=y;
            }

            if(dp[tx][ty][curstep+1]>dp[x][y][curstep]){
                dp[tx][ty][curstep+1]=dp[x][y][curstep];
                q.push(node(tx,ty,curstep+1));
            }
        }
    }
}

void init(){
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
            for(int k=0;k<maxn;k++)
                dp[i][j][k]=inf;
}

int main(){ 
    init();
    scanf("%d%d",&n,&m);

    for(int i=0;i<n;i++){
        getchar();
        for (int j=0;j<m;j++){
            scanf("%c",&mp[i][j]);
        }
    }

    getchar();
    cin>>instruction;

    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            if(mp[i][j]=='S') {
                opx=i;opy=j;
            }
            if(mp[i][j]=='G'){
                edx=i;edy=j;
            }
        }
    }

    dp[opx][opy][0]=0;
    bfs();

    int ans=inf;
    for (int i=0;i<=instruction.length();i++){
        ans=min(ans,dp[edx][edy][i]);
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值