• UVa 220 (黑白棋)


    这道题也是个模拟,没什么特别的想法,提交时一定要小心空格、 一定不要多,否则会WA

    这就是我冗长的代码:

    //UVa 220
    //Othello
    #define LOCAL
    #include <stdio.h> 
    #include <string.h> //use memset strcmp
    
    char chess[10][10], cnt;
    
    void Printf(int T) 
    {
        for(int i = 1; i <= 8; i++) {
            for(int j = 0; j < 8; j++)
                printf("%c", chess[i][j]);
            printf("
    ");
        }
        if(T != 0)
        printf("
    ");
    }
    
    int check(int x, int y, int choose)
    {
        char target;
        if(cnt == 'W') target = 'B'; 
        else target = 'W';
        if(x > 2 && chess[x-1][y] == target) 
            for(int i = x-2; i > 0; i--) { 
                if(chess[i][y] == '-')
                    break; 
                if(chess[i][y] == cnt) 
                    if(choose) {
                        for(int k = i+1; k <= x-1; k++) chess[k][y] = cnt;
                        break;    
                    }
                    else
                        return 1;
            }
        if(x < 7 && chess[x+1][y] == target)
            for(int i = x+2; i < 9; i++) {
                if(chess[i][y] == '-')
                    break; 
                if(chess[i][y] == cnt) 
                    if(choose) {
                        for(int k = x+1; k < i; k++) chess[k][y] = cnt;
                        break; 
                    }
                    else
                        return 1;
            }
        if(y > 1 && chess[x][y-1] == target)
            for(int i = y-2; i >= 0; i--) {
                if(chess[x][i] == '-')
                    break;
                if(chess[x][i] == cnt) 
                    if(choose) {
                        for(int k = i+1; k <= y-1; k++) chess[x][k] = cnt;
                        break;
                    }
                    else
                        return 1;
            }
        if(y < 6 && chess[x][y+1] == target)
            for(int i = y+2; i < 8; i++) {
                if(chess[x][i] == '-')
                    break;
                if(chess[x][i] == cnt) 
                    if(choose) {
                        for(int k = y+1; k < i; k++) chess[x][k] = cnt;
                        break; 
                    }    
                    else
                        return 1; 
            }
        int i = x; int j = y;
        if(chess[x-1][y-1] == target)
            while(--i && --j >= 0) {
                if(chess[i][j] == '-') 
                    break;
                if(chess[i][j] == cnt) 
                    if(choose) {
                        i = x; j = y; 
                        while(--i && --j >= 0) {
                            if(chess[i][j] == cnt) break;
                            chess[i][j] = cnt; 
                        }
                        break; 
                    }
                    else
                        return 1;
            }
        i = x; j = y; 
        if(chess[x+1][y+1] == target)
            while(++i < 9 && ++j < 8) {
                if(chess[i][j] == '-')
                    break;
                if(chess[i][j] == cnt) 
                    if(choose) {
                        i = x; j = y; 
                        while(++i < 9 && ++j < 8) {
                            if(chess[i][j] == cnt) break;
                            chess[i][j] = cnt; 
                        }
                        break; 
                    }
                    else
                        return 1;
            }
        i = x; j = y; 
        if(chess[x-1][y+1] == target)
            while(--i && ++j < 8) {
                if(chess[i][j] == '-')
                    break;
                if(chess[i][j] == cnt) 
                    if(choose) {
                        i = x; j = y; 
                        while(--i && ++j < 8) {
                            if(chess[i][j] == cnt) break;
                            chess[i][j] = cnt; 
                        }
                        break; 
                    }
                    else
                        return 1; 
            }
        i = x; j = y; 
        if(chess[x+1][y-1] == target) 
            while(++i < 9 && --j >= 0) {
                if(chess[i][j] == '-') 
                    break; 
                if(chess[i][j] == cnt) 
                    if(choose) {
                        i = x; j = y; 
                        while(++i < 9 && --j >= 0) {
                            if(chess[i][j] == cnt) break;
                            chess[i][j] = cnt; 
                        }
                        break; 
                    }
                    else
                        return 1;  
            }
        return 0;     
    }
    
    void Legal_Move()
    {
        int T = 0;
        for(int i = 1; i <= 8; i++) 
            for(int j = 0; j < 8; j++) 
                if(chess[i][j] == '-' && check(i, j, 0)) { 
                    if(!T) printf("(%d,%d)", i, j+1); 
                    else
                        printf(" (%d,%d)", i, j+1); 
                    T = 1;
                }
        if(!T) printf("No legal move.");
        printf("
    ");
    }
    
    void Move_To(int x, int y) 
    {
        if(!check(x, y, 0)) {
            if(cnt == 'W') cnt = 'B';
            else cnt = 'W';
        }
        int a = check(x, y, 1); //no use in a
        int numW = 0; int numB = 0;  
        chess[x][y] = cnt;
        for(int i = 1; i <= 8; i++)
            for(int j = 0; j < 8; j++) {
                if(chess[i][j] == 'W') numW++; 
                if(chess[i][j] == 'B') numB++;
            }
    //    Printf();
        printf("Black - %2d White - %2d
    ", numB, numW);
        if(cnt == 'W') cnt = 'B';
        else cnt = 'W';
    }
    
    int main()
    {
        #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        #endif
        int N;
        scanf("%d", &N); 
        while(N--) {
            for(int i = 1; i <= 8; i++) scanf("%s", chess[i]); 
            char a[5]; 
            scanf("%s", a); 
            cnt = a[0];
            while(scanf("%s", a) && strcmp(a, "Q") != 0) {
                if(!strcmp(a, "L")) Legal_Move();
                else Move_To(a[1]-'0', a[2]-'0'-1);        
            }
            Printf(N);
        }
        return 0;
    }
    语言c++计算机程序设计爱好者 不定期更新题目题解 望互相分享心得体会 有意留言加q
  • 相关阅读:
    PHP远程下载图片,微信头像存到本地,本地图片转base64
    jQuery Validate自定义错误信息,自定义方法
    创建自己的composer包
    js,JQ获取短信验证码倒计时
    JQ JS复制到剪贴板
    js,JQuery 生成二维码
    js,JQuery实现,带筛选,搜索的select
    HTML5拖放牛刀小试
    HTML5上传图片预览功能
    一次dropzone体验
  • 原文地址:https://www.cnblogs.com/yifeiWa/p/10341416.html
Copyright © 2020-2023  润新知