生活随笔
收集整理的這篇文章主要介紹了
                                
【华为笔试】【模拟】【Java】竖直四子棋
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            ■ 題目描述
 豎直四子棋的棋盤是豎立起來的,雙方輪流選擇棋盤的一列下子,棋子因重力落到棋盤底部或者其他棋子之上,當一列的棋子放滿時,無法再在這列上下子。
 一方的4個棋子橫、豎或者斜方向連成一線時獲勝。
 現給定一個棋盤和紅藍對弈雙方的下子步驟,判斷紅方或藍方是否在某一步獲勝。
 下面以一個6×5的棋盤圖示說明落子過程:
 
下面給出橫、豎和斜方向四子連線的圖示:
 
輸入描述
 輸入為2行,第一行指定棋盤的寬和高,為空格分隔的兩個數字;
 第二行依次間隔指定紅藍雙方的落子步驟,第1步為紅方的落子,第2步為藍方的落子,第3步為紅方的落子,以此類推。
 步驟由空格分隔的一組數字表示,每個數字為落子的列的編號(最左邊的列編號為1,往右遞增)。用例保證數字均為32位有符號數。
 輸出描述
 如果落子過程中紅方獲勝,輸出 N,red ;
 如果落子過程中藍方獲勝,輸出 N,blue ;
 如果出現非法的落子步驟,輸出 N,error。
 N為落子步驟的序號,從1開始。如果雙方都沒有獲勝,輸出 0,draw 。
 非法落子步驟有兩種,一是列的編號超過棋盤范圍,二是在一個已經落滿子的列上落子。
 N和單詞red、blue、draw、error之間是英文逗號連接。
 示例1 輸入輸出示例僅供調試,后臺判題數據一般不包含示例
 輸入
 5 5
 1 1 2 2 3 3 4 4
 輸出
 7,red
 說明
 在第7步,紅方在第4列落下一子后,紅方的四個子在第一行連成一線,故紅方獲勝,輸出 7,red。
 示例2 輸入輸出示例僅供調試,后臺判題數據一般不包含示例
 輸入
 5 5
 0 1 2 2 3 3 4 4
 輸出
 1,error
 說明
 第1步的列序號為0,超出有效列編號的范圍,故輸出 1,error。
 
public class VerticalChess {public static void main(String[] args
) {Scanner sc 
= new Scanner(System.in
);String[] nums 
= sc
.nextLine().split(" ");int rowLen 
= Integer.parseInt(nums
[0]);int colLen 
= Integer.parseInt(nums
[1]);String[] str 
= sc
.nextLine().split(" ");int[][] grids 
= new int[rowLen
][colLen
];boolean isOver 
= false;for (int i 
= 0; i 
< str
.length
; i
++) {int index 
= -1;int color 
= 1;int num 
= Integer.parseInt(str
[i
]);if (num 
<= 0 || num 
> colLen 
|| grids
[0][num 
- 1] != 0) {isOver 
= true;System.out
.println(i 
+ 1 + ",error");break;}if (i 
% 2 != 0) {color 
= 2;}for (int j 
= rowLen 
- 1; j 
>= 0; j
--) {if (grids
[j
][num 
- 1] == 0) {index 
= j
; grids
[j
][num 
- 1] = color
;break;}}if (index 
== -1) {System.out
.println(i 
+ 1 + ",error");isOver 
= true;break;}if (i 
>= 6 && isSuccess(grids
, index
, num 
- 1)) {if (color 
== 1) {System.out
.println(i 
+ 1 + ",red");isOver 
= true;break;} else {System.out
.println(i 
+ 1 + ",blue");isOver 
= true;break;}}}if (!isOver
) {System.out
.println("0,draw");}}private static boolean isSuccess(int[][] grids
, int row
, int col
) {int m 
= grids
.length
;int n 
= grids
[0].length
;int count 
= 0;int statist 
= 3; if (col 
< n 
- 3) {int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[r
][++c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}if (col
>=3){int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[r
][--c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}if (row
<m
-3){int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[++r
][c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}if (row
>=3){int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[--r
][c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}if (row
<m
-3&&col
>=3){int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[++r
][--c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}if (row
<m
-3&&col
<n
-3){int r 
= row
;int c 
= col
;while (statist 
!= 0 && grids
[++r
][++c
] == grids
[r
][c
]) {count
++;statist
--;}if (count 
== 3) {return true;}count 
= 0;statist 
= 3;}return false;}
}
                            總結
                            
                                以上是生活随笔為你收集整理的【华为笔试】【模拟】【Java】竖直四子棋的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。