题目
问题描述
小明有一块空地,他将这块空地划分为 n 行 m 列的小块,每行和每列的长度都为 1 。
小明选了其中的一些小块空地,种上了草,其他小块仍然保持是空地。
这些草长得很快,每个月,草都会向外长出一些,如果一个小块种了草,则它将向自己的上、下、左、右四小块空地扩展,这四小块空地都将变为有草的小块。
请告诉小明,k 个月后空地上哪些地方有草。
输入格式
输入的第一行包含两个整数 n, m。
接下来 n 行,每行包含 m 个字母,表示初始的空地状态,字母之间没有空格。如果为小数点,表示为空地,如果字母为 g,表示种了草。
接下来包含一个整数 k。
输出格式
输出 n 行,每行包含 m 个字母,表示 k 个月后空地的状态。如果为小数点,表示为空地,如果字母为 g,表示长了草。
样例输入
4 5
. g…
…. .
. . g. .
…. .
2
样例输出
gggg.
gggg.
ggggg
. ggg.
评测用例规模与约定
对于 30 % 的评测用例,2 <= n, m <= 20 。
对于 70 % 的评测用例,2 <= n, m <= 100 。
对于所有评测用例,2 <= n, m <= 1000 ,1 <= k <= 1000
有问题,这个要看最终官方给的数据
import java. util. Scanner;
public class 种草 {
public static int [ ] x= { 0 , 1 , 0 , - 1 } ;
public static int [ ] y= { 1 , 0 , - 1 , 0 } ;
public static int n= 0 , m= 0 , k= 0 ;
public static int [ ] [ ] bool;
public static int [ ] start;
public static int [ ] end;
public static char [ ] [ ] a;
public static void dfs ( int xx, int yy, int kk) {
bool[ xx] [ yy] = kk;
a[ xx] [ yy] = 'g' ;
for ( int i= 0 ; i< 4 ; i++ ) {
int newx= x[ i] + xx;
int newy= y[ i] + xx;
if ( newx>= 0 && newy>= 0 && newx< n&& newy< m&& kk- 1 > bool[ newx] [ newy] ) {
dfs ( newx, newy, kk- 1 ) ;
}
}
}
public static void main ( String[ ] args) {
Scanner sc= new Scanner ( System. in) ;
n= sc. nextInt ( ) ;
m= sc. nextInt ( ) ;
a= new char [ n] [ m] ;
for ( int i= 0 ; i< n; i++ ) {
String s= sc. next ( ) ;
a[ i] = s. toCharArray ( ) ;
}
k= sc. nextInt ( ) ;
sc. close ( ) ;
start= new int [ n* m] ;
end= new int [ n* m] ;
int temp= 0 ;
bool = new int [ n] [ m] ;
for ( int i= 0 ; i< n; i++ ) {
for ( int j= 0 ; j< m; j++ ) {
if ( a[ i] [ j] == 'g' ) {
start[ temp] = i;
end[ temp++ ] = j;
} else {
bool[ i] [ j] = - 1 ;
}
}
}
for ( int i= 0 ; i< temp; i++ ) {
dfs ( start[ i] , end[ i] , k) ;
}
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < m; j++ ) {
System. out. print ( a[ i] [ j] ) ;
}
System. out. println ( ) ;
}
}
}
import java. util. ArrayList;
import java. util. LinkedList;
import java. util. Queue;
import java. util. Scanner;
public class 种树1 {
public static void main ( String[ ] args) {
int n, m, month;
ArrayList< Node> node= new ArrayList < Node> ( ) ;
Scanner sc= new Scanner ( System. in) ;
n= sc. nextInt ( ) ;
m= sc. nextInt ( ) ;
sc. nextLine ( ) ;
int num[ ] [ ] = new int [ n] [ m] ;
for ( int i= 0 ; i< n; i++ ) {
String s= sc. next ( ) ;
for ( int j= 0 ; j< m; j++ ) {
if ( s. charAt ( j) == '.' )
num[ i] [ j] = 0 ;
else {
num[ i] [ j] = 1 ;
node. add ( new Node ( i, j) ) ;
}
}
}
month= sc. nextInt ( ) ;
bfs ( num, node, month, m, n) ;
}
private static void bfs ( int [ ] [ ] num, ArrayList< Node> node, int month, int m, int n) {
int [ ] [ ] direct = { { - 1 , 0 } , { 1 , 0 } , { 0 , - 1 } , { 0 , 1 } } ;
Queue< Node> queue= new LinkedList < Node> ( ) ;
while ( month>= 0 ) {
for ( int i= 0 ; i< node. size ( ) ; i++ ) {
queue. offer ( node. get ( i) ) ;
}
while ( queue. size ( ) > 0 ) {
Node temp= queue. poll ( ) ;
for ( int i= 0 ; i< 4 ; i++ ) {
int tx= temp. x+ direct[ i] [ 0 ] ;
int ty= temp. y+ direct[ i] [ 1 ] ;
if ( tx< 0 || ty< 0 || tx> m- 1 || ty> n- 1 )
continue ;
if ( num[ ty] [ tx] == 1 )
continue ;
Node next= new Node ( tx, ty) ;
num[ ty] [ tx] = 1 ;
node. add ( new Node ( ty, tx) ) ;
}
month-- ;
}
}
int i, k;
for ( i= 0 ; i< num. length; i++ ) {
for ( k= 0 ; k< num[ i] . length; k++ ) {
if ( num[ i] [ k] == 1 )
System. out. print ( 'g' ) ;
else
System. out. println ( '.' ) ;
}
System. out. println ( ) ;
}
}
}
class Node {
int x;
int y;
Node ( int x, int y) {
this . x= x;
this . y= y;
}
}
import java. io. BufferedWriter;
import java. io. IOException;
import java. io. OutputStreamWriter;
import java. util. LinkedList;
import java. util. Scanner;
public class 种草1 {
static final int [ ] dx = { 1 , 0 , - 1 , 0 } ;
static final int [ ] dy = { 0 , 1 , 0 , - 1 } ;
static int [ ] [ ] vis = new int [ 1000 ] [ 1000 ] ;
static int N, M, K;
static Scanner sc ;
public static void main ( String[ ] args) throws IOException {
sc= new Scanner ( System. in) ;
N = sc. nextInt ( ) ;
M = sc. nextInt ( ) ;
sc. nextLine ( ) ;
LinkedList< Block> q = new LinkedList < Block> ( ) ;
for ( int i = 0 ; i < N; i++ ) {
String line = sc. nextLine ( ) ;
for ( int j = 0 ; j < M; j++ ) {
if ( line. charAt ( j) == 'g' ) {
q. addLast ( new Block ( i, j, 0 ) ) ;
vis[ i] [ j] = 1 ;
}
}
}
K = sc. nextInt ( ) ;
while ( ! q. isEmpty ( ) ) {
Block b = q. removeFirst ( ) ;
int month = b. month;
if ( month < K) {
for ( int i = 0 ; i <= 3 ; i++ ) {
int nx = b. i + dx[ i] ;
int ny = b. j + dy[ i] ;
if ( 0 <= nx && nx < N && 0 <= ny && ny < M && vis[ nx] [ ny] == 0 ) {
vis[ nx] [ ny] = 1 ;
q. addLast ( new Block ( nx, ny, month + 1 ) ) ;
}
}
}
}
BufferedWriter writer = new BufferedWriter ( new OutputStreamWriter ( System. out) ) ;
for ( int i = 0 ; i < N; i++ ) {
for ( int j = 0 ; j < M; j++ ) {
if ( vis[ i] [ j] == 1 ) writer. write ( 'g' ) ;
else writer. write ( '.' ) ;
}
writer. write ( '\n' ) ;
}
writer. flush ( ) ;
}
private static class Block {
int i;
int j;
int month;
public Block ( int i, int j, int month) {
this . i = i;
this . j = j;
this . month = month;
}
}
}
这个是我修改了一下我发的前几个的数据,把arralylist改为了linkedlist就可以用了
import java. util. LinkedList;
import java. util. Queue;
import java. util. Scanner;
public class 种树1 {
public static void main ( String[ ] args) {
int n, m, month;
LinkedList< Node> node= new LinkedList < Node> ( ) ;
Scanner sc= new Scanner ( System. in) ;
n= sc. nextInt ( ) ;
m= sc. nextInt ( ) ;
sc. nextLine ( ) ;
int num[ ] [ ] = new int [ n] [ m] ;
for ( int i= 0 ; i< n; i++ ) {
String s= sc. next ( ) ;
for ( int j= 0 ; j< m; j++ ) {
if ( s. charAt ( j) == '.' )
num[ i] [ j] = 0 ;
else {
num[ i] [ j] = 1 ;
node. add ( new Node ( i, j) ) ;
}
}
}
month= sc. nextInt ( ) ;
bfs ( num, node, month, m, n) ;
}
private static void bfs ( int [ ] [ ] num, LinkedList< Node> node, int month, int m, int n) {
int [ ] [ ] direct = { { - 1 , 0 } , { 1 , 0 } , { 0 , - 1 } , { 0 , 1 } } ;
Queue< Node> queue= new LinkedList < Node> ( ) ;
while ( month>= 0 ) {
for ( int i= 0 ; i< node. size ( ) ; i++ ) {
queue. offer ( node. get ( i) ) ;
}
while ( queue. size ( ) > 0 ) {
Node temp= queue. poll ( ) ;
for ( int i= 0 ; i< 4 ; i++ ) {
int tx= temp. x+ direct[ i] [ 0 ] ;
int ty= temp. y+ direct[ i] [ 1 ] ;
if ( tx< 0 || ty< 0 || tx> m- 1 || ty> n- 1 )
continue ;
if ( num[ ty] [ tx] == 1 )
continue ;
Node next= new Node ( tx, ty) ;
num[ ty] [ tx] = 1 ;
node. add ( new Node ( ty, tx) ) ;
}
month-- ;
}
}
int i, k;
for ( i= 0 ; i< num. length; i++ ) {
for ( k= 0 ; k< num[ i] . length; k++ ) {
if ( num[ i] [ k] == 1 )
System. out. print ( 'g' ) ;
else
System. out. print ( '.' ) ;
}
System. out. println ( ) ;
}
}
}
class Node {
int x;
int y;
Node ( int x, int y) {
this . x= x;
this . y= y;
}
}