【习题 4-3 UVA - 220】Othello

【链接】 我是链接,点我呀:)
【题意】

【题解】

legal被我打成leagal... 然后注意输出坐标的时候,格式是%2d.. 然后就没啥难的了。。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int dx[8]={0,-1,-1,-1,0,1,1,1};
const int dy[8]={-1,-1,0,1,1,1,0,-1};

const int N = 10;
char s[N+10][N+10];
char ope[N+10];
char cur[3];

void _change(){
    if (cur[0]=='W')
        cur[0] = 'B';
    else
        cur[0]='W';
}

bool ok(int x,int y){
    for (int i = 0;i < 8;i++){
        int cx = x,cy = y,ok = 0,cntnc = 0;
        while (1){
            cx+=dx[i],cy+=dy[i];
            if (cx>=1 && cx<=8 && cy>=1 && cy<=8){
                if (s[cx][cy]=='-') break;
                if (s[cx][cy]!=cur[0]) {
                    cntnc++;
                    continue;
                }
                if (cntnc>0) ok = 1;
                break;
            }else{
                break;
            }
        }
        if (ok) return 1;
    }
    return 0;
}

vector<pair<int,int> > getlegalposition(){
    vector<pair<int,int> > v;v.clear();
    for (int i = 1;i <= 8;i++)
        for (int j = 1;j <= 8;j++)
            if (s[i][j]=='-' && ok(i,j))
                    v.push_back({i,j});
    return v;
}

int main(){
    //freopen("/home/ccy/rush.txt","r",stdin);
    //freopen("/home/ccy/rush_out.txt","w",stdout);
    int T;
    cin >> T;
    int kase = 0;
    while (T--){
        if (kase>0) cout<<endl;
        kase++;
        for (int i = 1;i <= 8;i++) cin >> (s[i]+1);
        cin >> cur;
        while (cin >> ope){
            if (ope[0]=='L'){
                vector<pair<int,int> > v = getlegalposition();
                if (v.empty()){
                    cout<<"No legal move."<<endl;
                }else{
                    int cnt = 0;
                    for (pair<int,int> temp:v){
                        if (cnt>0) cout<<" ";
                        cnt++;
                        cout<<"("<<temp.first<<","<<temp.second<<")";
                    }
                    cout<<endl;
                }
            }
            if (ope[0]=='M'){
                int x = ope[1]-'0',y= ope[2]-'0';
                vector<pair<int,int> > v = getlegalposition();
                if (v.empty()) _change();

                s[x][y]=cur[0];
                int cntb = 0,cntw = 0;


                for (int i = 0;i < 8;i++){
                    int cx = x,cy = y,cntnc = 0;
                    while (1){
                        cx+=dx[i],cy+=dy[i];
                        if (cx>=1 && cx<=8 && cy>=1 && cy<=8){
                            if (s[cx][cy]=='-') break;
                            if (s[cx][cy]!=cur[0]) {
                                cntnc++;
                                continue;
                            }
                            if (cntnc>0){
                                for (int ccx = x,ccy = y;ccx!=cx || ccy!=cy;ccx+=dx[i],ccy+=dy[i]){
                                    s[ccx][ccy] = cur[0];
                                }
                            }
                            break;
                        }else{
                            break;
                        }
                    }
                }

                _change();

                for (int i = 1;i <= 8;i++)
                    for (int j = 1;j <= 8;j++)
                        if (s[i][j]=='B')
                            cntb++;
                        else if (s[i][j]=='W')
                            cntw++;
                printf("Black - %2d White - %2d\n", cntb, cntw);
            }
            if (ope[0]=='Q'){
                for (int i = 1;i <= 8;i++){
                    for (int j = 1;j <= 8;j++)
                        cout<<s[i][j];
                    cout<<endl;
                }
                break;
            }
        }
    }
    return 0;
}
posted @ 2018-10-29 21:10  AWCXV  阅读(122)  评论(0编辑  收藏  举报