uvalive5992(搜索)

题意:

给出一个n*m的图,‘.’可以走,‘#’不能走,图中有字母‘a’~‘z’,每个时间每个字母会向四联通的四个格子扩散,如果两个字母相遇,那个格子会变成‘*’,问最后的图长什么样。


思路:

从每个字母开始搜,记录一下就好了。


代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<climits>
#include<queue>

using namespace std;

int n,m;
char maze[505][505];
int vis[505][505];
int a[]= {1,-1,0,0};
int b[]= {0,0,1,-1};

struct Point
{
    int x,y,r;
    char ans;
    Point(int xx,int yy,char anss,int rr)
    {
        x=xx;
        y=yy;
        ans=anss;
        r=rr;
    }
};

void bfs()
{
    queue<Point> q;

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(maze[i][j] >= 'a' && maze[i][j] <= 'z')
                q.push(Point(i,j,maze[i][j],0));

    while(!q.empty())
    {
        Point t = q.front();
        q.pop();
        int x = t.x;
        int y = t.y;
        int r = t.r;
        char ans = t.ans;
        if(maze[x][y] == '*') continue;
        for(int i=0; i<4; i++)
        {
            if(x + a[i] <= n && x + a[i] >= 1 && y + b[i] >= 1 && y + b[i] <= m)
            {
                if(maze[x + a[i]][y + b[i]] == '.')
                {
                    vis[x + a[i]][y + b[i]] = r + 1;
                    maze[x + a[i]][y + b[i]] = ans;
                    q.push(Point(x + a[i],y + b[i],ans,r+1));
                }
                else
                {
                    if(maze[x + a[i]][y + b[i]] >= 'a' || maze[x + a[i]][y + b[i]] <= 'z')
                    {
                        if(vis[x + a[i]][y + b[i]] )
                        {
                            if(vis[x + a[i]][y + b[i]] == r + 1 && ans != maze[x + a[i]][y + b[i]])
                            {
                                maze[x + a[i]][y + b[i]] = '*';
                            }
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%s",maze[i] + 1);

        memset(vis,0,sizeof(vis));
        bfs();
        for(int i=1; i<=n; i++)
            printf("%s\n",maze[i]+1);

        cout<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值