AcWing 1113. 红与黑
原题链接
简单
作者:
不知名路人
,
2021-04-01 16:33:00
,
所有人可见
,
阅读 306
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class My {
int x;
int y;
My(int x,int y){
this.x=x;
this.y=y;
}
}
public class Main {
static int w,z;
static int h,l;
static char[][] g;
static boolean[][] st;
static My s;
static int[] dx = new int[] {-1,0,1,0};
static int[] dy = new int[] {0,1,0,-1};
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
while (in.hasNext()){
w=in.nextInt();
h=in.nextInt();
if(w==0&&h==0)
break;
else {
z=w;
l=h;
g=new char[h][w];
st=new boolean[h][w];
}
for(int i=0;i<h;i++){
g[i]=in.next().toCharArray();
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(g[i][j]=='@'){
s=new My(i,j);
}
}
}
System.out.println(bfs(s));
}
}
static int bfs(My s){
int cnt=1;
Queue<My> q=new LinkedList<>();
q.offer(s);
while (!q.isEmpty()){
My t=q.poll();
for(int i=0;i<4;i++){
int x=t.x+dx[i];
int y=t.y+dy[i];
if(x<0||x>=l||y<0||y>=z||g[x][y]=='#'||g[x][y]=='@') {
continue;
}
if(st[x][y]){
continue;
}
st[x][y]=true;
cnt++;
q.offer(new My(x,y));
}
}
return cnt;
}
}