zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    怎样写贪吃蛇小游戏?用100行python代码轻松解决!
    面试必问的celery,你了解多少?
    您的机器学习环保吗?一只AI训练排出180吨二氧化碳
    NLP技术应用到音乐领域,分分钟让你变成音乐大师!
    数据可视化“升级”修炼宝典
    一文掌握Python可视化库的两大王者
    多线程-模拟阻塞queue队列
    设计模式-单例模式
    多线程之wait,notify,volatile,synchronized,sleep
    spring与quartz整合
  • 原文地址:https://www.cnblogs.com/yifeiWa/p/10341416.html
Copyright © 2011-2022 走看看