import java.util.ArrayList; import java.util.Scanner; public class 红与黑 { static int co=0; static ArrayListlist=new ArrayList<>(); public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x=0,y=0;//记录初始位置 //初始化 while (true) { int l=sc.nextInt(); int h=sc.nextInt(); if(l==0&&h==0) break; for (int i = 0; i < h; i++) { String s=sc.next(); char c[]=s.toCharArray(); list.add(c); } //记录初始位置 for (int i = 0; i < h; i++) { for (int i1 = 0; i1 < l; i1++) { if(list.get(i)[i1]=='@') { x=i;y=i1; } } } bfs(x, y); System.out.println(co); } } private static void bfs(int x,int y){ if(x<0||x==9||y<0||y==6||list.get(x)[y]=='#') return ; ++co; list.get(x)[y]='#'; bfs(x+1, y); bfs(x-1, y); bfs(x, y+1); bfs(x, y-1); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)