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.
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.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
5 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.
题意:0~3分别代表上下左右中的某一个,不重复. 问对于给定的s,有多少种可能的情况满足从S出发到E.
思路:暴力枚举上下左右,O(4*3*2*1*len),注意初始化
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define bug1 cout <<"bug1"<<endl
#define bug2 cout <<"bug2"<<endl
#define bug3 cout <<"bug3"<<endl
using namespace std;
typedef long long ll;
char mp[100][100];
void Find(int n,int m,int &sx,int &sy,int &ex,int &ey){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j]=='S') sx=i,sy=j;
else if(mp[i][j]=='E') ex=i,ey=j;
}
}
return ;
}
int main(void){
int n,m,sx,sy,ex,ey,len;
ll ans=0;
char op[500];
cin >>n>>m;
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
scanf("%s",op+1);
len=strlen(op+1);
Find(n,m,sx,sy,ex,ey);
int xx=sx,yy=sy;
// printf("sx=%d sy=%d\n",sx,sy);
for(int i=0;i<=3;i++){
for(int j=0;j<=3;j++){
if(j==i) continue;
for(int k=0;k<=3;k++){
if(k==i || k==j) continue;
for(int l=0;l<=3;l++){
xx=sx,yy=sy;
if(l==i || l==j|| l==k) continue;
for(int t=1;t<=len;t++){
if(op[t]-'0'==i) xx--;
else if(op[t]-'0'==j) xx++;
else if(op[t]-'0'==k) yy--;
else if(op[t]-'0'==l) yy++;
if(xx<1||xx>n||yy<1||yy>m ||mp[xx][yy]=='#') break;
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&xx==ex&&yy==ey){
ans++;
// printf("xx=%d yy=%d ",xx,yy);
// printf("%d %d %d %d\n",i,j,k,l);
break;
}
}
}
}
}
}
cout << ans << endl;
}