陈老师的多校联合20140816||spoj10237 bfs
2014-08-17 09:28
417 查看
http://www.spoj.com/problems/AMR11J/
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
题目大意:
给你一个n*m的棋盘,每一个位置有一个字符。若是a~z字符就从该字符向其上下左右四个方向进行拓展(即如果其四个方向是字符“.”的话,就变成该字符,新的字符可继续向四周拓展),如果有两个字母同时拓展到一个位置产生冲突的话就把冲突位置用*代替,并且该位置不能继续拓展。
解题思路:
这题一看就知道用bfs来做,可关键是细节的处理。我们另外建立一个v[][]数组存储状态。先遍历一遍若原字符为字母就领v[i][j]变为1,入队列,为“.”就变成inf,为#就变成0.然后对原棋盘进行宽度优先搜索,遇到字母就搜其四周,为“.”就变为该字母,v数组为该字母原位置的值加1,还为为字母就比较该位置v值是否为原位置的+1,若是且二字母并不相同,就说明该位置由另一个字母拓展而来应变为*,并且v置0.知道队列为空跳出循环,程序结束。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
const int inf=0x3f3f3f;
queue<pair<int,int> >p;
int n,m,ans;
char a[550][550];
int v[550][550];
int px[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
void bfs()
{
while(!p.empty())
{
int i=p.front().first;
int j=p.front().second;
p.pop();
if(v[i][j] == 0 ) continue;
char t=a[i][j];
for(int k=0; k<4; k++)
{
int xx=px[k][0]+i;
int yy=px[k][1]+j;
if(v[xx][yy]!=0)
{
if(v[xx][yy]==inf)
{
a[xx][yy]=t;
v[xx][yy]=v[i][j]+1;
p.push(make_pair(xx,yy));
}
else if(v[xx][yy]==v[i][j]+1&&a[xx][yy]!=t)
{
a[xx][yy]='*';
v[xx][yy]=0;
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%*c",&n,&m);
memset(a,0,sizeof(a));
ans=0;
while(!p.empty())
p.pop();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%c",&a[i][j]);
}
getchar();
}
memset(v,0,sizeof(v));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j]=='.')
v[i][j]=inf;
else if(a[i][j]>='a'&&a[i][j]<='z')
{
v[i][j]=1;
p.push(make_pair(i,j));
}
}
}
bfs();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}
/**
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
**/
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
题目大意:
给你一个n*m的棋盘,每一个位置有一个字符。若是a~z字符就从该字符向其上下左右四个方向进行拓展(即如果其四个方向是字符“.”的话,就变成该字符,新的字符可继续向四周拓展),如果有两个字母同时拓展到一个位置产生冲突的话就把冲突位置用*代替,并且该位置不能继续拓展。
解题思路:
这题一看就知道用bfs来做,可关键是细节的处理。我们另外建立一个v[][]数组存储状态。先遍历一遍若原字符为字母就领v[i][j]变为1,入队列,为“.”就变成inf,为#就变成0.然后对原棋盘进行宽度优先搜索,遇到字母就搜其四周,为“.”就变为该字母,v数组为该字母原位置的值加1,还为为字母就比较该位置v值是否为原位置的+1,若是且二字母并不相同,就说明该位置由另一个字母拓展而来应变为*,并且v置0.知道队列为空跳出循环,程序结束。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
const int inf=0x3f3f3f;
queue<pair<int,int> >p;
int n,m,ans;
char a[550][550];
int v[550][550];
int px[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
void bfs()
{
while(!p.empty())
{
int i=p.front().first;
int j=p.front().second;
p.pop();
if(v[i][j] == 0 ) continue;
char t=a[i][j];
for(int k=0; k<4; k++)
{
int xx=px[k][0]+i;
int yy=px[k][1]+j;
if(v[xx][yy]!=0)
{
if(v[xx][yy]==inf)
{
a[xx][yy]=t;
v[xx][yy]=v[i][j]+1;
p.push(make_pair(xx,yy));
}
else if(v[xx][yy]==v[i][j]+1&&a[xx][yy]!=t)
{
a[xx][yy]='*';
v[xx][yy]=0;
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%*c",&n,&m);
memset(a,0,sizeof(a));
ans=0;
while(!p.empty())
p.pop();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%c",&a[i][j]);
}
getchar();
}
memset(v,0,sizeof(v));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j]=='.')
v[i][j]=inf;
else if(a[i][j]>='a'&&a[i][j]<='z')
{
v[i][j]=1;
p.push(make_pair(i,j));
}
}
}
bfs();
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
printf("%c",a[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}
/**
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
**/
相关文章推荐
- 陈老师的多校联合20140816||SPOJ AMR11B 三角形的有向面积
- 陈老师的多校联合20140816A题||spoj 10228 动态规划
- 陈老师的多校联合2010811c题||09年武汉站||hdu3232
- 陈老师的多校联合 20140809
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- 陈老师的多校联合20180808 map映射字符串
- 陈老师的多校联合 20140809 C题
- 陈老师的多校联合20140818||Uvalive 5683 模拟
- 陈老师的多校联合 I题 二分
- 陈老师的多校联合 20140808 E题
- 陈老师的多校联合20140815 ||uvalive 5881 区间处理
- UVALive 5691 二分图匹配||陈老师的多校联合20140818 J题
- 陈老师的多校联合 D题 字符串处理起来挺麻烦
- 陈老师的多校联合20140811||HDU 3236 ||2009年武汉站G题 01背包问题
- 多校联合集训(汇总)
- 多校联合(4) ZZ’s Fibonacci Problem
- hdu 4865 Peter's Hobby(2014 多校联合第一场 E)
- 多校联合练习赛1 problem1009 I-number 信心题啊。。。
- 2013 多校联合 2 A Balls Rearrangement (hdu 4611)
- 2013多校联合2 I Warm up 2(hdu 4619)