广搜总结(Kuangbin 搜索专题)

B - Dungeon Master  POJ - 2251 

 这道题就是广搜模板题,只是变换了一些条件,例如变成三维的。思路和广搜题目思路一样。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=40;
char map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
struct point{
  int x,y,z;
  int step;
};
int nex[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int sx,sy,sz,ex,ey,ez,ans;
int l,r,c;
int bfs()
{
  point st,next;
  st.x=sx;
  st.y=sy;
  st.z=sz;
  st.step=0;
  vis[sx][sy][sz]=1;
  queue<point>q;
  q.push(st);
  while(!q.empty())
  {
    st=q.front();
    q.pop();
    if(st.x==ex&&st.y==ey&&st.z==ez)
    {
      return st.step;
    }
    for(int k=0;k<6;k++)
    {
      next.x=st.x+nex[k][0];
      next.y=st.y+nex[k][1];
      next.z=st.z+nex[k][2];
      if(vis[next.x][next.y][next.z]==0&&next.x>=0&&next.x<l
        &&next.y>=0&&next.y<r&&next.z>=0&&next.z<c&&
        map[next.x][next.y][next.z]!='#')
        {
          vis[next.x][next.y][next.z]=1;
          next.step=st.step+1;
          q.push(next);
        }
    }
  }
  return 0;
}
int main()
{
  while(cin>>l>>r>>c)
  {
    ans=0;
    if(l==0&&r==0&&c==0)
    {
      break;
    }
    memset(vis,0,sizeof(vis));
    for(int i=0;i<l;i++)
    {
      for(int j=0;j<r;j++)
      {
        for(int k=0;k<c;k++)
        {
          cin>>map[i][j][k];
          if(map[i][j][k]=='S')
          {
            sx=i;
            sy=j;
            sz=k;
          }
          else if(map[i][j][k]=='E')
          {
            ex=i;
            ey=j;
            ez=k;
          }
        }
      }
    }
    ans=bfs();
  //  cout<<ans<<endl;
    if(ans)
    {
      printf("Escaped in %d minute(s).\n",ans);
    }
    else
    {
      printf("Trapped!\n");
    }
  }
  return 0;
}

 

C - Catch That Cow      POJ - 3278  

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100001;
struct point
{
  int x;
  int step;
};
int n,k;
int vis[maxn];
int bfs()
{
  point st,next;
  st.x=n;
  st.step=0;
  vis[st.x]=1;
  queue<point>q;
  q.push(st);
  while(!q.empty())
  {
    st=q.front();
    q.pop();
    if(st.x==k)
    {
      return st.step;
    }
    for(int i=0;i<3;i++)
    {
      if(i==0)next.x=st.x-1;
      if(i==1)next.x=st.x+1;
      if(i==2)next.x=st.x*2;
      if(next.x>maxn||next.x<0)
         continue;
      if(!vis[next.x])
      {
        next.step=st.step+1;
        vis[next.x]=1;
        q.push(next);
      }
      if(next.step==k)
      {
        return next.step;
      }
    }
  }
  return 0;
}
int main()
{
  while(cin>>n>>k)
  {
    memset(vis,0,sizeof(vis));
    if(n<k)
    {
      cout<<bfs()<<endl;
    }
    else
    {
      cout<<n-k<<endl;
    }
  }
  return 0;
}

 有一点需要注意,就是枚举i==0时,步数加1,i==1,步数减1,i==2时,st.x*2,这都是一样的,不分先后,i==0,也可以st.x*2,是枚举的问题,它只是列出三种情况,不分先后。

D - Fliptile   POJ - 3279 

 

F - Prime Path   POJ - 3126  

 

G - Shuffle'm Up  POJ - 3087  

 

H - Pots   POJ - 3414  

I - Fire Game FZU - 2150  

 双起点BFS,思路:首先题意是选择两个起点bfs.另外需要剪枝,数据量貌似很多,所以不剪枝绝对超时。

 Firstly they choose two grids which are consisting of grass and set fire

ends when no new grid get fire.

output the minimal time they need to wait after they set fire。这是需要注意的几个点。两个起点意味着如果起点多了该如何处理

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=21;
const int INF=0x3f3f3f3f;
int n,m;
struct point
{
  int x;
  int y;
  int step;
}node[101];
int vis[maxn][maxn];
char map[maxn][maxn];
int nex[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool judge(int x,int y)
{
  if(x<0||x>=n||y<0||y>=m)
  {
    return false;
  }
  if(vis[x][y]==1||map[x][y]=='.')
  {
    return false;
  }
  return true;
}
point bfs(point s1,point s2)
{
  point cur,next;
  queue<point>q;
  q.push(s1);
  q.push(s2);
  vis[s1.x][s1.y]=1;
  vis[s2.x][s2.y]=1;
  int man=0;
  while(!q.empty())
  {
    cur=q.front();
    q.pop();
    man=max(cur.step,man);//??????
    for(int k=0;k<4;k++)
    {
      next.x=cur.x+nex[k][0];
      next.y=cur.y+nex[k][1];
      if(!judge(next.x,next.y))
           continue;
      vis[next.x][next.y]=1;
      next.step=cur.step+1;
      q.push(next);
    }
  }
  return cur;
}
int main()
{
  int t,cnt=1,sum;
  cin>>t;
  while(t--)
  {
    cin>>n>>m;
    sum=0;
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        cin>>map[i][j];
        if(map[i][j]=='#')
        {
          node[sum].x=i;
          node[sum].y=j;
          sum++;
        }
      }
    }
    int cur=0,ans=INF;
    if(sum<=2)
    {
      cout<<"Case "<<cnt<<": "<<0<<endl;
    }
    else
   {
    for(int i=0;i<sum;i++)
    {
      for(int j=0;j<sum;j++)
      {
        if(i!=j)
        {
          memset(vis,0,sizeof(vis));
          int flag=1;
          cur=bfs(node[i],node[j]).step;
          for(int i=0;i<n;i++)
          {
            for(int j=0;j<m;j++)
            {
              if(map[i][j]=='#'&&!vis[i][j])
              {
                flag=0;
                break;
              }
            }
          }
          if(flag)
          {
            ans=min(ans,cur);
          }
        }
      }
    }
    if(ans==INF)
    {
      cout<<"Case "<<cnt<<": "<<-1<<endl;
    }
    else
     {
      cout<<"Case "<<cnt<<": "<<ans<<endl;
     }
   }
    cnt++;
  }
  return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值