spoj Goblin Wars(简单bfs)

J - Goblin Wars

Time Limit:432MS    Memory Limit:1572864KB    64bit IO Format:%lld & %llu

SubmitStatusPracticeSPOJ
AMR11J

Description

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?

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

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

####

#c#

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 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填充,当到达同一点时看他们时间状态是否相同,如果相同则肯定冲突。vist数组省略。只需在bfs

过程中不让他遇到相同字母。

#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())
    {

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-29 19:02:08

spoj Goblin Wars(简单bfs)的相关文章

Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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 cla

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

【POJ 3669 Meteor Shower】简单BFS

流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封锁).安全地带为永远不会被封锁的点. 简单bfs,开始WA在把平面空间上限当成300*300,但根据题目,这只是有流星雨撞击的范围.实际可走的空间理论上没上限,但分析可得,离原点最近的安全地带一定在(302,302)范围内,所以应可把数组至少开为303*303. 后来WA在把G[0][0]==1的情

LightOJ 1012 简单bfs,水

1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) using names

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

SPOJ 206 BITMAP(BFS+剪枝)

SPOJ 206 BITMAP(BFS+剪枝) ACM 题目地址:SPOJ 206 BITMAP 题意: 给出一个矩阵,有黑白点,计算每个点离最近的白点的距离,p1=(i1,j1) and p2=(i2,j2),距离d(p1,p2)=|i1-i2|+|j1-j2|. 分析: 有剪枝的BFS,如果从黑色的开始进行BFS最近的白色,复杂度是O(n^4),复杂度无法接受. 于是想把黑色白色分开记录下来,然后两遍for,发现复杂度不变... 从黑色开始搜,如果要记忆化的话,貌似很麻烦,但是我们可以从白色

poj 1562 简单 bfs

// 简单 bfs #include <iostream>#include<fstream>using namespace std; char map[110][110];int flag[110][110];int qu[11000][2],qe,qs,m,n;int add[8][2]={-1,-1,  -1,0, -1,1,   0,-1,  0,1,  1,-1,    1,0,   1,1 }; void bfs(int r,int c){    int i,tr,tc;

POJ3185(简单BFS,主要做测试使用)

没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实这里我是很不明白的,,TM状态总数就只有1e6怎么也不应该超时的,,,,只能说STL的queue的常数实在是太大,完全没法弄...) 1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <qu

逃脱 (简单BFS)

题目传送门 G逃脱  题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园某处的mengxiang000和Tabris听到了火灾警报声的同时拔腿就跑,不知道两人是否能够逃脱险境? 幼儿园可以看成是一个N*M的图,在图中一共包含以下几种元素: “.”:表示这是一块空地,是可以随意穿梭的. “#”:表示这是一块墙,是不可以走到这上边来的,但是可以被火烧毁. “S”:表示men