HDU1728 逃离迷宫 DFS练习题

这道题其实单纯使用DFS实现并不难,在函数里面加一个转弯次数的形参就行。刚拿到这道题我确实也是这么做的。

但是把代码提交之后出现了DFS很常见的问题——超时,后来在讨论区发现大部门AC的代码都是使用的BFS+优先队列,因为DFS都写出来了就不想改了,再说也有用DFS成功AC的呀。看了大牛的代码知道加一个数组进行剪剪枝就行。

#include 
#include 
#include 

using namespace std;

int to[4][2]={1,0,-1,0,0,1,0,-1};
char map[105][105];
int turn[105][105];
int sx,sy,ex,ey,swerve,nowtime;
bool endn=false;
int m,n;

void dfs(int x,int y,int flag){
    if(nowtime>swerve) return;
    if(x==ex&&y==ey){
        endn=true;
    }
    if(endn) return;
    //cout<>t;
    while(t--){
        endn=false;
        memset(map,0,sizeof(map));
        memset(turn,999999,sizeof(turn));    //初始化剪枝数组要无限大,因为后面就是要筛选转弯次数少的路。
        nowtime=0;
        cin>>m>>n;
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++){
                cin>>map[i][j];
            }
        //cout<>swerve>>sy>>sx>>ey>>ex;
        turn[sx][sy]=0;
        dfs(sx,sy,0);
        if(endn) cout<<"yes"<

你可能感兴趣的:(acm)