[kuangbin带你飞]专题一 简单搜索 I FZU 2150

来源:互联网 发布:可靠性软件招标2016 编辑:程序博客网 时间:2024/06/06 12:59

题目地址:https://vjudge.net/contest/65959#problem/I

思路:之前福大oj崩了,不知道对不对就没写这题题解了,现在补一下。从两个人所有每个可能的起点位置走一次bfs,找到最小值。福大平台不支持c++11,所以编译错误了无数次……坑啊

AC代码:

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn=15;char map[maxn][maxn];int t;int m,n;bool vis[maxn][maxn];struct pos{    int x,y,d;};int xx[4]={-1,0,1,0};int yy[4]={0,-1,0,1};   vector<pos>s;bool ok(){    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)    {        if(map[i][j]=='#' && !vis[i][j])            return false;    }    return true;}int bfs(pos a,pos b){    queue<pos>q;    memset(vis,false,sizeof(vis));    q.push(a),q.push(b);    vis[a.x][a.y]=vis[b.x][b.y]=true;    int d;    while(!q.empty())    {        pos now=q.front();        q.pop();        int x=now.x;        int y=now.y;        d=now.d;        for(int i=0;i<4;i++)        {            int tempx=x+xx[i];            int tempy=y+yy[i];            if(tempx>=0 && tempx<n && tempy>=0 &&tempy<m)                if(!vis[tempx][tempy] && map[tempx][tempy]=='#')            {                vis[tempx][tempy]=true;                pos temp;                temp.x=tempx,temp.y=tempy,temp.d=now.d+1;                q.push(temp);            }        }    }    return d;}int main(){    scanf("%d",&t);    for(int casei=1;casei<=t;casei++)    {     s.clear();        memset(map,0,sizeof(map));    scanf("%d%d",&n,&m);    for(int i=0;i<n;i++)    {       scanf("%s",map[i]);       for(int j=0;j<m;j++)       {           if(map[i][j]=='#')           {              pos temp;           temp.x=i,temp.y=j,temp.d=0;            s.push_back(temp);           }       }    }    int minn=0x3f3f3f3f;       for(int i=0;i<s.size();i++)       {           for(int j=i;j<s.size();j++)           {              int count= bfs(s[i],s[j]);               if(ok())                minn=min(minn,count);           }       }       if(minn==0x3f3f3f3f)        minn=-1;        printf("Case %d: %d\n",casei,minn);    }    return 0;}


0 0
原创粉丝点击