您的位置:首页 > 编程语言 > Go语言

Codeforces Good Bye 2017 B. New Year and Buggy Bot 枚举全排列、模拟

2018-01-14 16:08 459 查看
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
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.

Source

Good Bye 2017

My Solution

题意:给出一张地图,有一个出口和入口,以及一些障碍和通道。然后给出一个操作序列0123,分别表示上下左右,

求有多少种对应的可能可以使得按照该指令序列从入口走到出口。0->下,1->左,2->上,3->右为一种可能,以此类推。

枚举全排列、模拟

我们规定长度为4的序列op,op0表示上,op1表示下,op2表示左,op3表示右。

所以只要枚举0123的全排列即可得到所以的对应可能,然后用每一个排列去模拟的跑一遍地图即可判断该情况是否可行。

/*//枚举全排列代码

inline void dfs(int u){

    int i, j, ok;

    if(u == 4){

       ;//

        return;

    }

    for(i = 0; i < 4; i++){

        ok = 1;

        for(j = 0; j < u; j++){

            if(op[j] == i) ok = 0;

        }

        if(ok){

            op[u] = i;

            dfs(u+1);

        }

    }

}

*/

时间复杂度 O(4! * n)

空间复杂度 O(n)

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long LL;
const int MAXN = 50 + 8;

string mp[MAXN], s;
int v[108], op[4];
int ans, sx, sy, ex, ey, sz, n, m;

inline void dfs(int u){
int i, j, ok;
if(u == 4){
int x = sx, y = sy, ptr;
for(i = 0; i < sz; i++){
for(j = 0; j < 4; j++){
if(op[j] == v[i]){
ptr = j;
}
}
if(ptr == 0){
x -= 1;
}
else if(ptr == 1){
x += 1;
}
else if(ptr == 2){
y -= 1;
}
else{
y += 1;
}
if(y < 0 || y >= m || x < 0 || x >= n){
return;
}
if(mp[x][y] == '#') return;
if(mp[x][y] == 'E'){
ans++; return;
}
}

return;
}

for(i = 0; i < 4; i++){
ok = 1;
for(j = 0; j < u; j++){
if(op[j] == i) ok = 0;
}
if(ok){
op[u] = i;
dfs(u+1);
}
}
}

int main()
{
#ifdef LOCAL
freopen("b.txt", "r", stdin);
//freopen("b.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

int i, j;
ans = 0;
cin >> n >> m;
for(i = 0; i < n; i++){
cin >> mp[i];
}
cin >> s;
sz = s.size();
for(i = 0; i < sz; i++){
v[i] = s[i] - '0';
}
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
if(mp[i][j] == 'S'){
sx = i;
sy = j;
}
else if(mp[i][j] == 'E'){
ex = i;
ey = j;
}
}
}

//op[0] = -1, op[1] = -1, op[2] = -1, op[3] = -1;
dfs(0);
cout << ans << endl;

#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}


  Thank you!

                                                                       
                                                                     ------from ProLights
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: