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.
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.
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
5 6 .....# S....# .#.... .#.... ...E.. 333300012
1
6 6 ...... ...... ..SE.. ...... ...... ...... 01232123212302123021
14
5 3 ... .S. ### .E. ... 3
0
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 <stdio.h>
#include <vector>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <sstream>
#include <string>
#include <utility>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 150;
char g[maxn][maxn];
int n, m;
int dir[4] = {0, 1, 2, 3};
int sx, sy, ex, ey;
string str;
int func() {
int x = sx, y = sy;
for (int i = 0; i < str.size(); ++i) {
int d = str[i] - '0';
for (int j = 0; j < 4; ++j) {
if (d == dir[j]) {
if (j == 0)
x++;
if (j == 1)
x--;
if (j == 2)
y++;
if (j == 3)
y--;
}
if (x > n || x < 1 || y > m || y < 1)
return 0;
else if (g[x][y] == 'E') {
return 1;
}
else if (g[x][y] == '#')
return 0;
}
}
return 0;
}
int main() {
//freopen("in.txt", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> g[i][j];
if (g[i][j] == 'S') {
sx = i;
sy = j;
}
else if (g[i][j] == 'E') {
ex = i;
ey = j;
}
}
}
cin >> str;
int res = 0;
do {
res += func();
} while (next_permutation(dir, dir + 4));
cout << res << endl;
}

本文介绍了一个迷宫导航问题,机器人需要从起点S出发,通过解析一串数字指令找到到达终点E的所有可能路径。文章探讨了如何通过遍历所有方向映射的可能性来计算成功到达终点的方案数。

385

被折叠的 条评论
为什么被折叠?



