1. 程式人生 > >Codeforces-Good Bye 2017 B. New Year and Buggy Bot(模擬)

Codeforces-Good Bye 2017 B. New Year and Buggy Bot(模擬)

Describe

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. It’s start position is denoted
with the character ‘S’. This position has no obstacle in it. There is
also a single exit in the maze. It’s 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.

思路

先說題意,題目給了一個nm的圖,其中S代表起點,E代表終點,#代表牆。
然後有一串包含0123的字串,這裡面的每個數字代表上下左右的其中的一個方向,但是現在出現的問題是,忘記了0123誰代表哪個方向,所以求出0123代表方向的所有可能組合,然後輸出,通過指令能成功走到終點的數量。注意,一旦走到終點,後面的指令會失效,如果碰到牆或者走到牆壁代表當前的這一串指令是錯誤的。

直接利用next_permutation枚舉出所有的方向組合數,然後模擬機器人走路,記錄一下能走到終點的次數就是答案

程式碼

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=50+5;
int n,m;
char mp[N][N];
int vis[N][N];
struct node
{
    int x,y;
} st;
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#')
    {
        if(mp[x][y]=='E')
            return 1;
        else
            return 2;
    }
    else
        return 0;
}
int main()
{
    string s;
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        cin>>mp[i];
        for(int j=0; j<m; j++)
            if(mp[i][j]=='S')
                st.x=i,st.y=j;
    }
    cin>>s;
    int len=s.length();
    int sum=0;
    int c[4]= {0,1,2,3};//上下左右
    do
    {
        mem(vis,0);
        node sst=st;
        node now[4];
        now[c[0]].x=1,now[c[0]].y=0;
        now[c[1]].x=-1,now[c[1]].y=0;
        now[c[2]].x=0,now[c[2]].y=1;
        now[c[3]].x=0,now[c[3]].y=-1;
        for(int i=0; i<len; i++)
        {
            node to=now[s[i]-'0'];
            int temp=judge(sst.x+to.x,sst.y+to.y);
            if(temp==1)
            {
                sum++;
                break;
            }
            else if(temp==2)
            {
                sst.x+=to.x;
                sst.y+=to.y;
            }
            else if(temp==0)
                break;
        }
    }
    while(next_permutation(c,c+4));
    cout<<sum<<endl;
    return 0;
}