1.题目描述
题目链接:点击这里
2.解决思路
只需要考虑,不管是什么序列,可选方案最多24种(4!种)所以我们只需要用next_permutation搞出0123的全排列,并让它们每一个值与序列中不同的值相对应(如0代表上,可以对应序列中的0,1,2,3;即0,1,2,3都可作为上)然后再按序列跑一遍图就行了。
3.代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=2550;
char s[MAXN][MAXN];
string cmd;
int n,m,t;
bool go(int start_x,int start_y,string cmd,int now)
{
int x=start_x,y=start_y;
while(now!=(int)cmd.size()){
int ne_x,ne_y;
if(cmd[now]=='0') ne_x=x-1,ne_y=y;
else if(cmd[now]=='1') ne_x=x+1,ne_y=y;
else if(cmd[now]=='2') ne_x=x,ne_y=y-1;
else if(cmd[now]=='3') ne_x=x,ne_y=y+1;
if(ne_x>=0&&ne_x<n&&ne_y>=0&&ne_y<m){
if(s[ne_x][ne_y]=='#') return false;
else{
if(s[ne_x][ne_y]=='E') return true;
else{
x=ne_x,y=ne_y;
now++;
}
}
}else
break;
}
return false;
}
int main(void)
{
cin>>t;
while(t--){
cin>>n>>m;
memset(s,0,sizeof(s));
for(int i=0;i<n;i++) cin>>s[i];
int start_x,start_y;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s[i][j]=='S'){
start_x=i,start_y=j;
break;
}
}
}
//cout<<start_x<<' '<<start_y<<endl;
cin>>cmd;
string mid=cmd;
int order[]={
0,1,2,3};//0-up 1-down 2-left 3-right
int ans=0;
do{
for(int i=0;i<(int)mid.length();i++){
//change turn
mid[i]=order[mid[i]-'0']+'0';
}
if(go(start_x,start_y,mid,0)) ans++;
mid=cmd;
}while(next_permutation(order,order+4));
cout<<ans<<endl;
}
return 0;
}