zoukankan      html  css  js  c++  java
  • 跳蚤移动

    package unit3;
    
    import com.sun.org.apache.xpath.internal.axes.WalkerFactory;
    
    public class House {
        private int m;
        private int n;
        private int[][]a;
        public  House() {
            m=10;
            n=10;
            a=new int[m][n];
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    a[i][j]=0;
                }
            }
        }
        public House(int m,int n) {
            this.m=m;
            this.n=n;
            a=new int[m][n];
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    a[i][j]=0;
                }
            }
        }
        public int getM() {
            return m;
        }
        public int getN() {
            return n;
        }
        public int[][] getA() {
            return a;
        }
        public int getElement(int i,int j) {
            return a[i][j];
        }
        public void setElement(int i,int j,int value) {
            a[i][j]=value;
        }
        public boolean checkZero() {
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    if(a[i][j]==0) return true;
                }
            }
            return false;
        }
        public void  display() {
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    System.out.print(" "+a[i][j]+" ");
                }
                System.out.println();
            }
        }
        public class Tiaozao{
            private static final int UP=0;
            private static final int DOWN=1;
            private static final int RIGHT=2;
            private static final int LEFT=3;
            private int x,y;
            private int totals=0;
            private House ahouse;
            public Tiaozao(House h){
                ahouse=h;
                totals=0;
                x=(int)(Math.random()*ahouse.getM());
                y=(int)(Math.random()*ahouse.getN());
            }
            public int getTotals() {
                return totals;
            }
            public boolean walk(int direction) {
                System.out.println("x="+x+", y="+y+", direction="+direction);
                switch (direction) {
                case UP: 
                    if(y==0) {
                        return false;
                    }else{
                        ahouse.setElement(x,y,ahouse.getElement(x,y)+1);
                        y=y-1;
                    }
                    return true;    
                case DOWN:
                    if(y==ahouse.getN()-1){
                        return false;
                    }else{
                        ahouse.setElement(x,y,ahouse.getElement(x,y)+1);
                        y=y+1;
                    }
                    return true;
                case LEFT:
                    if(x==0){
                        return false;
                    }else{
                        ahouse.setElement(x,y,ahouse.getElement(x,y)+1);
                        x=x-1;
                    }
                    return true;
                case RIGHT:
                    if(x==ahouse.getM()-1){
                        return false;
                    }else{
                        ahouse.setElement(x,y,ahouse.getElement(x,y)+1);
                        x=x+1;
                    }
                    return true;
                default:
                    System.out.println("分发移动");return false;
                }
            }
            public void move() {
                int nextdirection;
                boolean success;
                do{
                    nextdirection=(int)(Math.random()*4);
                    success=walk(nextdirection);
                    if(success) totals++;
                }while(ahouse.checkZero());
            }
        
        }
        public static void main(String[] args){
            House ahouse= new House(6,6);
            Tiaozao atiaozao =ahouse.new Tiaozao(ahouse);
            atiaozao.move();
            ahouse.display();
            System.out.println("Totals="+atiaozao.getTotals());
        }
        
    }
  • 相关阅读:
    ubuntu14.04 Cannot find OpenSSL's <evp.h>
    git 常用命令
    Python3常用模块的安装
    Centos7 安装配置优化mysql(mariadb分支)
    Centos7 编译安装python3
    Centos6.5搭建git远程仓库
    年轻
    springboot 报错Field XXX required a bean of type XXX that could not be found.
    springboot 启动报错[classpath:/application.yml] but snakeyaml was not found on the classpath
    idea 使用点击maven clean/install或maven其他命令失败,显示:乱码+archetypeCatalog=internal
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5686767.html
Copyright © 2011-2022 走看看