当前位置:编程学习 > C/C++ >>

UVA 10196 - Check The Check (将军)

[cpp 
/*  
[cpp]  
* 题目的链接地址  <a target="_blank" href="http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=1137">http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=1137</a>   
[cpp]  
*  
* 作者 仪冰  
* QQ   974817955  
*  
* 题目描述  
* 模拟棋盘。黑棋在棋盘的上半部分,用小写字母表示;白棋在下半部分,用大写字母表示。  
* 一共有六种棋子。  
* p ——> 只能走斜前方。即白棋只能走↖(左上),↗(右上);黑棋只能走↙(左下),↘(右下)  
* r ——> 可以走垂直方向和水平方向上走,可以走任意格。  
* b ——> 可以走对角线方向上走,可以走任意格。  
* q ——> 可以走垂直方向、水平方向、对角线方向,可以走任意格。  
* k ——> 可以走垂直方向、水平方向、对角线方向,只能走一格。  
* n ——> 和中国象棋马的走法一样,走日字。(没有蹩马腿这个规则)  
*  
* 需要注意的是:  
* 对于r、b、q这三种棋子,如果在他们的行走的方向上有其他的棋子,则不能继续走了。  
*  
* 输入样例  
..k.....  
ppp.pppp  
........  
.R...B..  
........  
........  
PPPPPPPP  
K.......  
  
rnbqkbnr  
pppppppp  
........  
........  
........  
........  
PPPPPPPP  
RNBQKBNR  
  
rnbqk.nr  
ppp..ppp  
....p...  
...p....  
.bPP....  
.....N..  
PP..PPPP  
RNBQKB.R  
  
........  
........  
........  
........  
........  
........  
........  
........  
* 输出样例  
Game #1: black king is in check.  
Game #2: no king is in check.  
Game #3: white king is in check.  
  
* 自己的输入样例  
R.pkp..R  
..ppp...  
.....B..  
B..Q..Q.  
........  
...R....  
........  
........  
  
R.pkp..R  
.NpppN..  
..N..B..  
B..Q..Q.  
........  
...R....  
.....P..  
....K...  
* 自己的输出样例  
Game #1: no king is in check.  
Game #2: black king is in check.  
*/  
  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
  
using namespace std;  
  
const int SIZEX = 9;   //棋盘行数  
const int SIZEY = 9;   //棋盘列数  
  
//这是Knight的边界数组, 在很多题目中都有用到边界数组  
int coordinate[8][2] = {-1,-2, -2,-1, -2,1, -1,2, 1,2, 2,1, 2,-1, 1,-2};  
  
/* 函数声明 */  
bool blackChessPawn(char chess[SIZEX][SIZEY], int x, int y, char ch); //黑棋的pawn  
bool whiteChessPawn(char chess[SIZEX][SIZEY], int x, int y, char ch); //白棋的pawn  
bool Rook(char chess[SIZEX][SIZEY], int x, int y, char ch);           //黑白棋的Rook  
bool Bishop(char chess[SIZEX][SIZEY], int x, int y, char ch);         //黑白棋的Bishop  
bool Queen(char chess[SIZEX][SIZEY], int x, int y, char ch);          //黑白棋的Queen  
bool Knight(char chess[SIZEX][SIZEY], int x, int y, char ch);         //黑白棋的Knight  
  
int main()  
{  
    char chessBoard[SIZEX][SIZEY]; //棋盘  
    int number = 0;                //计数器,判断是否结束程序  
    bool boolean = false;          //判断是否将军  
    int gameCount = 1;             //数据的组数  
  
    while (true)  
    {  
        for (int i=1; i<9; i++)  
        {  
            for (int j=1; j<9; j++)  
            {  
                scanf("%c", &chessBoard[i][j]);  
                if (chessBoard[i][j] == '.')  
                {  
                    number++;  
                }  
            }  
            scanf("%*c");  
        }  
  
        //判断程序是否结束  
        if (number == 64)  
        {  
            break;  
        }  
        number = 0;  
  
        scanf("%*c");  //吸收一个换行  
  
        for (int i=1; i<9; i++)  
        {  
            for (int j=1; j<9; j++)  
            {  
                switch (chessBoard[i][j])  
                {  
                    case 'p':  
                         boolean = blackChessPawn(chessBoard, i, j, 'K');  
                         if (boolean)  
                         {  
                             cout << "Game #" << gameCount  
                                  << ": white king is in check." << endl;  
           
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,