zoukankan      html  css  js  c++  java
  • Training #2 cell battle (BFS)

    Constraints:

    1 <= R, C <= 500

    1 <= T <= 5

    Sample Input:

    5

    3 5

    #####

    a...b

    #####

    3 4

    ####

    a..b

    ####

    3 3

    #c#

    a.b

    #d#

    3 3

    #c#

    ...

    a.b

    3 5

    .....

    .#.#.

    a...b

    Sample Output:

    #####

    aa*bb

    #####

    ####

    aabb

    ####

    #c#

    a*b

    #d#

    #c#

    acb

    a*b

    aa*bb

    a#.#b

    aa*bb

    #include <bits/stdc++.h>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define foror(i,a,b) for(i=a;i<b;i++)
    #define foror2(i,a,b) for(i=a;i>b;i--)
    #define EPS 1e-9
    #define PI acos(-1.0)
    #define INF 1000000000
    #define MOD 1000000007
    #define mem(a,b) memset((a),b,sizeof(a))
    #define TS printf("!!!
    ")
    #define lson o<<1, l, m
    #define rson o<<1|1, m+1, r
    using ll = long long;
    using ull= unsigned long long;
    //std::ios::sync_with_stdio(false);
    using namespace std;
    char s[505][505];
    int visit[505][505];
    int dx[4]={-1,0,0,1};
    int dy[4]={0,-1,1,0};
    int a,b;
    int sum;
    struct point{
     int x,y;
     char ch;
     int num;
    };
    queue<point> q;
    bool check(point jqk)
    {
     if((jqk.x>=1&&jqk.x<=a)&&(jqk.y>=1&&jqk.y<=b)&&(s[jqk.x][jqk.y]!='#')&&(s[jqk.x][jqk.y]!='*'))
     return true;
     return false;
    }
    char readchar()
    {
        char tmp = 0;
        char ch;
        bool read = false;
        while (ch = getchar())
        {
            if ((ch >='a'&&ch<='z')||ch=='#'||ch=='.')
            {
                read = true;
                tmp = ch;
            }
            else if (read)
            {
                break;
            }
            else
            {
                continue;
            }
        }
        return tmp;
    }
    void bfs()
    {
     while(!q.empty())
     {
      point aim=q.front();
      q.pop();
      //cout << aim.ch << " ";
      if(s[aim.x][aim.y]!='*')
      for(int i=0;i<=3;i++)
      {
            point cur;
            cur.x=aim.x+dx[i];
            cur.y=aim.y+dy[i];
            cur.ch=aim.ch;
            cur.num=aim.num+1;
            if(check(cur))
            {
            if(s[cur.x][cur.y]>='a'&&s[cur.x][cur.y]<='z')
            {
            if(s[cur.x][cur.y]==cur.ch)
            continue;
            else
            {
            if(visit[cur.x][cur.y]!=cur.num)
            continue;
            else
            {
            s[cur.x][cur.y]='*';
            }
            }
            }
            else if(s[cur.x][cur.y]=='.')
            {
            s[cur.x][cur.y]=cur.ch;
            visit[cur.x][cur.y]=cur.num;
            q.push(cur);
            }
            }
      }
     }
    }
    int main()
    {
     //freopen("in.txt", "r", stdin);
     //freopen("out.txt", "w", stdout);
     int T;
     cin >>T;
     while(T--)
     {
             mem(visit,0);
             char pop;
             scanf("%d %d",&a,&b);
             point aim;
             for(int i=1;i<=a;i++)
                    scanf("%s",s[i]+1);
             /*for(int i=1;i<=a;i++)
                    printf("%s
    ",s[i]+1);*/
             for(int i=1;i<=a;i++)
                    for(int j=1;j<=b;j++)
             {
                    pop=s[i][j];
                    if(pop!='#'&&pop!='.')
                    {
                    aim.x=i;
                    aim.y=j;
                    aim.ch=pop;
                    aim.num=1;
                    q.push(aim);
                    visit[i][j]=1;
                    }
             }
             if(!q.empty())
                    bfs();
             for(int i=1;i<=a;i++)
                    printf("%s
    ",s[i]+1);
                    printf("
    ");
                    printf("
    ");
    
     }
     //fclose(stdin);
     //fclose(stdout);
     return 0;
    }
  • 相关阅读:
    上下伸展的JS菜单
    [ZZ]Debug VBScript with Visual Studio
    面试总结之杂题
    [ZZ]9 Confusing Naming Conventions for Beginners
    Robocopy
    [ZZ]什么是Alpha,Beta,RC,RTM,CTP版
    使用位运算交换两个值,不用临时变量
    学习笔记之编程之美微软技术面试心得(一)
    C#中如何获取系统环境变量
    学习笔记之SQL教程 from W3School
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7065766.html
Copyright © 2011-2022 走看看