New Year and Buggy Bot

本文介绍了一种通过编程迷宫中的机器人寻找从起点S到终点E的可能路径的方法。机器人根据给定的数字指令字符串进行移动,每个数字代表不同的方向。文章提供了一个使用C++实现的示例代码,演示了如何计算所有有效的路径映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bob programmed a robot to navigate through a 2d maze.

The maze has some obstacles. Empty cells are denoted by the character ‘.’, where obstacles are denoted by ‘#’.

There is a single robot in the maze. Its start position is denoted with the character ‘S’. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character ‘E’. This position has no obstacle in it.

The robot can only move up, left, right, or down.

When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.

The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.

Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.

Input
The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze.

The next n lines will contain exactly m characters each, denoting the maze.

Each character of the maze will be ‘.’, ‘#’, ‘S’, or ‘E’.

There will be exactly one ‘S’ and exactly one ‘E’ in the maze.

The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.

Output
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.

Example
Input
5 6
…..#
S….#
.#….
.#….
…E..
333300012
Output
1
Input
6 6
……
……
..SE..
……
……
……
01232123212302123021
Output
14
Input
5 3

.S.

#

.E.

3
Output
0
Note
For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.

题意 : 0-3代表四个方向,一共24种映射情况,求一共有几种可以从S到达E
然后有一个神奇的函数,next_permutation(a,a+n); 返回值是bool类型,表示比a序列的下一个序列。
用do-while()循环可以输出包括第一个排列在内的所有的排列。

#include<iostream>
#include<cstring>
#include<algorithm>  
using namespace std;  
char s[70][70],op[105];  
int n,m,sx,sy,ex,ey;  
int dir[4]={0,1,2,3};  
int Solve(){  
    int len=strlen(op);  
    int x=sx,y=sy;  
    for(int i=0;i<=len-1;i++){  
        int num=op[i]-'0';  
        for(int j=0;j<=3;j++){  
            if(num==dir[j]){  
                if(j==0)x++;  
                else if(j==1)x--;  
                else if(j==2)y++;  
                else if(j==3)y--;  
            }  
            if(x>n||x<1||y>m||y<1||s[x][y]=='#')return 0;  
            else if(s[x][y]=='E')return 1;  
        }  
    }  
    return 0;  
}  

int main(){  
    cin>>n>>m;
    for(int i=1;i<=n;i++)    
        cin>>s[i]+1;  
    cin>>op;  
    for(int i=1;i<=n;i++){  
        for(int j=1;j<=m;j++){  
            if(s[i][j]=='S'){  
                sx=i;  
                sy=j;  
            }  
            else if(s[i][j]=='E'){  
                ex=i;  
                ey=j;  
            }  
        }  
    }  
    int ans=0;  
    do{  
        ans+=Solve();  
    }while(next_permutation(dir,dir+4));  
    cout<<ans<<endl;  
    return 0;  
}

STL模板库包含的东西太陌生了。学习学习!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值