2020 BIT冬训-模拟与暴力 E - New Year and Buggy Bot CodeForces - 908B

Problem Description

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

这题是要求s字符串中的0,1,2,3分别与NSWE的对应关系。一共有A(4,4)=24种可能。
这时候我们就想到了next_permutation();这个全排列函数。
暴力枚举一遍所有对应的可能即可。(注意要分清x和y坐标)
AC代码如下(make_pair就方便多了)(CF好像不让用gets(),提示编译错误)。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,sx,sy,tempx,tempy,cnt;
int d[4]={0,1,2,3};
char mp[55][55],s[105];
struct robot{
    int x,y,d;
}r[105];
pair<int,int>dir[4];
int main(){
    dir[0]=make_pair(0,1),dir[1]=make_pair(1,0);
    dir[2]=make_pair(0,-1),dir[3]=make_pair(-1,0);
    scanf("%d%d",&n,&m);
    getchar();
    for(int i=0;i<n;i++){
        scanf("%s",mp[i]);
        for(int j=0;j<m;j++){
            if(mp[i][j]=='S'){
                sx=i;
                sy=j;
            }
        }
    }
    scanf("%s",s);
    do{
        tempx=sx,tempy=sy;
        for(int i=0;i<strlen(s);i++){
            tempx+=dir[d[s[i]-'0']].first;
            tempy+=dir[d[s[i]-'0']].second;
            if(tempx==-1||tempy==-1||tempx==n||tempy==m) 
                break;
            if(mp[tempx][tempy]=='E'){
                cnt++;
                break;
            }else if(mp[tempx][tempy]=='#')
                break;
        }
    }while(next_permutation(d,d+4));
    printf("%d",cnt);
    return 0;
}

 



posted @ 2021-02-05 17:15  mikku  阅读(58)  评论(0)    收藏  举报