哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)G(BFS)

题目链接:https://www.nowcoder.com/acm/contest/27/G

思路:预处理S点和*点,按顺序push进队列,然后BFS,判断对头是否有火,若有则扩展火的移动,若无则扩展人的移动。这里建一个huo[][]数组来存储每个点火的状态,如果已经是1了就不能再进队列了!

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1000;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
char g[35][35],vis[35][35],huo[35][35];
struct point
{
    int x,y,step,ifh;
};
int hdx[]= {0,0,-1,-1,-1,1,1,1};
int hdy[]= {-1,1,-1,0,1,-1,0,1};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
point s,e,h;
int n,m;
void bfs()
{
    memset(vis,0,sizeof(vis));
    memset(huo,0,sizeof(huo));
    queue<point> q;
    q.push(s);
    q.push(h);
    vis[s.x][s.y]=1;
    huo[h.x][h.y]=1;
    while(!q.empty())
    {
        point f=q.front();
        q.pop();
        if(f.ifh==0)
        {
            if(g[f.x][f.y]=='E')
            {
                cout<<f.step<<endl;
                return ;
            }
            if(huo[f.x][f.y])
                continue;
            for(int i=0; i<4; i++)
            {
                point te=f;
                int x=f.x+dx[i];
                int y=f.y+dy[i];
                if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&g[x][y]!='#'&&!huo[x][y])
                {
                    vis[x][y]=1;
                    te.x=x;
                    te.y=y;
                    te.step++;
                    q.push(te);
                }
            }
        }
        else
        {
            for(int i=0; i<8; i++)
            {
                point te=f;
                int x=f.x+hdx[i];
                int y=f.y+hdy[i];
                if(x>=0&&x<n&&y>=0&&y<m&&!huo[x][y])
                {
                    huo[x][y]=1;
                    te.x=x;
                    te.y=y;
                    q.push(te);
                }
            }
        }
        /*    for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    cout<<g[i][j]<<" ";
                }
                cout<<endl;
            }*/
    }

        cout<<"T_T"<<endl;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>g[i][j];
                if(g[i][j]=='S')
                {
                    s.x=i;
                    s.y=j;
                    s.step=0;
                    s.ifh=0;
                }
                if(g[i][j]=='*')
                {
                    h.x=i;
                    h.y=j;
                    h.ifh=1;
                }
            }
        }

        bfs();
        /*     for(int i=0; i<n; i++)
             {
                 for(int j=0; j<m; j++)
                 {
                     cout<<g[i][j]<<" ";
                 }
                 cout<<endl;
             }*/
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/82320932
今日推荐