zoukankan      html  css  js  c++  java
  • 【习题 4-3 UVA

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

    【题解】

    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
    ", 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;
    }
    
  • 相关阅读:
    Java之JVM调优案例分析与实战(4)
    Qt浅谈之四十九俄罗斯方块(代码来自网络)
    自作聪明的开发
    Visual Studio 连接 SQL Server 的connectionStringz和
    删除反复行SQL举例
    一起学android之怎样设置TextView中不同字段的字体颜色(22)
    A008-drawable资源
    android 自己定义组件随着手指自己主动画圆
    一个简单的HTML5摇一摇实例
    关于事件的传递机制。
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9873299.html
Copyright © 2011-2022 走看看