CCF CSP 棋局評估 java python 201803_4 100分
問題描述
Alice和Bob正在玩井字棋遊戲。
井字棋遊戲的規則很簡單:兩人輪流往3*3的棋盤中放棋子,Alice放的是“X”,Bob放的是“O”,Alice執先。當同一種棋子佔據一行、一列或一條對角線的三個格子時,遊戲結束,該種棋子的持有者獲勝。當棋盤被填滿的時候,遊戲結束,雙方平手。
Alice設計了一種對棋局評分的方法:
- 對於Alice已經獲勝的局面,評估得分爲(棋盤上的空格子數+1);
- 對於Bob已經獲勝的局面,評估得分爲 -(棋盤上的空格子數+1);
- 對於平局的局面,評估得分爲0;
例如上圖中的局面,Alice已經獲勝,同時棋盤上有2個空格,所以局面得分爲2+1=3。
由於Alice並不喜歡計算,所以他請教擅長編程的你,如果兩人都以最優策略行棋,那麼當前局面的最終得分會是多少?
輸入格式
輸入的第一行包含一個正整數T,表示數據的組數。
每組數據輸入有3行,每行有3個整數,用空格分隔,分別表示棋盤每個格子的狀態。0表示格子爲空,1表示格子中爲“X”,2表示格子中爲“O”。保證不會出現其他狀態。
保證輸入的局面合法。(即保證輸入的局面可以通過行棋到達,且保證沒有雙方同時獲勝的情況)
保證輸入的局面輪到Alice行棋。
輸出格式
對於每組數據,輸出一行一個整數,表示當前局面的得分。
樣例輸入
3
1 2 1
2 1 2
0 0 0
2 1 1
0 2 1
0 0 2
0 0 0
0 0 0
0 0 0
樣例輸出
3
-4
0
樣例說明
第一組數據:
Alice將棋子放在左下角(或右下角)後,可以到達問題描述中的局面,得分爲3。
3爲Alice行棋後能到達的局面中得分的最大值。
第二組數據:
Bob已經獲勝(如圖),此局面得分爲-(3+1)=-4。
第三組數據:
井字棋中若雙方都採用最優策略,遊戲平局,最終得分爲0.
數據規模和約定
對於所有評測用例,1 ≤ T ≤ 5。
問題分析:
這道題在考場上做的時候滿腦子想的都是有關博弈的東西,想着如何去構建博弈模型,下來仔細思考後發現這道題的規模是很小的,可以用搜索遍歷出每一種情況,然後得到滿足題意的情況就可以了。說到底還是深搜。
Java代碼如下:
import java.util.Scanner;
public class csp201803_4 {
public static int array[][] = new int[3][3];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
for(int i = 0;i < number;i++) {
for(int j = 0;j < 3;j++) {
for(int k = 0;k < 3;k++) {
array[j][k] = input.nextInt();
}
}
int score = dfs(1);
System.out.println(score);
}
}
public static int dfs(int index) {
int space = 0;
for(int i = 0;i < array.length;i++) {
for(int j = 0;j < array[i].length;j++) {
if(array[i][j] == 0) {
space++;
}
}
}
if(index==1&&judge(2)) {
return -1*(space + 1);
}
if(index==2&&judge(1)) {
return (space + 1);
}
if(space == 0) {
return 0;
}
int max = -10000;
int min = 10000;
for(int i = 0;i < array.length;i++) {
for(int j = 0;j < array[i].length;j++) {
if(array[i][j] == 0) {
array[i][j] = index;
if(index == 1) {
max = Math.max(max, dfs(2));
}
if(index == 2) {
min = Math.min(min, dfs(1));
}
array[i][j] = 0;
}
}
}
if(index == 1) {
return max;
}
else {
return min;
}
}
public static boolean judge(int index) {
for(int i = 0;i < 3;i++) {
if(array[i][0]==array[i][1]&&array[i][1]==array[i][2]&&array[i][2]==index) {
return true;
}
if(array[0][i]==array[1][i]&&array[1][i]==array[2][i]&&array[2][i]==index) {
return true;
}
}
if(array[0][0]==array[1][1]&&array[1][1]==array[2][2]&&array[2][2]==index) {
return true;
}
if(array[2][0]==array[1][1]&&array[1][1]==array[0][2]&&array[0][2]==index) {
return true;
}
return false;
}
}
python3代碼如下:
def judge(index):
for i in range(3):
if list1[i][0]==list1[i][1] and list1[i][1]==list1[i][2] and list1[i][2]==index:
return True
if list1[0][i]==list1[1][i] and list1[1][i]==list1[2][i] and list1[2][i]==index:
return True
if list1[0][0]==list1[1][1] and list1[1][1]==list1[2][2] and list1[2][2]==index:
return True
if list1[2][0]==list1[1][1] and list1[1][1]==list1[0][2] and list1[0][2]==index:
return True
return False
def dfs(index):
space = 0
for i in range(3):
for j in range(3):
if list1[i][j] == 0:
space += 1
if index == 1 and judge(2):
return -1 * (space + 1)
if index == 2 and judge(1):
return (space + 1)
if space == 0:
return 0
max1 = -10000
min1 = 10000
for i in range(3):
for j in range(3):
if list1[i][j] == 0:
list1[i][j] = index
if index == 1:
max1 = max(max1, dfs(2))
if index == 2:
min1 = min(min1, dfs(1))
list1[i][j] = 0
if index == 1:
return max1
else :
return min1
number = (int)(input())
for i in range(number):
list1 = []
for j in range(3):
temp = input().split(" ")
list2 = []
for k in range(3):
list2.append((int)(temp[k]))
list1.append(list2)
score = dfs(1)
print(score)
ok!這就大功告成了,如果你有更好的方法或者問題,歡迎在評論區交流!