Algorithm


B. New Year and Buggy Bot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
5 6
.....#
S....#
.#....
.#....
...E..
333300012
output
Copy
1
input
Copy
6 6
......
......
..SE..
......
......
......
01232123212302123021
output
Copy
14
input
Copy
5 3
...
.S.
###
.E.
...
3
output
Copy
0
Note

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

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e2 + 1;
int n, m;
char g[N][N];
string s, nw;
vector<int> idx;
int sx, sy, dx, dy;

bool check() {
	nw = s;

	for(int i = 0; i < nw.size(); ++i)
		if(nw[i] - '0' == idx[0])
			nw[i] = 'D';
		else if(nw[i] - '0' == idx[1])
			nw[i] = 'L';
		else if(nw[i] - '0' == idx[2])
			nw[i] = 'U';
		else
			nw[i] = 'R';

	int cx = sx, cy = sy;
	for(int i = 0; i < nw.size(); ++i) {
		if(cx < 0 || cx >= n || cy < 0 || cy >= m || g[cx][cy] == '#')
			return false;

		if(cx == dx && cy == dy)
			return true;

		if(nw[i] == 'D')
			++cx;
		if(nw[i] == 'L')
			--cy;
		if(nw[i] == 'U')
			--cx;
		if(nw[i] == 'R')
			++cy;
	}

	return cx == dx && cy == dy;
}

int main() {
	scanf("%d %d", &n, &m);

	for(int i = 0; i < n; ++i)
		for(int j = 0; j < m; ++j) {
			scanf(" %c", &g[i][j]);
			if(g[i][j] == 'S')
				sx = i, sy = j;
			if(g[i][j] == 'E')
				dx = i, dy = j;
		}

	cin >> s;

	for(int i = 0; i < 4; ++i)
		idx.push_back(i);

	int res = 0;
	do {
		res += check();
	} while(next_permutation(idx.begin(), idx.end()));

	printf("%d\n", res);

	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5 6
.....#
S....#
.#....
.#....
...E..
333300012

Output

x
+
cmd
1
Advertisements

Demonstration


Codeforces Solution-New Year and Buggy Bot-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+