CodeVS 1026 逃跑的拉尔夫 题解

#include 
#include 
#include 
#include 
using namespace std;

const int dr[]={-1, 0, +1, 0};
const int dc[]={0, +1, 0, -1};
const char T[]="NESW";

char Map[50+2][50+2];
int flag[50+2][50+2];
int R, C, N;
int turn[1000+2];

struct NODE{
    int r, c;
    int n;
}node;

queueque;

int Find_turn(char GET){
    for(int i = 0; i<4; ++i) if(GET==T[i]) return i;
}

bool check(int r, int c, int n){
    if(r<0 || c<0) return false;
    if(r>=R || c>=C) return false;
    if(flag[r][c]==n) return false;
    if(Map[r][c]=='X') return false;
    return true;
}

int main(){
    memset(flag, -1, sizeof(flag));
    scanf("%d %d", &R, &C);
    for(int r = 0; r> Map[r][c];
        if(Map[r][c]=='*') node.r = r, node.c = c, node.n = 0, Map[r][c] = '.', flag[r][c] = 0;;
    }
    que.push(node);

    scanf("%d", &N);
    for(int n = 1; n<=N; ++n){
        char tmp[5+2];
        scanf("%s", tmp);
        turn[n] = Find_turn(*tmp);
    }

    while(!que.empty()){
        NODE New;
        New = que.front(); que.pop();
        ++New.n;
        while(check(New.r+dr[turn[New.n]], New.c+dc[turn[New.n]], New.n)){
            New.r += dr[turn[New.n]];
            New.c += dc[turn[New.n]];
            flag[New.r][New.c] = New.n;
            if(New.n!=N) que.push(New);
        }
    }

    for(int r = 0; r

你可能感兴趣的:(广度优先搜索)