UVA - 220 Othello(模拟)

35 篇文章 0 订阅
11 篇文章 0 订阅

点击打开题目链接
一秒钟想思路,一下午调BUG
注意的地方:
1.输出格式最后不要多加空行。
2.坐标之间一个空格间隔,行尾没空格
3.棋子数的输出%2d
4.No legal move.(我忘了点,udebug样例全过了,找了半天才找出来TAT)
AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int mp[10][10];
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int N;
int player, x, y, cnt[3];
int nx, ny;
char op, c;

void printMp() {
    for(int i = 1; i <= 8; i++) {
        for(int j = 1; j <= 8; j++) {
            if(mp[i][j] == 0) cout << '-';
            else if(mp[i][j] == 1) cout << 'W';
            else if(mp[i][j] == 2)cout << 'B';
        }
        cout << endl ;
    }
    return;
}

int isLegal(int x, int y, int i) {
    x += dx[i];
    y += dy[i];
    if(mp[x][y] == 0 || mp[x][y] == player) return 0;
    if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 0;
    while(mp[x][y] == 3-player) {
        x += dx[i];
        y += dy[i];
        if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 0;
        while(mp[x][y] == player) {
            x += dx[i];
            y += dy[i];
            if(x <= 0 || x >= 9 || y <= 0 || y >= 9) return 1;
            if(mp[x][y] == player) continue;
            if(mp[x][y] == 0) return 1;
            return 1;
        }
    }
    return 0;
}

void L() {
    int flag = 0;
    for(int i = 1; i<= 8; i++) {
        for(int j = 1; j <= 8; j++) {
            if(!mp[i][j]) {
                for(int k = 0; k < 8; k++) {
                    if(isLegal(i, j, k)) {
                        if(!flag) printf("(%d,%d)", i, j);
                        else printf(" (%d,%d)", i, j);
                        flag = 1;
                        break;
                    }
                }
            }
        }
    }
    if(flag) cout << endl;
    if(!flag) cout << "No legal move." << endl;
    return;
}

void M(int x, int y) {
    int flag = 0;
    for(int i = 0; i < 8; i++) {
        if(isLegal(x, y, i)) {
            mp[x][y] = player;
            nx = x + dx[i], ny = y + dy[i];
            while(mp[nx][ny] == 3-player) {
                mp[nx][ny] = player;
                cnt[player]++;
                cnt[3-player]--;
                nx += dx[i];
                ny += dy[i];
            }
            flag = 1;
        }
    }
    if(flag == 1) {
        cnt[player]++;
    }
    else if(flag == 0)  {
        player = 3 - player;
        mp[x][y] = player;
        cnt[player]++;
        for(int i = 0; i < 8; i++) {
            if(isLegal(x, y, i)) {
                mp[x][y] = player;
                nx = x + dx[i], ny = y + dy[i];
                while(mp[nx][ny] == 3-player) {
                    mp[nx][ny] = player;
                    cnt[player]++;
                    cnt[3-player]--;
                    nx +=- dx[i];
                    ny += dy[i];
                }
            }
        }
    }
    player = 3-player;
    printf("Black - %2d White - %2d\n", cnt[2], cnt[1]);
}

int main() {
    //freopen("D:\\input.txt","r",stdin);
    //freopen("D:\\output.txt","w",stdout);
    //ios::sync_with_stdio(false);
    cin >> N;
    while(N--) {
        cnt[1] = cnt[2] = player = 0;
        memset(mp, 0, sizeof(mp));
        for(int i = 1; i <= 8; i++) {
            for(int j = 1; j <= 8; j++) {
                cin >> c;
                if(c == '-') mp[i][j] = 0;
                if(c == 'W') mp[i][j] = 1;
                if(c == 'B') mp[i][j] = 2;
                cnt[mp[i][j]]++;
            }
        }
        cin >> c;
        if(c == 'W') player = 1;
        else if(c == 'B') player = 2;
        while(cin >> op) {
            if(op == 'L') L();
            if(op == 'M')  {
                cin >> x;
                y = x % 10;
                x= x / 10;
                M(x, y);
            }
            if(op == 'Q') {
                printMp();
                if(N >= 1) cout << endl;
                break;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值