首页 > 试题广场 >

下象棋

[编程题]下象棋
  • 热度指数:2699 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

牛妹在和牛牛下牛客象棋。现在轮到牛妹了,牛妹想知道她在这一回合能否战胜牛牛。

棋盘chessboard上只可能包含:炮,将,车,兵

牛客象棋的规则解释:
炮:炮在不吃子的时候,走动与车完全相同,但炮在吃棋子时,必须跳过一个棋子,我方的和敌方的都可以
兵:可以上下左右移动,每次只能移动一格
车:上下左右均可走,只要无棋子阻拦,步数不受限制。
将:可以上下左右移动,每次只能移动一格
接下来给出一个棋盘,牛妹的棋子用大写字母表示,牛牛的棋子用小写字母表示。
将用表示,炮用表示,车用表示,兵用表示,没有棋子的格子用表示

保证棋盘上一定同时包含各一个。

牛妹能胜利则返回"Happy",否则返回"Sad"

示例1

输入

["......", "..B...", "P.C.j.", "......", "..b..."," ...J.." ]

输出

"Happy"

说明

牛妹的炮可以攻击到牛牛的将,所以获胜

备注:


class Solution {
public:
    /**
     * 
     * @param chessboard string字符串vector 
     * @return string字符串
     */
    string playchess(vector<string>& chessboard) {
        int n=chessboard.size(), m=chessboard[0].size();
        vector<pair<int,int>> J, P, C, B;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                if(chessboard[i][j]=='j')
                    J.push_back({i,j});
                if(chessboard[i][j]=='P')
                    P.push_back({i,j});
                if(chessboard[i][j]=='C')
                    C.push_back({i,j});
                if(chessboard[i][j]=='B' || chessboard[i][j]=='J')
                    B.push_back({i,j});
            }
        for(int i=0;i<J.size();i++){
            int jx=J[i].first, jy=J[i].second;
            for(int j=0;j<B.size();j++){
                int bx=B[j].first, by=B[j].second;
                if((bx==jx && abs(by-jy)==1) || (by==jy && abs(bx-jx)==1))
                    return "Happy";
            }
            for(int j=0;j<C.size();j++){
                int cx=C[j].first, cy=C[j].second, t=-1;
                if(cx == jx){
                    t = 0;
                    for(int k=min(cy, jy)+1;k<max(cy, jy) && t==0;k++)
                        if(chessboard[jx][k]!='.')
                            t++;
                }
                if(t==0)
                    return "Happy";
                if(cy == jy){
                    t = 0;
                    for(int k=min(cx, jx)+1;k<max(cx, jx) && t==0;k++)
                        if(chessboard[k][jy]!='.')
                            t++;
                }
                if(t==0)
                    return "Happy";
            }
            for(int j=0;j<P.size();j++){
                int px=P[j].first, py=P[j].second, t=0;
                if(px == jx)
                    for(int k=min(py, jy)+1;k<max(py, jy) && t<=1;k++)
                        if(chessboard[jx][k]!='.')
                            t++;
                if(py == jy)
                    for(int k=min(px, jx)+1;k<max(px, jx) && t<=1;k++)
                        if(chessboard[k][jy]!='.')
                            t++;
                if(t==1)
                    return "Happy";
            }
        }
        return "Sad";
    }
};

发表于 2020-07-06 01:38:22 回复(0)
先找到对方的将,然后上,下,左,右四个方向判断能否一步吃掉
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param chessboard string字符串一维数组 
     * @return string字符串
     */
    public String playchess (String[] chessboard) {
        // write code here
        char arr[][]=new char[chessboard.length][chessboard[0].length()];
        for(int i=0;i<arr.length;i++){
            arr[i]=chessboard[i].toCharArray();
        }
        int[][]dir={{-1,0},{0,-1},{1,0},{0,1}};
        int n=arr.length;
        int m=arr[0].length;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(arr[i][j]=='j'){
                    for(int k=0;k<4;k++){
                        for(int cnt=0,nx=i+dir[k][0],ny=j+dir[k][1],l=2;
                           nx>=0&&nx<n&&ny>=0&&ny<m;
                            nx=i+dir[k][0]*l,ny=j+dir[k][1]*l,l++)
                        {
                            if(((arr[nx][ny]=='B'||arr[nx][ny]=='J')&&l==2)||
                              (arr[nx][ny]=='P'&&cnt==1)||
                               (arr[nx][ny]=='C'&&cnt==0))    
                            {
                                return "Happy";
                            }
                            if(arr[nx][ny]!='.')cnt++;
                            if(cnt>1) break;
                        }
                    }
                }
            }
        }
        return "Sad";
    }
}

发表于 2022-06-26 18:56:59 回复(0)
思考
发表于 2022-04-10 10:50:29 回复(0)
class Solution:
    def playchess(self , chessboard ):
        # write code here
        x,y = -1,-1
        m,n = len(chessboard),len(chessboard[0])
        for i in range(m):
            for j in range(n):
                if chessboard[i][j] == 'j':
                    x = i
                    y = j
        for dx,dy in {(1,0),(-1,0),(0,1),(0,-1)}:
            cur_x,cur_y = x,y
            cnt = 0
            step = 0
            while 0 <= cur_x + dx < m and 0 <= cur_y + dy < n:
                if step == 0:
                    if chessboard[cur_x + dx][cur_y + dy] == 'B'&nbs***bsp;chessboard[cur_x + dx][cur_y + dy] == 'J':
                        return 'Happy'
                if chessboard[cur_x + dx][cur_y + dy] == 'C':
                    if cnt == 0:
                        return 'Happy'
                if chessboard[cur_x + dx][cur_y + dy] == 'P':
                    if cnt == 1:
                        return 'Happy'
                if chessboard[cur_x + dx][cur_y + dy] != '.':
                    cnt += 1
                
                step += 1
                cur_x += dx
                cur_y += dy 
        return 'Sad'

发表于 2021-08-28 17:21:21 回复(0)
            int boardcol = chessboard.size();
            if (boardcol == 0) return "Sad";
            int boardrow = chessboard[0].length();
            if (boardrow == 0) return "Sad";

            bool found = false;
            int rowPos = 0;
            int colPos = 0;


            for (vector<string>::iterator it = chessboard.begin(); it != chessboard.end(); ++it, ++rowPos)
            {
                string str = (*it);

                //先找到黑将
                for (colPos = 0; colPos < str.length(); colPos++)
                {
                    std::cout << str[colPos];
                    switch (str[colPos])
                    {
                    case 'j':
                        found = true;
                        break;
                    }
                    if (found) goto DoCheck;
                }
            }
        DoCheck:
            if (!found) return "Sad";
            //而后按4个方向寻找对方棋子
            if (checkMate(chessboard, rowPos, -1, colPos, 0)) return "Happy";
            if (checkMate(chessboard, rowPos, 1, colPos, 0)) return "Happy";
            if (checkMate(chessboard, rowPos, 0, colPos, 1)) return "Happy";
            if (checkMate(chessboard, rowPos, 0, colPos, -1)) return "Happy";

            // write code here
            return "Sad";
        }

        bool checkMate(vector<string>& chessboard, int rowPos, int rowMove, int colPos, int colMove)
        {
            int boardrow = chessboard.size();
            int boardcol = chessboard[0].length();

            bool check = false;
            
            int iCount = 0; //炮中间棋子统计
            rowPos += rowMove;
            colPos += colMove;
            while (rowPos >= 0 && rowPos < boardrow && colPos >= 0 && colPos < boardcol)
            {
                char pieces = chessboard[rowPos][colPos];
                if (pieces == 'J')
                {
                    if (iCount == 0)
                        return true;
                }
                switch (pieces) {
                case 'B':
                    return "true";
                case '.':
                    break;
                case 'j':
                case 'p':
                case 'c':
                case 'b':
                case 'J':
                    iCount++;
                    break;
                case 'P':
                    if (iCount == 1) return true;
                    else
                    {
                        iCount++;
                        break;
                    }
                case 'C':
                    if (iCount == 0)return true;
                }
                rowPos += rowMove;
                colPos += colMove;
            }
            return false;

        }
发表于 2021-06-15 19:28:00 回复(0)
import java.util.*;
  
  
public class Solution {
    /**
     *
     * @param chessboard string字符串一维数组
     * @return string字符串
     */
     public String playchess(String[] chessboard) {
        // write code here
        if (chessboard == null || chessboard.length == 0) {
            return "Sad";
        }
        String Happy = "Happy";
        int rows = chessboard.length;
        int cols = chessboard[0].length();

        int[][] directions = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

        //
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                // 找到j
                if (chessboard[i].charAt(j) == 'j') {
                    //兵/将吃将
                    if (((j - 1) >= 0 && ('B' == chessboard[i].charAt(j - 1) || 'J' == chessboard[i].charAt(j - 1)))
                            || ((j + 1) < cols && ('B' == chessboard[i].charAt(j + 1) || 'J' == chessboard[i].charAt(j + 1)))
                            || ((i - 1) >= 0 && ('B' == chessboard[i - 1].charAt(j) || 'J' == chessboard[i - 1].charAt(j)))
                            || ((i + 1) < rows && ('B' == chessboard[i + 1].charAt(j) || 'J' == chessboard[i + 1].charAt(j)))) {
                        return Happy;
                    }

                    int len = Math.max(i, rows - i);
                    len = Math.max(len, j);
                    len = Math.max(len, cols - j);
                    for (int k = 0; k <= len; k++) {
                        //车吃将
                        if (((j - k) >= 0 && 'C' == chessboard[i].charAt(j - k))
                                || ((j + k) < cols && 'C' == chessboard[i].charAt(j + k))
                                || ((i - k) >= 0 && 'C' == chessboard[i - k].charAt(j))
                                || ((i + k) < rows && 'C' == chessboard[i + k].charAt(j))) {
                            return Happy;
                        }
                        //炮吃将
                        if (((j - k) >= 0 && 'P' == chessboard[i].charAt(j - k))) {
                            for (int kk = 1; kk < k - 1; kk++) {
                                if ('.' != chessboard[i].charAt(j - kk)) {
                                    return Happy;
                                }
                            }
                        }
                        if (((j + k) < cols && 'P' == chessboard[i].charAt(j + k))) {
                            for (int kk = 1; kk < k - 1; kk++) {
                                if ('.' != chessboard[i].charAt(j + kk)) {
                                    return Happy;
                                }
                            }
                        }
                        if (((i - k) >= 0 && 'P' == chessboard[i - k].charAt(j))) {
                            for (int kk = 1; kk < k - 1; kk++) {
                                if ('.' != chessboard[i - kk].charAt(j)) {
                                    return Happy;
                                }
                            }
                        }
                        if (((i + k) < rows && 'P' == chessboard[i + k].charAt(j))) {
                            for (int kk = 1; kk < k - 1; kk++) {
                                if ('.' != chessboard[i + kk].charAt(j)) {
                                    return Happy;
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
        return "Sad";
    }

}

编辑于 2021-03-05 09:24:41 回复(0)
测试用例还能有将帅相遇的?我下的是假象棋2333
发表于 2020-08-20 21:27:50 回复(0)
class Solution {
private:
    int m, n;   //row, col
    int j_x, j_y;  //the position of 'j'
public:
    /**
     * 
     * @param chessboard string字符串vector 
     * @return string字符串
     */
    bool paoWin(vector<string>& chessboard, int i, int j) {
        if (i != j_x && j != j_y)
            return false;
        else if (i == j_x) {
            int count = 0; 
            int beg = min(j, j_y), end = max(j, j_y);
            for (int t = beg + 1; t < end; ++t) {
                if (chessboard[i][t] != '.')
                    ++count;
            }
            if (count == 1)
                return true;
        }
        else if (j == j_y) {
            int count = 0; 
            int beg = min(i, j_x), end = max(i, j_x);
            for (int t = beg + 1; t < end; ++t) {
                if (chessboard[t][j] != '.')
                    ++count;
            }
            if (count == 1)
                return true;
        }
        return false;
    }
    bool binWin(vector<string>& chessboard, int i, int j) {
        if (i != j_x && j != j_y)
            return false;
        else if (i == j_x) {
            if (abs(j - j_y) == 1)
                return true;
        }
        else if (j == j_y) {
            if (abs(i - j_x) == 1)
                return true;
        }
        return false;
    }
    bool cheWin(vector<string>& chessboard, int i, int j) {
        if (i != j_x && j != j_y)
            return false;
        
        else if (i == j_x) {
            int beg = min(j, j_y), end = max(j, j_y);
            for (int t = beg + 1; t < end; ++t) {
                if (chessboard[i][t] != '.')
                    return false;
            }
        }
        else if (j == j_y) {
            int beg = min(i, j_x), end = max(i, j_x);
            for (int t = beg + 1; t < end; ++t) {
                if (chessboard[t][j] != '.')
                    return false;
            }
        }
        return true;
    }
    
    string playchess(vector<string>& chessboard) {
        // write code here
        m = chessboard.size();
        if (m == 0)    return "Sad";
        n = chessboard[0].size();
        bool found = false;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (chessboard[i][j] == 'j') {
                    j_x = i;
                    j_y = j;
                    found = true;
                    break;
                }
            }
            if (found)
                break;
        }
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (chessboard[i][j] == 'P') {
                    if (paoWin(chessboard, i, j)) {
                        return "Happy";
                    }
                }
                else if (chessboard[i][j] == 'B' || chessboard[i][j] == 'J') {
                    if (binWin(chessboard, i, j)) {
                        return "Happy";
                    }
                }
                else if (chessboard[i][j] == 'C') {
                    if (cheWin(chessboard, i, j)) {
                        return "Happy";
                    }
                }
            }
        }
        return "Sad";
    }
};

发表于 2020-08-08 14:20:34 回复(0)