栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

红与黑棋盘

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

红与黑棋盘

import java.util.ArrayList;
import java.util.Scanner;

public class 红与黑 {
    static int co=0;
    static ArrayList list=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);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863098.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号