UVa220 Othello 解题报告

5 篇文章 0 订阅
2 篇文章 0 订阅

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35583
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=156
这是一道模拟的题,题目的意思是在8x8的棋盘上进行有规则的游戏。
而核心内容是判断这个棋盘上是否八个方面都符合要求。只需要实现这八个方向的判断就基本上结束了。

下面是代码(无注释):​

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n;
char mat[10][10], cmd[5];
char oppostatus(char status) {
    return status == 'W' ? 'B' : 'W';
}
struct rcheck {
    bool result;
    int cnt;
};
rcheck check(int x, int y, char status, bool ischange) {
    rcheck ret;
    if(mat[x][y] != '-') {
        ret.result = false;
        return ret;
    }
    ret.result = false;
    char ostatus = oppostatus(status);
    bool f = false;
    for(int tx = x - 1, ty = y - 1; tx >= 0 && ty >= 0; tx--, ty--) {
        if(mat[tx][ty] == ostatus)
            f = true;
        else if(mat[tx][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx + 1, ty2 = ty + 1; tx2 <= x - 1 && ty2 <= y - 1; tx2++, ty2++)
                    mat[tx2][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int tx = x - 1; tx >= 0; tx--) {
        if(mat[tx][y] == ostatus)
            f = true;
        else if(mat[tx][y] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx + 1; tx2 <= x - 1; tx2++)
                    mat[tx2][y] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int tx = x - 1, ty = y + 1; tx >= 0 && ty <= 8; tx--, ty++) {
        if(mat[tx][ty] == ostatus)
            f = true;
        else if(mat[tx][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx + 1, ty2 = ty - 1; tx2 <= x - 1 && ty2 >= y + 1; tx2++, ty2--)
                    mat[tx2][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int ty = y + 1; ty <= 8; ty++) {
        if(mat[x][ty] == ostatus)
            f = true;
        else if(mat[x][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int ty2 = ty - 1; ty2 >= y + 1; ty2--)
                    mat[x][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int tx = x + 1, ty = y + 1; tx <= 8 && ty <= 8; tx++, ty++) {
        if(mat[tx][ty] == ostatus)
            f = true;
        else if(mat[tx][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx - 1, ty2 = ty - 1; tx2 >= x + 1 && ty2 >= y + 1; tx2--, ty2--)
                    mat[tx2][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int tx = x + 1; tx <= 8; tx++) {
        if(mat[tx][y] == ostatus)
            f = true;
        else if(mat[tx][y] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx - 1; tx2 >= x + 1; tx2--)
                    mat[tx2][y] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int tx = x + 1, ty = y - 1; tx <= 8 && ty >= 0; tx++, ty--) {
        if(mat[tx][ty] == ostatus)
            f = true;
        else if(mat[tx][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int tx2 = tx - 1, ty2 = ty + 1; tx2 >= x + 1 && ty2 <= y - 1; tx2--, ty2++)
                    mat[tx2][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    f = false;
    for(int ty = y - 1; ty >= 0; ty--) {
        if(mat[x][ty] == ostatus)
            f = true;
        else if(mat[x][ty] == status && f) {
            ret.result = true;
            if(ischange == true)
                for(int ty2 = ty + 1; ty2 <= y - 1; ty2++)
                    mat[x][ty2] = status;
            else return ret;
            break;
        }
        else break;
    }
    return ret;
}
int main() {
    cin>>n;
    for(int i = 1; i <= n; i++) {
        if(i != 1) printf("\n");
        memset(mat, 0, sizeof mat);
        for(int x = 1; x <= 8; x++)
            cin>>(mat[x] + 1);
        char status;
        cin>>status;
        while(true) {
            cin>>cmd;
            if(cmd[0] == 'Q') {
                for(int x = 1; x <= 8; x++) {
                    for(int y = 1; y <= 8; y++)
                        printf("%c", mat[x][y]);
                    printf("\n");
                }
                break;
            }
            else if(cmd[0] == 'L') {
                bool hasans = false;
                for(int x = 1; x <= 8; x++)
                    for(int y = 1; y <= 8; y++)
                        if(check(x, y, status, false).result) {
                            if(hasans) printf(" ");
                            printf("(%d,%d)", x, y);
                            hasans = true;
                        }
                if(!hasans) printf("No legal move.");
                printf("\n");
            }
            else if(cmd[0] == 'M') {
                int r = cmd[1] - '0';
                int c = cmd[2] - '0';
                rcheck rc = check(r, c, status, true);
                if(rc.result == false) {
                    status = oppostatus(status);
                    rc = check(r, c, status, true);
                }
                mat[r][c] = status;
                int cntw = 0, cntb = 0;
                for(int x = 1; x <= 8; x++)
                    for(int y = 1; y <= 8; y++) {
                        if(mat[x][y] == 'W') cntw++;
                        else if(mat[x][y] == 'B') cntb++;
                    }
                printf("Black - %2d White - %2d\n", cntb, cntw);
                status = oppostatus(status);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值