问题 F: Hexagon Game

时间限制: 1 Sec  内存限制: 128 MB
提交: 5  解决: 2
[提交][状态][讨论版]

题目描述

This problem was inspired by a board game called Hex, designed independently by Piet Hein and John Nash. It has a similar idea, but does not assume you have played Hex. This game is played on an NxN board, where each cell is a hexagon. There are two players: Red side (using red stones) and Blue side (using blue stones). The board starts empty, and the two players take turns placing a stone of their color on a single cell within the overall playing board. Each player can place their stone on any cell not occupied by another stone of any color. There is no requirement that a stone must be placed beside another stone of the same color. The player to start first is determined randomly (with equal probability among the two players). The upper side and lower sides of the board are marked as red, and the other two sides are marked as blue. The goal of the game is to form a connected path of one player's stones connecting the two sides of the board that have that player's color. The first player to achieve this wins. Note that the four corners are considered connected to both colors. The game ends immediately when one player wins. Given a game state, help someone new to the game determine the status of a game board. Say one of the following: "Impossible": If it was impossible for two players to follow the rules and to have arrived at that game state. "Red wins": If the player playing the red stones has won. "Blue wins": If the player playing the blue stones has won. "Nobody wins": If nobody has yet won the game. Note that a game of Hex can't end without a winner! Note that in any impossible state, the only correct answer is "Impossible", even if red or blue has formed a connected path of stones linking the opposing sides of the board marked by his or her colors. Here's a an example game on a 6x6 gameboard where blue won. Blue was the first player to move, and placed a blue stone at cell marked as 1. Then Red placed at cell 2, then blue at cell 3, etc. After the 11th stone is placed, blue wins.

输入要求

The first line of input gives the number of test cases, T(1 ≤ T ≤ 100). T test cases follow.

Each test case start with the size of the side of the board, N(1 ≤ N ≤ 100).

This is followed by a board of N rows and N columns consisting of only 'B', 'R' and '.' characters.

'B' indicates a cell occupied by blue stone, 'R' indicates a cell occupied by red stone, and '.' indicates an empty cell.

输出要求

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the status of the game board.

It can be "Impossible", "Blue wins", "Red wins" or "Nobody wins" (excluding the quotes).

假如输入

7
1
.
1
B
1
R
2
BR
BB
4
BBBB
BBB.
RRR.
RRRR
4
BBBB
BBBB
RRR.
RRRR
6
......
..R...
BBBBBB
..R.R.
..RR..
......

应当输出

Case #1: Nobody wins
Case #2: Blue wins
Case #3: Red wins
Case #4: Impossible
Case #5: Blue wins
Case #6: Impossible
Case #7: Blue wins

提示

1、首先进行六边形棋盘与矩形的转换:
关于方向上的问题解决方案如下:
如上图所示,以6为中心,在矩形内本该有上、下、左、右、左上、左下、右上、右下8个遍历方向,由于六边形六条边面的特性(只与周围其他六个六边形相连),即没有左上和右下的方向。
2、获胜的条件:
(1)所给的棋盘状态下的棋子摆放合理:
(i)按照每人每次轮流下一步的规则,两种颜色的棋子总数最多相差一个,或者相等。
(ii)按照规则,一旦有一方获胜游戏即刻停止。即不可能出现某一方获胜两次及以上,或者,两方都获胜的情况。
(2)对于红方:红色棋子将棋盘的上下边相连。
 对于蓝方:蓝色棋子将棋盘的左右边相连。
注:本来应该先判断棋盘上的棋子布局是否合理,但是检查合理性会很费时,应当先检查输赢或者与检查输赢一起进行。
源码:
import java.util.Scanner;public class Main {//定义四种游戏最终状态,并与RESULT里的字符串一一对应public static final int BLUE_WINS = 0;public static final int IMPOSSIBLE = 1;public static final int RED_WINS = 2;public static final int NOBODY_WINS = 3;public static final String[] RESULT = { "Blue wins", "Impossible","Red wins", "Nobody wins" };//蓝方和红方赢得比赛的次数public static int blueWinCount = 0;public static int redWinCount = 0;/*** 判断双方的棋子数量是否合理(双方棋子数量的差的绝对值不超过1),因为是轮流每人下一颗棋* @param map* @return*/private static boolean isCellNumberReasonable(char[][] map) {int bCount = 0, rCount = 0;for (int i = 0; i < map.length; i++) {for (int j = 0; j < map.length; j++) {if (map[i][j] == 'B') {bCount++;} else if (map[i][j] == 'R') {rCount++;}}}// System.out.println("CountCha=" + Math.abs(bCount - rCount));return Math.abs(bCount - rCount) <= 1;}/*** 判断蓝方是否赢得比赛* @param map* @param x* @param y* @return*/public static boolean isBlueWin(char[][] map, int x, int y) {map[x][y] = '.';//将遍历过的位置改为“.”,防止出现循环遍历的情况if (y == map.length - 1) {blueWinCount++;}// UPif (y - 1 >= 0 && map[x][y - 1] == 'B') {isBlueWin(map, x, y - 1);}// RIGHT_UPif (x + 1 < map.length && y - 1 >= 0 && map[x + 1][y - 1] == 'B') {isBlueWin(map, x + 1, y - 1);}// RIGHTif (x + 1 < map.length && map[x + 1][y] == 'B') {isBlueWin(map, x + 1, y);}// DOWNif (y + 1 < map.length && map[x][y + 1] == 'B') {isBlueWin(map, x, y + 1);}// LEFT_DOWNif (x - 1 >= 0 && y + 1 < map.length && map[x - 1][y + 1] == 'B') {isBlueWin(map, x - 1, y + 1);}// LEFTif (x - 1 >= 0 && map[x - 1][y] == 'B') {isBlueWin(map, x - 1, y);}return blueWinCount >= 1;}/*** 判断红方是否赢得比赛* @param map* @param x* @param y* @return*/private static boolean isRedWin(char[][] map, int x, int y) {map[x][y] = '.';//将遍历过的位置改为“.”,防止出现循环遍历的情况if (x == map.length - 1) {redWinCount++;}// UPif (y - 1 >= 0 && map[x][y - 1] == 'R') {isRedWin(map, x, y - 1);}// RIGHT_UPif (x + 1 < map.length && y - 1 >= 0 && map[x + 1][y - 1] == 'R') {isRedWin(map, x + 1, y - 1);}// RIGHTif (x + 1 < map.length && map[x + 1][y] == 'R') {isRedWin(map, x + 1, y);}// DOWNif (y + 1 < map.length && map[x][y + 1] == 'R') {isRedWin(map, x, y + 1);}// LEFT_DOWNif (x - 1 >= 0 && y + 1 < map.length && map[x - 1][y + 1] == 'R') {isRedWin(map, x - 1, y + 1);}// LEFTif (x - 1 >= 0 && map[x - 1][y] == 'R') {isRedWin(map, x - 1, y);}return redWinCount >= 1;}/*** 得到最终结果* * @param map* @return*/public static int judge(char[][] map) {int n = map.length;int state = NOBODY_WINS;// 由于isBlueWin和isRedWin两个方法会修改map里的数据,所以要提前判断棋子数量书否合理boolean isCellNumberReasonable = isCellNumberReasonable(map);for (int i = 0; i < n; i++) {if (map[i][0] == 'B' && isBlueWin(map, i, 0)) {state = BLUE_WINS;}if (map[0][i] == 'R' && isRedWin(map, 0, i)) {state = RED_WINS;}}// 如果蓝方和红方赢得比赛的次数超过1,或者,棋子数量不合理,则判定游戏当前状态为impossibleif (blueWinCount + redWinCount > 1 || !isCellNumberReasonable) {state = IMPOSSIBLE;}// System.out.println("WinCount=" + (blueWinCount + redWinCount));return state;}public static void main(String[] args) {Scanner cin = new Scanner(System.in);int z = cin.nextInt();for (int j = 1; j <= z; j++) {int n = cin.nextInt();char[][] map = new char[n][n];for (int i = 0; i < map.length; i++) {map[i] = cin.next().toCharArray();}// 每次初始化蓝方和红方赢得比赛的次数blueWinCount = 0;redWinCount = 0;System.out.println("Case #" + j + ": " + RESULT[judge(map)]);}}
}

#算法有Bug,日后再改。

【数据结构+算法】浙传OJ Contest 2290:13信息1 Java 6 问题 F: Hexagon Game相关推荐

  1. 微软等数据结构+算法面试100题全部答案集锦

    微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时间:二零一一年十月十三日. 引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一周年 ...

  2. 微软等数据结构+算法面试100题全部答案完整亮相

    重磅分享:微软等数据结构+算法面试100题全部答案完整亮相 来源: 王永刚的日志 本文转载自CSDN大牛的一篇博客:http://blog.csdn.net/v_july_v/article/deta ...

  3. 微软等数据结构+算法面试100题

    转载于:http://blog.csdn.net/garfielder007/article/details/48931183 微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时 ...

  4. (转)微软等数据结构+算法面试100题全部答案集锦

    微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时间:二零一一年十月十三日. 引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一周年 ...

  5. 微软等数据结构+算法面试100题全部答案集锦 复制过来比较乱

    亲,"社区之星"已经一周岁了!        WebApp实时开源框架Clouda---认识心得      Tag功能介绍-我们为什么打Tag      订阅CSDN社区周刊,及时 ...

  6. 微软公司等数据结构+算法面试100题2010版全部出炉

    微软等公司数据结构+算法面试100题2010版首次完整亮相                         作者:July.2010年12月6日. 更新:现今,这100题的答案已经全部整理出来了,微软 ...

  7. Comet OJ - Contest #11 题解赛后总结

    Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...

  8. ACMer,OIer:Comet OJ Contest #0原创题程序设计大赛邀请!

    比赛邀请 2019年3月31日,Comet OJ主办 Comet OJ Contest #0 ,欢迎广大算法爱好者参加~ 第一名奖品:樱桃G80-3000机械键盘,第二名之后有一定比例的T恤和日系短裙 ...

  9. 数据结构+算法面试100题

    1.把二元查找树转变成排序的双向链表(树)  题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表. 要求不能创建任何新的结点,只调整指针的指向.    10   / /   6  14 ...

最新文章

  1. python postgresql跨数据库查询_Postgresql跨数据库查询
  2. python xpath语法-python中使用XPath笔记
  3. 微信小程序之bindtap事件绑定传参
  4. 图解Android - 如何看Android的UML 图?
  5. LoadRunner常见问题整理
  6. WPF基础入门 - 1
  7. java获取用户地理位置_java web 通过ip获取当前地理位置
  8. docker容器的常用命令以及访问tomcat报404的解决方法(二)
  9. (转载)排序六 堆排序
  10. 动力学是如何做预测的
  11. Vmware+Virtualbox+Ubuntu+debian+USB转串口+kermit
  12. Async注解使用及源码分析
  13. 小学生计算机按键分布图,小学生计算器上各种按键的作用
  14. python设置win10壁纸
  15. 无基础的人如何唱好歌、三分钟让你快速学会唱歌技巧
  16. 【微信小程序】微信小程序读取本地文件--学习微信小程序之路02
  17. layui的treetable
  18. 复旦FM1208 CPU卡调试
  19. ae等高线_AE插件-地形海拔轮廓等高线动画 Topograph v1.0 Win/Mac + 视频教程
  20. 结构力学回顾与展望:结构工程是人类文明的脊梁

热门文章

  1. 微软云Centos byobu安装
  2. 支持向量机 SVM 算法推导优缺点 代码实现 in Python
  3. linux怎么读(中文读音发音)
  4. 【C语言】让你不再害怕指针——C指针详解(经典,非常详细)
  5. 3GPP TS 23501-h20 中英文对照 | 5.29.2 5G VN group management
  6. 1100 校庆(JAVA)
  7. 100天精通Andriod逆向——第3天:真机环境配置
  8. wpsmac和pc版的区别_WPS Office for Mac VS Microsoft Office 365,同为办公软件,差别竟然这么大!...
  9. 音视频开发系列(10)ffmpeg基础使用
  10. java并发学习28:有序性