New Year and Buggy Bot

版权声明:欢迎大家指正错误,有不同观点的欢迎评论,共同进步 https://blog.csdn.net/Sirius_han/article/details/79961917

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.

Examples
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,1,2,3分别表示四个方向, 给出一串只包含0~3的字串, 问有几只种给0~3赋予方向的方案可以走出迷宫, 一共24种方案, 每个试一遍, 能走出迷宫ans++;

#include <iostream>
using namespace std;
int ans;
string s;	
int n, m, s_x, s_y, e_x, e_y;
char maze[55][55];
int nex[4][2]={1,0,0,1,-1,0,0,-1};
int a[5];
void dfs(int x, int y){
	for(int i=0; i<s.size(); i++){
		x=x+nex[a[s[i]-'0']][0], y=y+nex[a[s[i]-'0']][1];
		if(x>n||y>m||x<=0||y<=0) return;
		if(maze[x][y]=='#') return;
		if(x==e_x&&y==e_y){
			ans++;
			return;
		}
	}
}
int main(){
	cin >> n >> m;
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			cin >> maze[i][j];
			if(maze[i][j]=='S') s_x=i, s_y=j;
			if(maze[i][j]=='E') e_x=i, e_y=j;
		}
	}
	cin >> s;
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++){
			if(i==j) continue;
			for(int k=0; k<4; k++){
				if(i==k||j==k) continue;
				for(int l=0; l<4; l++){
					if(i==l||j==l||k==l) continue;
					a[0]=i, a[1]=j, a[2]=k, a[3]=l;
					dfs(s_x, s_y);
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页