版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87473855
http://codeforces.com/contest/908/problem/B
0 1 2 3 可以对应 上下左右。(具体哪个对应哪个,试过才知道)
str 的 长度 为 100,0 1 2 3 的全排列一共24种,最坏的情况可以看成遍历长为2400的字符串,不会超时
然后就暴力+next_permutation即可
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
char map[52][52];
char str[102];
int a[] = {0,1,2,3};
int x[] = {0,0,1,-1};
int y[] = {1,-1,0,0};
int x0,y0;
int sum = 0;
int n,m;
void solve()
{
int i,len = strlen(str);
do
{
int xx = x0;
int yy = y0;
for(i=0;i<len;i++)
{
xx += x[a[str[i]-'0']];
yy += y[a[str[i]-'0']];
if(xx>=n || xx<0 || yy>=m || yy<0 || map[xx][yy]=='#')
{
break;
}
if(map[xx][yy]=='E')
{
sum++;
//cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << endl;
break;
}
}
}while(next_permutation(a,a+4));
}
int main ()
{
cin >> n >> m;
getchar();
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin >> map[i][j];
if(map[i][j] == 'S')
{
x0 = i;
y0 = j;
}
}
getchar();
}
cin >> str ;
solve();
cout << sum << endl;
return 0;
}