伪DFS...BFS+模拟--poj3083

题目描述

额,这个标题有点不友好,主要是被恶心了两天了,本来很快就写出来,结果超内存,怎么优化都不行,刚才才发现!原因是找最短路时用bfs,没有加visit数组...导致他虽然能出结果,但是搜索了无数次。。。

 

这道题主要在于方向处理,真正思考过并不难,定义move数组时,按移动方向顺时针定义,然后,对于当前方向,比如“上”,如果这时候我们是向左手边走,那么我们就从move向“左”开始遍历move,只要遍历到合法的move,就把for break掉。

另外说一点,这道题其实不需要dfs,dfs是用来搜索的,是一种搜索的策略,搜索什么呢?目标,路径,都可以,,搜索的是我们原本不知道具体位置的东西,而这道题中,“沿左手边走”“沿右手边走”这都是确定的东西,所以根本不需要dfs,一个while(没有找到目标位置),里面套一个遍历move数组的for,这就够了,这样子的代码也简单

至于bfs,找最短路,这个就无需解释了

代码0ms,284kb,还算ok,也不长  感谢马榕仙女姐姐的指点~

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

#define move movee

int l,h,bx,by,ex,ey;
char map[41][41];
int ans,num;
int vis[41][41];
int move[][2]={{0,-1},{-1,0},{0,1},{1,0}}; //左上右下 

struct tt{
    int x;
    int y;
    int steps;
    tt(int x,int y,int steps):x(x),y(y),steps(steps){}
    tt(){}
}ttt;
queue<tt> q;

int check(int x,int y){
    return x>0 && x<=h && y>0 && y<=l;
}

void dfsl(int x,int y,int i){

    int cnt=1;
    int tx,ty;
    while(!(x==ex && y==ey)){
        int cnt=1;
        i=(i+3)%4;
        for(;(cnt++)<=4;i=(i+1)%4){
            tx=x+move[i][0];
            ty=y+move[i][1];
            if(!check(tx,ty) || map[tx][ty]=='#') continue;
            num++;
            x=tx;
            y=ty;
//            cout<<123456789;
            break;
            
        }
    }
        ans=num;
        cout<<ans<<' ';
}

void dfsr(int x,int y,int i){

    int cnt=1;
    int tx,ty;
    while(!(x==ex && y==ey)){
        int cnt=1;
        i=(i+1)%4;
        for(;(cnt++)<=4;i=(i+3)%4){
            tx=x+move[i][0];
            ty=y+move[i][1];
            if(!check(tx,ty) || map[tx][ty]=='#') continue;
            num++;
            x=tx;
            y=ty;
            break;
        }
    }
        ans=num;
        cout<<ans<<' ';
}

void bfs(){
    tt temp;
    int tx,ty;
    while(!q.empty()){
        temp=q.front();
        q.pop();
        if(temp.x==ex && temp.y==ey){
            cout<<temp.steps+1;
            goto L;
        }
        for(int i=0;i<4;i++){
            tx=temp.x+move[i][0];
            ty=temp.y+move[i][1];
            if(!check(tx,ty) || map[tx][ty]=='#' || vis[tx][ty]) continue;
            vis[tx][ty]=1;
            q.push(tt(tx,ty,temp.steps+1) );
        }
    }
L:  return; 
}


int main(){

    int t;
    cin>>t;
    while(t--){
        while(!q.empty()) q.pop();
        ans=num=1;
        cin>>l>>h;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=l;j++){
                cin>>map[i][j];
                if(map[i][j]=='S'){
                    bx=i;by=j;
                } 
                if(map[i][j]=='E'){
                    ex=i;ey=j;
                }
            } 
        } 
        int tx,ty;
        for(int i=0;i<4;i++){
            tx=bx+move[i][0];
            ty=by+move[i][1];
            if(!check(tx,ty)) continue;
            
            if(map[tx][ty]=='.'){
                dfsl(bx,by,i);
                
                ans=num=1;
                dfsr(bx,by,i);
                
                ans=num=1;
                ttt.x=bx;
                ttt.y=by;
                ttt.steps=0;
                q.push(ttt);
                memset(vis,0,sizeof(vis));
                vis[bx][by]=1;
                bfs(); 
                break;
            }
            
        }
        cout<<endl;
    }
    return 0;
}

 

posted @ 2018-03-29 15:40  柳暗花明_liu  阅读(140)  评论(0编辑  收藏  举报