2021-08-21 AcWing第13场周赛 3812. 机器人走迷宫
暴力+全排列
输入样例:
2
5 6
.....#
S....#
.#....
.#....
...E..
333300012
6 6
......
......
..SE..
......
......
......
01232123212302123021
输出样例:
1
14
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int m,n;
char a[51][51];
string path;
bool check(vector<int>& turn)
{
int x0,y0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
for (int i = 0; i < m; i ++ )
for (int j = 0; j < n; j ++ )
if(a[i][j]=='S') y0 = i, x0 = j;
for(auto ch: path)
{
int k = turn[ch-'0'];
x0 += dx[k], y0 += dy[k];
if(x0<0 || y0<0 || y0>=m || x0>=n || a[y0][x0]=='#')
return false;
if(a[y0][x0]=='E')
return true;
}
return false;
}
int main()
{
int t;
cin >> t;
while (t -- )
{
cin >> m >> n;
for (int i = 0; i < m; i ++ ) cin >> a[i];
cin >> path;
int res=0;
vector<int> turn{0, 1, 2, 3};
for (int i = 0; i < 24; i ++ ){
if(check(turn))
res++;
next_permutation(turn.begin(),turn.end());//以此更换每轮0123与上下左右的对应关系
}
cout << res << endl;
}
return 0;
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799130.html