IEEEXtreme 10.0 - Checkers Challenge

时间:2022-11-05 13:12:03

这是 meelo 原创的 IEEEXtreme极限编程大赛题解

Xtreme 10.0 - Checkers Challenge

题目来源 第10届IEEE极限编程大赛

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/draughts-1

Watch the following YouTube video clip. Your task is to compute the number of possible ways the white player can win from an opening state of a single white piece in a game of Turkish Draughts. For more information on the game, you can view the Wikipedia page.

For this challenge, we will use the following variation on the official rules:

  1. The black pieces can be arbitrary placed, and will not necessarily be located at places reachable in a legal game

  2. A single white piece is a king if, and only if, it is placed in or reaches the top most line. Once a piece is a king it remains a king throughout.

  3. A white piece can capture by jumping over a single black piece to the left, right or upwards, landing in the adjacent square

  4. A white king can capture by jumping left, right, upwards or backwards and can skip arbitrary number of blank squares before and after the black piece

  5. After capturing a black piece, the white piece (or king) must turn 90 degrees or keep moving in the same direction (no 180 degree turns are allowed).

  6. We ask for the number of different ways the white player can win a single move. White wins by capturing all black pieces.

Input Format

Each input begins with an integer t, on a line by itself, indicating how many testcases are present.

Each testcase will contain 8 lines with the state of the board. The board will have a single white piece o, some black pieces x, and empty places .. White's side of the board is at the bottom of the board. So if the white piece were to reach to top row of the board, it would become a king.

In between each testcase is a blank line.

Constraints

1 ≤ t ≤ 5

There will always be at least 1, and no more than 16, black pieces in each game.

The game board will always be 8x8 squares in size.

Output Format

For each testcase, output, on a line by itself, the number of possible ways the white can win, or 0 if he cannot.

Sample Input

3
.......o
.x.x.x..
xxxx.xx.
........
........
.x.xx..x
x.......
..x...x. ........
........
....o...
........
....x...
........
........
........ ...o....
........
...x....
........
........
........
........
........

Sample Output

12
0
5

Explanation

The first testcase is the state of the board in the 56th second of the YouTube video. There are 12 ways in which this game can be won. These ways are represented below:

  1. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 2

  2. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 3

  3. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 4

  4. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 5

  5. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 6

  6. down 7, left 3, up 6, left 2, down 4, right 4, up 4, left 3, down 4, left 3, up 4, right 5, down 6, left 5, up 5, right 7

  7. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 2

  8. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 3

  9. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 4

  10. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 5

  11. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 6

  12. down 7, left 3, up 6, right 2, down 4, left 4, up 4, right 3, down 4, left 5, up 4, right 3, down 6, left 3, up 5, right 7

There is no way for white to win the second testcase.

For the final testcase, white has a king, and white can capture the single black piece, and land on any of the five spaces below the piece.

题目解析

这题是一个搜索题,用深度优先搜索可以解决。

题目中的游戏规则比较复杂,一定要仔细阅读。最初没有注意到,普通白子不能向下走,浪费了很多时间。

使用回溯法可以避免保存状态。

程序

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

// check whether (x,y) is a legal location
bool legal(int x, int y) {
return (x>=) && (x<) && (y>=) && (y<);
}

/**
board: 8x8 array representing the game board
isKing: whether white piece is king
wx: white piece's location on x axis
wy: white piece's location on y axis
lastDir: direction of last move, valid value are -1, 0, 1, 2, 3, -1 represents initial move
numBlack: number of black pieces on board
*/
int countWin(char board[][], bool isKing, int wx, int wy, int lastDir, int numBlack) {
int count = ; // game over, white piece win
if(numBlack == ) return ; int dir[][] = { {,}, {,-}, {-,}, {,} };
int bx, by; // black piece to the left, right or upwards
int sx, sy; // landing square if(!isKing) {
// cannot go downwards, possible directions: 0, 1, 2
for(int d=; d<; d++) { bx = wx + dir[d][];
by = wy + dir[d][];
sx = wx + dir[d][] * ;
sy = wy + dir[d][] * ; if(board[bx][by]=='x' && legal(sx, sy) && board[sx][sy]=='.') {
if(sx == ) isKing = true;
board[bx][by] = '.';
numBlack--;
count += countWin(board, isKing, sx, sy, d, numBlack);
// backtrack
board[bx][by] = 'x';
numBlack++;
}
}
}
else {
for(int d=; d<; d++) {
if((d== && lastDir==) || (d== && lastDir==) ||
(d== && lastDir==) || (d== && lastDir==)) {
continue;
}
bx = by = -;
// white king can go at least 1 step, at most 6 steps
for(int skipBefore=; skipBefore<=; skipBefore++) {
int tx = wx + dir[d][] * skipBefore;
int ty = wy + dir[d][] * skipBefore;
if(legal(tx, ty) && board[tx][ty]=='x') {
bx = tx;
by = ty;
break;
}
}
//cout << bx << ' ' << by << endl;
if(!legal(bx, by)) continue;
for(int skipAfter=; skipAfter<=; skipAfter++) {
int tx = bx + dir[d][] * skipAfter;
int ty = by + dir[d][] * skipAfter;
if(legal(tx, ty) && board[tx][ty]=='.') {
board[bx][by] = '.';
numBlack--;
int C = countWin(board, isKing, tx, ty, d, numBlack);
count += C;
// backtrack
board[bx][by] = 'x';
numBlack++;
}
else {
break;
} }
}
} return count;
} int main() {
int T;
cin >> T;
for(int t=; t<T; t++) {
char board[][];
for(int l=; l<; l++) {
cin >> board[l];
} // check whether white piece is king or not
bool isKing = false;
for(int c=; c<; c++) {
if(board[][c] == 'o') isKing = true;
} // locate white piece
int wx, wy, numBlack = ;
for(int l=; l<; l++) {
for(int c=; c<; c++) {
if(board[l][c] == 'o') {
wx = l;
wy = c;
board[l][c] = '.';
}
else if(board[l][c] == 'x') {
numBlack++;
}
}
}
cout << countWin(board, isKing, wx, wy, -, numBlack) << endl;
getchar();
}
return ;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Checkers Challenge的更多相关文章

  1. IEEEXtreme 10&period;0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10&period;0 - Painter&&num;39&semi;s Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10&period;0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

  4. IEEEXtreme 10&period;0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  5. IEEEXtreme 10&period;0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10&period;0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  7. IEEEXtreme 10&period;0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  8. IEEEXtreme 10&period;0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  9. IEEEXtreme 10&period;0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

随机推荐

  1. &lbrack;转载&rsqb;ODAC &lpar;odp&period;net&rpar; 开发到部署

    1. 确定你开发机和服务器的操作系统是32位还是64位, 而且要确定要部署的服务器是什么操作系统; 2. 下载开发机和服务器所需的dll, 地址:http://download.csdn.net/de ...

  2. 在竞赛ACM Java处理输入输出

    一.Java之ACM注意点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非 ...

  3. category应用(计算nssting的数量)

    // //  main.m //  03-分类应用 // //  Created by apple on 14-3-18. //  Copyright (c) 2014年 apple. All rig ...

  4. hadoop hdfs 命令行 设置文件夹大小的上限 quota:配额

    >bin/hdfs dfs -put readme.txt /finance >bin/hdfs dfs -du -s /finance > /finance >bin/hdf ...

  5. mysql 时间

    显示当前时间: mysql> select now(); +---------------------+ | now() | +---------------------+ | -- :: | ...

  6. Android安卓身份证识别SDK

    一.Android安卓身份证识别SDK应用背景 这些年,随着互联网金融的极速发展,第三方支付.理财.P2P网贷.征信等APP应用成爆发式的增长,在众多APP中都涉及到对身份证信息的录入,如第三方支付. ...

  7. python 爬虫与数据可视化--数据提取与存储

    一.爬虫的定义.爬虫的分类(通用爬虫.聚焦爬虫).爬虫应用场景.爬虫工作原理(最后会发一个完整爬虫代码) 二.http.https的介绍.url的形式.请求方法.响应状态码 url的形式: 请求头: ...

  8. redis缓存设计

    1:缓存技术和框架的重要性 互联网的一些高并发,高性能的项目和系统中,缓存技术是起着功不可没的作用.缓存不仅仅是key-value的简单存取,它在具体的业务场景中,还是很复杂的,需要很强的架构设计能力 ...

  9. 荣耀实锤Magic2或将助力AI,再次带动成长?

    临近年底,热闹了一年的手机圈纷纷偃旗息鼓,准备为明年3月的新品发力.然而今天(12月7日),恰逢节气大雪,@荣耀手机 在微博发布了一张预热海报,随后荣耀总裁赵明转发这条微博表示「关于技术,真的有很多话 ...

  10. &period;Net Core 本地化&amp&semi;全球化 实践

    介绍: 所有有关本地化的数据获取,都是从统一的一个资源文件中获取 1.创建虚拟类.资源文件,用于作为本地化数据的获取源 2.Configure localization:支持view.data ann ...