搜索中的转弯问题

</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
在这个问题中涉及到人走后转弯的问题,搜索的难度并不是很大,但是还是要好好理解。主要的优点在于用一个vis【】【】去记录到达每个点后的已经走过的弯道
 
数,这个在里面尤其要注意
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace  std;
#define maxn 110
char map[maxn][maxn];
int vis[maxn][maxn];
struct node{
   int x;
   int y;
   int count;
   int dir;
};
int di[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int x1,y1,x2,y2;
int n,m,k;

int  BFS()
{
    node st,en;
    queue<node>q;
    st.x=y1-1;st.y=x1-1;st.count=0;st.dir=-1;
    vis[y1-1][x1-1]=0;
    q.push(st);
    while(!q.empty())
    {
         st=q.front();
         q.pop();
       //  printf("%d %d\n",st.x,st.y);
         if(st.x==y2-1&&st.y==x2-1)
                return 1;
         for(int i=0;i<=3;i++)
         {
             int y3=st.x+di[i][0];
             int x3=st.y+di[i][1];
           //  printf("x3=%d  x3=%d\n",x3,y3);
             int count=st.count;
             int dir=st.dir;
             if(x3>=0&&x3<n&&y3>=0&&y3<m&&map[y3][x3]=='.')
             {
                   if(dir!=i&&dir!=-1)
                   {
                       dir=i;
                       count++;
                   }
                   if(dir==-1)
                    dir=i;
                  //  printf("%d\n",count);
                    if(count>k)
                     continue;
                    if(count<=vis[y3][x3])//当我们的广度搜索已经超过了他他以前走过的弯道数的时候就没有必要再把点放到队列中
                    {
                        vis[y3][x3]=count;
                        en.x=y3;en.y=x3;en.count=count;en.dir=dir;
                        q.push(en);
                    }
             }
         }
    }
    return 0;
}


int main()
{
    int T;
  //  freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
         scanf("%d%d",&m,&n);
         for(int i=0;i<m;i++)
         {
             scanf("%s",&map[i]);
         }
         for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
         {
             vis[i][j]=1000000;
         }
         scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
        if( BFS()==1)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值