New Year and Buggy Bot

New Year and Buggy Bot


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.

因为行走的路线每次都是固定的,只需要用for循环进行就可以,一开始用递归写不知道哪里错了,第一个点都过不了,然后用循环走就过了,四重for枚举方向
code:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n,m;
char G[100][100];
char instru[200];
int dir[4][2];
int ans;
int sx,sy,ex,ey;
void judge(){
   int i;
   int xx = sx;
   int yy = sy;
   for(i = 0; i < strlen(instru); i++){
      xx += dir[instru[i]-'0'][0];
      yy += dir[instru[i]-'0'][1];
      if(xx<0||xx>=n||yy<0||yy>=m||G[xx][yy]=='#')return;
      if(xx == ex && yy == ey){
        ans++;
        return;
      }
   }
}
int main(){
    int i,j;
    scanf("%d%d",&n,&m);
    for(i = 0; i < n; i++){
        scanf("%s",G[i]);
    }
    scanf("%s",instru);
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            if(G[i][j]=='S')
                sx = i,sy = j;
            if(G[i][j]=='E')
                ex = i,ey = j;
        }
    }
    int p,q;
    ans = 0;
    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            for(p = 0; p < 4; p++){
                for(q = 0; q < 4; q++){
                    if(j!=i&&(p!=j&&p!=i)&&(q!=p&&q!=j&&q!=i)){
                        dir[i][0] = 1,dir[i][1] = 0;//down
                        dir[j][0] = -1,dir[j][1] = 0;//up
                        dir[p][0] = 0,dir[p][1] = 1;//right
                        dir[q][0] = 0,dir[q][1] = -1;//left
                        judge();
                    }
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值