Codeforces New Year and Buggy Bot 题解

主要思路

  全排列,然后按输入的字符串走并且判断是否撞墙

注:这样不会TLE,全排列最多24种

代码

#include<bits/stdc++.h>
using namespace std;
const int N=60;
const int S=110;
int dx[5]={0 , -1 , 1 , 0}; //
int dy[5]={1 , 0 , 0 , -1}; //// {右, 上 , 下 ,左}
int s[S],t[5];
char a[N][N];
int n,m;
int sx,sy,ex,ey;//开始与结束的坐标
int main() {
    int ans=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            cin>>a[i][j];
            if(a[i][j]=='S') {
                sx=i;sy=j; //记下起点
            }
            else if(a[i][j]=='E') {//这里可以删去,后面if(x==ex &&         y==ex) 改为 if(a[x][y]=='E')
            ex=i;ey=j; //记下终点
        } 
    }
    }
    string st;
    cin>>st;
    int l=st.size();
    for(int i=1;i<=l;i++) s[i]=st[i-1]-'0';//将st换为数组
    for(int i=0;i<4;i++) t[i]=i;
    int x,y;
    do {
    x=sx;y=sy;//这边别忘了,每次都要初始化
    for(int i=1;i<=l;i++) {
       if(x>n || y>m || x<1 || y<1) break; //出界
            x+=dx[t[s[i]]];
            y+=dy[t[s[i]]];
            if(x==ex && y==ey) { //到达终点
                ans++;
                break ;
            }
            if(a[x][y]=='#') break;//撞墙
        }
    }while(next_permutation(t,t+4));//全排列函数
    cout<<ans<<endl;
    return 0;
}            

 

posted @ 2019-08-22 12:51  Ryan-Liu  阅读(244)  评论(0编辑  收藏  举报