SPOJ - AMR11J (BFS,标记活用)

The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson to be no less boring than you found your own history classes.  Recently Binns has been droning on about Goblin wars, and which goblin civilization fought which group of centaurs where etc etc.  The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams.  Can you help them?

The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a '#' in the grid. All the other cells - to which the civilizations can move  - are represented by a '.' in the grid.

A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid.   Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a '*'. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.

Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.

 

Input (STDIN):

The first line contains T, the number of cases. This is followed by T test case blocks.

Each test case contains two integers, R, C.

This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.

Each cell is either a

1. '.' which represents an unoccupied cell

2. '#' which represents a cell that cannot be occupied

3. A civilization represented by a lowercase letter ('a' - 'z')

 

Output (STDOUT):

For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use '*' to denote that a battle is being waged in that particular cell. 

Print a blank line at the end of each case.

 

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

题意:

     给你一副地图,上面一种字母表示一种文明,问随着每种文明扩张后,最终地图成了什么样在。每种文明扩张速度一样,文明之间不能蚕食(不科学(滑稽)),但当俩文明同一时间到到达同一点时,那一点会成为边界‘*’。

思路:

    多起点BFS,难点不在于扩张而在于形成边界。我们可以把标记vir活用,不仅用于记录是否“占领”过,还可以记录该位置是什么时候别标记的,用于判断是非在不同文明间同一时间被到达!

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int n,m;
int dxy[4][2] = { 0,1,-1,0,0,-1,1,0 } ;
int vir[505][505] ;
char maze[505][505] ;
bool check( int x, int y )
{
    if( x<0 || y<0 || x>=n || y>=m || maze[x][y]=='#' )
    return false;
    return true;
}

struct Q
{
    int x,y,s;
    char c;
}no,de;
queue<Q> iq ;

void bfs( )
{
    while( !iq.empty() )
    {
        no = iq.front();
        iq.pop();
        if( maze[no.x][no.y] == '*' ) 
        continue;
        
        de.s = no.s + 1 ;
        de.c = no.c ;
        for( int i=0; i<4; i++ )
        {
            de.x = no.x + dxy[i][0];
            de.y = no.y + dxy[i][1];
            if( check(de.x,de.y) )
            {
                if( vir[de.x][de.y] == de.s && maze[de.x][de.y]!=de.c ) //条件缺一不可,不同文明,同一时间
                {
                    maze[de.x][de.y] = '*';
                }
                else if( vir[de.x][de.y]==0 )
                {
                    vir[de.x][de.y] = de.s ;
                    maze[de.x][de.y] = de.c ;
                    iq.push(de);
                }
            }
        }
    }
    for( int i=0; i<n; i++ )
    {
        for( int j=0; j<m; j++ )
        {
            printf("%c",maze[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while( T-- )
    {
        while( !iq.empty() )
        iq.pop();
        memset(vir,0,sizeof(vir));
        scanf("%d%d",&n,&m);
        for( int i=0; i<n; i++ )
        {
            scanf("%s",&maze[i]);
            for(int j=0; j<m; j++ )
            {
                if( maze[i][j]>='a' && maze[i][j] <='z' )
                {
                    no.x=i,no.y=j,no.c=maze[i][j],no.s=1;
                    vir[i][j] = 1;
                    iq.push(no);
                }
            }
        }
        bfs();
        printf("\n");
    }
    return 0;
}

Time 90ms
Memory 17408kB
Length 1899


猜你喜欢

转载自blog.csdn.net/qq_40764917/article/details/80977186