SPOJ Goblin Wars (Simple BFS)

Source: Internet
Author: User

J-Goblin WarsTime limit:432MS Memory Limit:1572864KB 64bit IO Format:%lld &A mp %llu SubmitStatusPracticeSpoj amr11j

Description

The Wizards and Witches of Hogwarts School of witchcraft found Prof. Binn ' s History of Magic lesson to being no less boring t  Han you found your own classes. Recently Binns have been droning on about Goblin wars, and which Goblin civilization fought which group of Centaurs where E  TC etc. The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of me  Morizing the results for their upcoming exams. Can you help them?

Civilization fought which group of centaurs where etc etc.  the students of Hogwarts decided to use the new-fangled C Omputer to figure out the outcome of all these wars instead of memorizing, the results for their upcoming exams.  can help them? The magical world looks like a 2-d r*c grid. Initially there is many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There is also certain cells that is uninhabitable (swamps, mountains, sinkholes etc)-these cells is denoted by a ' # ' In the grid. All of the other cells-to which the civilizations can move  -is represented by a '. ' In the grid. A cell is said to being 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 is within the boundaries of the grid.   Every year civilization would 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, or more civilizations could move into an unoccupied cell at the same time-this would leads to a batt Le between the civilizations and the cell would be marked with a ' * '. Note that the civilizations fighting in a particular cell does not try to expand from that cell, but would continue to expand From the 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 was followed by T-test case blocks. Each test case contains the integers, R, C. This was 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 the year 0.Each cells is either a 1. '. ' which represents an unoccupied cell 2. ' # ' which represents a cell that cannot is 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 on that particular cell. p Rint a blank line at the end of each case. Constraints:1 <= R, C <= 1 <= T <= 5 time Limit:  3 s Memory limit:64 MB 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 # # A*b #d # #c # ACB a*b aa*bb a#.# AA*BB

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

A cell is said to being 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 is within the boundaries of the grid. Every year civilization would expand to all unoccupied adjacent cells. If It is already inhabited by some and other civilization, it just leaves the cell alone. It is possible, that, or more civilizations could move into an unoccupied cell at the same time-this would leads to a batt Le between the civilizations and the cell would be marked with a ' * '. Note that the civilizations fighting in a particular cell does not try to expand from that cell, but would continue to expand From the 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 was followed by T-test case blocks.

Each test case contains the integers, R, C.

This was 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 and the input, use ' * ' to denote, a battle is being waged on 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

####

7 ·

%ce

A.b

#d #

7 ·

%ce

...

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


The training game has the great God made, learned a time array of use, each letter in turn BFS fill, when the same point to see whether their time state is the same, if the same will definitely conflict. Vist array omitted. Just in BFS

The process does not let him encounter the same letter.

#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace Std;const int Inf=510;char    Cnt[inf][inf];int time4[510][510];int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};int n,m;struct Node{int x,y,t;    char c; Node (int x,int y,int T,char C): X (x), Y (y), T (t), C (c) {}};queue<node>q;void BFS () {while (!q.empty ()) {Nod        e E=q.front ();        Q.pop (); if (cnt[e.x][e.y]== ' * ') continue;            for (int i=0;i<4;i++) {int tx=dir[i][0]+e.x;            int ty=dir[i][1]+e.y; if (tx>=1&&tx<=n&&ty>=1&&ty<=m&&cnt[tx][ty]!= ' # ' && cnt[tx][               TY]!=CNT[E.X][E.Y]) {if (cnt[tx][ty]== '. ')    {cnt[tx][ty]=e.c;               time4[tx][ty]=e.t+1;               Q.push (Node (tx,ty,e.t+1,cnt[tx][ty));                        } else {if (time4[tx][ty]==e.t+1) {                    cnt[tx][ty]= ' * ';    }}}}}}int main () {int t;cin>>t;        while (t--) {while (!q.empty ()) Q.pop ();        cin>>n>>m;        memset (time4,0,sizeof (time4));            for (int i=1;i<=n;i++) {scanf ("%s", cnt[i]+1);                    for (int j=1;j<=m;j++) {if (cnt[i][j]>= ' a ' &&cnt[i][j]<= ' Z ') {                Q.push (Node (i,j,0,cnt[i][j));        }}} BFS ();    for (int i=1;i<=n;i++) printf ("%s\n", cnt[i]+1); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SPOJ Goblin Wars (Simple BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.