zoukankan      html  css  js  c++  java
  • 华为上机:五子棋

    (粤港澳补充题)五子棋
    描述:

    此题目限定的五子棋规则:

    1、棋盘大小:15*15

    2、分为黑棋和白棋,一种颜色棋子不论横、竖、斜连成5个或大于5个算赢

    3、黑白交替下,也就是说一方不能连续2次成功落子。

    要求:根据双方落子,进行有效性判断和胜负判断。

    运行时间限制: 无限制
    内存限制: 无限制
    输入:

    输入一行或多行,每行包括三个整数,分别是棋子类型,棋子的横坐标,棋子的纵坐标,以空格隔开

    棋子类型:0  黑棋,1 白棋

    棋子坐标从0开始,小于15。

    输出:

    针对每行输入,如果:

    黑方胜,输出1;

    白方胜,输出2;

    落子失败,比如坐标错误,或顺序错,输出-1;

    其他情况:落字成功,不输出;

    胜负一旦确定,测试用例就结束。

    样例输入:
    0 7 7
    1 7 8
    0 7 7
    1 7 5
    0 8 8
    1 9 9
    0 6 6
    1 8 9
    0 5 5
    1 7 9
    0 4 4
    样例输出:
    -1
    -1
    1

    解题

    判断是否连成五指棋比较简单,判断合法的下棋顺序话费了很长时间

    import java.util.Arrays;
    import java.util.LinkedList;
    import java.util.Scanner;
    public class Main3{
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int[][] a = new int[15][15];
            for(int i=0;i<15;i++){
                for(int j=0;j<15;j++){
                    a[i][j] = -1;
                }
            }
            int i = 0;
            int first = 1;
            int next = 2;
            int target = -1;
            while(in.hasNext()){
                String[] point = in.nextLine().split(" ");
                if(!isLeague(point)){
                    System.out.println("-1");
                    continue;
                }
                    
                int opt = Integer.parseInt(point[0]);
                int x = Integer.parseInt(point[1]);
                int y = Integer.parseInt(point[2]);
                if(a[x][y]!=-1){
                    System.out.println("-1");
                    continue;
                }
                if(i==0){ // 第0次下棋:first 1 next 2
                    first = 2 ;// 下一次棋子
                    next = 1;
                    i++;
                    target = opt;// 第0次下的棋子
                }else{
                    if(i%2==0){ 
                        if(target != opt){// 偶数位和第0次下的棋子应该一样  first 1 next 2
                            System.out.println("-1");
                            continue;
                        }
                        if(first==1&&next==2){ // 判断是否是该子下棋,是的更新到下一个下棋棋子
                           
                           first = 2;
                           next = 1;
                           i++;
                        }else{
                            System.out.println("-1");
                            continue;
                        }
                    }else{
                        if(target == opt){ // 偶数位和第0次下的棋子不应该一样 
                            System.out.println("-1");
                            continue;
                        }
                        if(first==2&&next==1){ // 判断是否是该子下棋,是的更新到下一个下棋棋子
                            
                           first = 1;
                           next = 2;
                           i++;
                        }else{
                            System.out.println("-1");
                            continue;
                        }
                    }
                }
                
                a[x][y]=opt;
                if(isWin(a,x,y)){
                    if(opt==0){
                        System.out.println("1");
                    }else{
                        System.out.println("2");
                    }
                    break;
                }
            }
            
            in.close();
        }
        /**
         * 四个方向判断是否赢
         * @param a
         * @param x
         * @param y
         * @return
         */
        public static boolean isWin(int[][] a,int x,int y){
            int  target = a[x][y];
            int count = 1;
            int i = x;
            int j = y;
            // 上下
            while(i<14){
                i++;
                if(a[i][y]==target){
                    count++;
                }
            }
            i = x;
            while(i>=1){
                i--;
                if(a[i][y]==target){
                    count++;
                }
            }
            if(count>=5){
                return true;
            }
            // 左右
            count = 1;
            j = y;
            while(j<14){
                j++;
                if(a[x][j]==target){
                    count++;
                }
            }
            j = y;
            while(j>=1){
                j--;
                if(a[x][j]==target){
                    count++;
                }
            }
            if(count>=5){
                return true;
            }
            // 左上右下
            i = x;
            j = y;
            count = 1;
    
            while(i<14&&j<14){
                j++;
                i++;
                if(a[i][j]==target){
                    count++;
                }
            }
            i = x;
            j = y;
            while(j>=1&&i>=1){
                j--;
                i--;
                if(a[i][j]==target){
                    count++;
                }
            }
            if(count>=5){
                return true;
            }
            
            // 左下右上
            i = x;
            j = y;
            count = 1;
    
            while(i>=1&&j<14){
                j++;
                i--;
                if(a[i][j]==target){
                    count++;
                }
            }
            i = x;
            j = y;
            while(j>=1&&i>14){
                j--;
                i++;
                if(a[i][j]==target){
                    count++;
                }
            }
            if(count>=5){
                return true;
            }
            return false;
        }
        /**
         * 输入是否合法
         * @param point
         * @return
         */
        public static boolean isLeague(String[] point){
            if(point==null ||point.length!=3){
                return false;
            }
                
            if(!(point[0].equals("0")||point[0].equals("1"))){
    //            System.out.println(0);
                return false;
            }
                
            int x = Integer.parseInt(point[1]);
            if(x<0 ||x>=15){
    //            System.out.println("X");
                return false;
            }
                
            int y = Integer.parseInt(point[2]);
            if(y<0 ||y>=15){
    //            System.out.println("Y");
                return false;
            }
                
            return true;
        }
    }
  • 相关阅读:
    [转]asp.net页面缓存技术
    UL和LI在div中的高度的IE6下兼容性
    jquery制作的横向图片滚动带横向滚动条TackerScroll
    电脑可以上网,但是qq登陆不上去?
    Introduction to discrete event system学习笔记4.6
    Introduction to Discrete event system学习笔记4.9
    Introduction to discrete event systemsstudy 4.5
    Symbolic synthesis of obserability requirements for diagnosability B.Bittner,M.Bozzano,A.Cimatti,and X.Olive笔记4.16
    Introduction to discrete event system学习笔记4.8pm
    Introduction to discrete event system学习笔记 4.8
  • 原文地址:https://www.cnblogs.com/bbbblog/p/5707134.html
Copyright © 2011-2022 走看看