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.

只要安全到达出口就可以了,可以有多余的指令。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int n,m,dir[][] = {,,,,,-,-,},c = ,visited[],f[],sx,sy;
char mp[][],s[];
int check()
{
int px = sx,py = sy;
for(int i = ;s[i];i ++)
{
px += dir[f[s[i] - '']][],py += dir[f[s[i] - '']][];
if(px < || py < || px >= n || py >= m || mp[px][py] == '#')return ;
if(mp[px][py] == 'E')return ;
}
return ;
}
void dfs(int k)
{
if(k == )
{
if(check())c ++;
return;
}
for(int i = ;i < ;i ++)
{
if(!visited[i])
{
visited[i] = ;
f[k] = i;
dfs(k + );
visited[i] = ;
}
}
}
int main()
{
cin>>n>>m;
for(int i = ;i < n;i ++)
{
for(int j = ;j < m;j ++)
{
cin>>mp[i][j];
if(mp[i][j] == 'S')sx = i,sy = j;
}
}
cin>>s;
dfs();
cout<<c;
}

New Year and Buggy Bot的更多相关文章

  1. 【Good Bye 2017 B】 New Year and Buggy Bot

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举一下全排列.看看有多少种可以到达终点即可. [代码] #include <bits/stdc++.h> using ...

  2. Codeforces New Year and Buggy Bot 题解

    主要思路:全排列,然后按输入的字符串走并且判断是否撞墙 注:这样不会TLE,全排列最多24种 Code(C++): #include<bits/stdc++.h> using namesp ...

  3. 冬训 day2

    模拟枚举... A - New Year and Buggy Bot(http://codeforces.com/problemset/problem/908/B) 暴力枚举即可,但是直接手动暴力会非 ...

  4. Good Bye 2017

    太菜了啊,一不小心就goodbye rating了 A. New Year and Counting Cards time limit per test 1 second memory limit p ...

  5. [Codeforces]Good Bye 2017

    A - New Year and Counting Cards #pragma comment(linker, "/STACK:102400000,102400000") #inc ...

  6. Good Bye 2017 A B C

    Good Bye 2017 A New Year and Counting Cards 题目链接: http://codeforces.com/contest/908/problem/A 思路: 如果 ...

  7. CodeForces Goodbye 2017

    传送门 A - New Year and Counting Cards •题意 有n张牌,正面有字母,反面有数字 其中元音字母$a,e,o,i,u$的另一面必须对应$0,2,4,6,8$的偶数 其他字 ...

  8. cf 908B

    B - New Year and Buggy Bot 思路:刚开始看到这个题的时候,一头雾水,也不知道要干什么,后来百度翻译了了一遍,看明白了,不得不说自己的英语太差了,好了,步入正题: 给你n行m列 ...

  9. 《HelloGitHub》之GitHub Bot

    起因 我在github上发起了一个开源项目:<HelloGitHub月刊>,内容是github上收集的好玩,容易上手的开源项目. 目的:因为兴趣是最好的老师,我希望月刊中的内容可以激发读者 ...

随机推荐

  1. foreach使用和函数

    2016-04-25 一.foreach( 对集合每个元素的引用 in 集合 ) { } int[] a = new int[5]{1,2,3,4,5}; foreach( int b in a ) ...

  2. [笔记]一道C语言面试题:IPv4字符串转为UInt整数

    题目:输入一个IPv4字符串,如“1.2.3.4”,输出对应的无符号整数,如本例输出为 0x01020304. 来源:某500强企业面试题目 思路:从尾部扫描到头部,一旦发现无法转换,立即返回,减少无 ...

  3. image has dependent child images

    在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器(哪怕是已经停止的容器),否则无法删除该镜像. 停止容器 # docker stop $(docker ps -a | grep ...

  4. C语言中static的使用方法【转】

    本文转自:http://blog.csdn.net/renren900207/article/details/21609649 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量 ...

  5. RHCE学习笔记 管理1 (第六章 第七章)

    第六章 利用linux 文件系统权限文件访问 1.linux文件系统权限 文件的权限分为:  rwx  读/写/执行 ls -l  /home   查看/home下文件 ls -ld /home   ...

  6. Ajax缓存处理

    如果直接用jQuery里的$.ajax()方法的话,去除缓存很简单,只需要配置一下缓存属性cache为false,但如果想要简单写法getJSON(),去除缓存就不能通过配置来解决了.因为getJSO ...

  7. P3391 文艺平衡树

    hh 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  8. Datax官方笔记总结

    # DataX DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.SQL Server.Oracle.PostgreSQL.HDFS.Hive.HBase.OTS. ...

  9. MapReduce-二进制输入

    Hadoop的MapReduce不只是可以处理文本信息,它还可以处理二进制格式的数据1. 关于SequenceFileInputFormat类Hadoop的顺序文件格式存储二进制的键/值对的序列.由于 ...

  10. HRBUST 1717 字典树模板

    之前写字典树虽然很熟也能变化 但是一直是到了场上再乱写 写的很长 于是准备写个短点的板子 于是选了个水题 然而写出了1W个bug insert和query反而写的没什么问题.. L c[100050] ...