BFS 逃跑的拉尔夫

#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include <stdio.h>
#include <string.h>
#include<iostream>

using namespace std;
char map_[55][55];
char directs[2010][10];
int total_directs;
bool flag[55][55][2010];
struct hereT{
    int x,y,cen;
};
int height,width;
queue<hereT>que;
void push_eve(hereT temp){
    hereT haha;
    //cout<<"posi:"<<temp.x<<" "<<temp.y<<"direct:"<<directs[temp.cen]<<endl;
    if(flag[temp.x][temp.y][temp.cen]==1)return;
    if(temp.cen>total_directs){map_[temp.x][temp.y]='*';return;}
    if(directs[temp.cen][0]=='N'){
        for(int i=1;;i++){
            int newx=temp.x-i,newy=temp.y;
            if(newx<1)return;
            if(map_[newx][newy]=='X')return;
            haha.cen = temp.cen+1;
            haha.x=newx;haha.y=newy;
            que.push(haha);
            flag[temp.x][temp.y][temp.cen]=1;
        }
    }
    else  if(directs[temp.cen][0]=='S'){
        for(int i=1;;i++){
            int newx=temp.x+i,newy=temp.y;
            if(newx>height)return;
            if(map_[newx][newy]=='X')return;
            haha.cen = temp.cen+1;
            haha.x=newx;haha.y=newy;
            que.push(haha);
            flag[temp.x][temp.y][temp.cen]=1;
        }
    }
    else  if(directs[temp.cen][0]=='W'){
        for(int i=1;;i++){
            int newx=temp.x,newy=temp.y-i;
            if(newy<1)return;
            if(map_[newx][newy]=='X')return;
            haha.cen = temp.cen+1;
            haha.x=newx;haha.y=newy;
            que.push(haha);
            flag[temp.x][temp.y][temp.cen]=1;
        }
    }
    else  if(directs[temp.cen][0]=='E'){
        for(int i=1;;i++){
            int newx=temp.x,newy=temp.y+i;
            if(newy>width)return;
            if(map_[newx][newy]=='X')return;
            haha.cen = temp.cen+1;
            haha.x=newx;haha.y=newy;
            que.push(haha);
            flag[temp.x][temp.y][temp.cen]=1;
        }
    }
}
int main()
{
    
    memset(flag,0,sizeof(flag));
    cin>>height>>width;
    hereT start;
    for(int i=1;i<=height;i++){
        for(int j=1;j<=width;j++){
            cin>>map_[i][j];
            if(map_[i][j]=='*'){start.x=i;start.y=j;start.cen=1;}
        }
    }
    /*
    for(int i=1;i<=height;i++){
        for(int j=1;j<=width;j++){
            cout<<map_[i][j];
        }cout<<endl;
    }*/
    cin>>total_directs;
    map_[start.x][start.y]='.';
    for(int i=1;i<=total_directs;i++)cin>>directs[i];
    que.push(start);
    while(!que.empty()){
        start=que.front();
        que.pop();
        push_eve(start);
    }
    for(int i=1;i<=height;i++){
        for(int j=1;j<=width;j++){
            cout<<map_[i][j];
        }cout<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值