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.
OutputPrint a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
Examples5 6 .....# S....# .#.... .#.... ...E.. 333300012
1
6 6 ...... ...... ..SE.. ...... ...... ...... 01232123212302123021
14
5 3 ... .S. ### .E. ... 3
0
For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int x,y;
}S,E;
char mp[55][55];
string str;
int n,m;
int Next[4]={0,1,2,3};
void Getmap(){
for(int i=0;i<n;i++){
cin>>mp[i];
for(int j=0;j<m;j++){
if(mp[i][j]=='S'){
S.x=i;
S.y=j;
}
if(mp[i][j]=='E'){
E.x=i;
E.y=j;
}
}
}
}
int solve(){
int x=S.x,y=S.y;
for(int i=0;i<str.size();i++){
int d=str[i]-'0';
for(int j=0;j<4;j++){
if(d==Next[j]){
if(j==0) x++;
if(j==1) x--;
if(j==2) y++;
if(j==3) y--;
}
if(x>=n||x<0||y>=m||y<0) return 0;
else if(mp[x][y]=='#') return 0;
else if(mp[x][y]=='E') return 1;
}
}
return 0;
}
int main(){
cin>>n>>m;
Getmap();
cin>>str;
int ans=0;
do{
ans+=solve();
}while(next_permutation(Next,Next+4));
cout<<ans<<endl;
return 0;
}
本文介绍了一种迷宫中机器人通过指令集寻找出口的方法。机器人根据未指定方向的数字指令,在考虑所有可能的方向映射下尝试到达出口。文章提供了一个算法实现方案,包括输入输出格式说明、样例解析及代码示例。
&spm=1001.2101.3001.5002&articleId=79961400&d=1&t=3&u=4ac9c9515cc34a02b730beb588284747)
1084

被折叠的 条评论
为什么被折叠?



