HDOJ/HDU 1728 逃離迷宮 DFS 深度優先搜素

來源:互聯網
上載者:User

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1728

深搜思路:記錄4個方向和拐彎次數,若拐彎3次,則必須是當前點和終點在一條直線才滿足要求,否則剪掉,這樣也逾時,坑爹,之前連連看那個題就是用這種方法也能過,z這次的資料有點強,100*100了,所以深搜不行,不過這思路倒不錯。

代碼:

#include <iostream>#include <cmath>#include <string>#include <algorithm>#include <vector>#include <fstream>using namespace std;int m,n,k,sx,sy,endx,endy;int dir[4][2]={-1,0,1,0,0,1,0,-1};char maze[101][101];bool visit[101][101];bool flag;void dfs(int x,int y,int dir,int turn){//cout<<"x: "<<x<<" y: "<<y<<endl;if( x<=0||x>m||y<=0||y>n || maze[x][y]=='*')return ;if(x==endx&&y==endy&&turn<=k){flag=1;return ;}if(flag)return ;if(turn>k)return ; if(turn==k) //剪枝,此時拐彎已經k次,若沒在一條直線上則剪掉 {if(!(dir==1&&x>endx&&y==endy || dir==2&&x<endx&&y==endy || dir==3&&x==endx&&y>endy || dir==4&&x==endx&&y<endy))return ;}if(visit[x][y])return ;visit[x][y]=1;if(dir==1){      //往上走,用dir==1表示,此時三種情況,1:繼續往上走,不拐//2:往右走,拐一次,3:往左走,拐一次。以下類似。dfs(x-1,y,1,turn);          dfs(x,y-1,3,turn+1);          dfs(x,y+1,4,turn+1);  }else if(dir==2){          dfs(x+1,y,2,turn);          dfs(x,y-1,3,turn+1);          dfs(x,y+1,4,turn+1);      }      else if(dir==3){          dfs(x-1,y,1,turn+1);          dfs(x+1,y,2,turn+1);          dfs(x,y-1,3,turn);      }      else if(dir==4){          dfs(x-1,y,1,turn+1);          dfs(x+1,y,2,turn+1);          dfs(x,y+1,4,turn);      }      visit[x][y]=0; }int main(){int t;//ifstream fin;//fin.open("abc.txt");//cout<<fin.is_open()<<endl;scanf("%d",&t);//cin>>t;while(t--){cin>>m>>n;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){cin>>maze[i][j];}}scanf("%d %d %d %d %d",&k,&sy,&sx,&endy,&endx);if(sx==endx&&sy==endy){cout<<"yes"<<endl;continue;}memset(visit,0,sizeof(visit));flag=0;visit[sx][sy]=1;dfs(sx-1,sy,1,0); //go up,dir==1dfs(sx+1,sy,2,0); //go down ,dir==2dfs(sx,sy-1,3,0); //go left,dir==3dfs(sx,sy+1,4,0); //go right,dir==4if(flag)cout<<"yes"<<endl;else cout<<"no"<<endl;}return 0;}

下面改為廣搜了,不過也需要一定的技巧,防止記憶體超,這題在網上看瞭解題報告之後tricky是沿著一個方向搜尋下去,這樣的拐彎次數最少,優先這樣的點入隊,可以提高不少時間:AC 31MS

轉自:http://blog.csdn.net/sdc1992/article/details/8825729

代碼:

#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{    int x,y;}now,tmp;queue<struct node>q;int m,n;char maze[111][111];int vis[111][111];int k,bx,by,ex,ey;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};bool bfs(){    while(!q.empty())        q.pop();    now.x=bx;now.y=by;    q.push(now);    while(!q.empty())    {        now=q.front();        for(int i=0;i<4;i++)        {            tmp.x=now.x+dx[i];            tmp.y=now.y+dy[i];            while(tmp.x>0&&tmp.y>0&&tmp.x<=m&&tmp.y<=n&&maze[tmp.x][tmp.y]!='*')            {                if(vis[tmp.x][tmp.y]==-1)                {                    q.push(tmp);                    vis[tmp.x][tmp.y]=vis[now.x][now.y]+1;                    if(tmp.x==ex&&tmp.y==ey)                        return true;                }                tmp.x+=dx[i];tmp.y+=dy[i];            }        }        q.pop();    }    return false;}int main(){    int t;    while(scanf("%d",&t)==1)    {        while(t--)        {            memset(vis,-1,sizeof(vis));            scanf("%d %d",&m,&n);            int i;            for(i=1;i<=m;i++)                scanf("%s",maze[i]+1);            scanf("%d %d %d %d %d",&k,&by,&bx,&ey,&ex);            if(bx==ex&&by==ey)            {                printf("yes\n");                continue;            }            if(bfs())            {                if(vis[ex][ey]<=k)                    printf("yes\n");                else                    printf("no\n");            }            else                printf("no\n");        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.