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;
}

Time90ms
Memory17408kB
Length1899


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章