UVA - 220 -Othello (模拟)

版权声明:沃斯里德小浩浩啊 https://blog.csdn.net/Healer66/article/details/83795241

链接:

https://vjudge.net/problem/UVA-220

题意:

见紫书96页

思路:

见代码

#include <bits/stdc++.h>
using namespace std;
char mp[10][10];
int f[2][10][10];//存那些位置可以放
int dx[] = {-1,-1,-1,1,1,1,0, 0};
int dy[] = {-1,0,1,-1,0,1,1,-1};
void is(int a,char b,int x,int y)//判断以该点端点的可以放有效棋子的令一点
{
    for(int k = 0; k < 8; k++)//八方向尝试
    {
        int tx = x + dx[k];
        int ty = y + dy[k];
        int num = 0;
        while(tx>=0 &&ty >= 0 && tx < 8 && ty < 8 && mp[tx][ty]==b)
        {
            tx+=dx[k];
            ty+=dy[k];
            num++;
        }
        if(num >= 1 &&mp[tx][ty] == '-')
            f[a][tx][ty] |= 1<<k;
    }
}
void init()//更新可以放的位置
{
    for(int i = 0; i < 8; i++)
        for(int j = 0; j < 8; j++)
            if(mp[i][j] == 'W')
                is(0,'B',i,j);
            else if(mp[i][j] == 'B')
                is(1,'W',i,j);
}
bool print(bool isprint,int p)//检查是否换人或输出
{
    bool flag = false;//输出格式控制
    for(int i = 0; i < 8; i++)
    {
           for(int j = 0; j < 8; j++)
                if(f[p][i][j])
                {
                    if(flag && isprint)
                        cout<<' ';
                    flag = true;
                    if(isprint)
                        cout<<'('<<i+1<<','<<j+1<<')';
                    else
                        return flag;
                }
    }
    if(isprint)
    {
        if(flag)
            cout<<endl;
        else
            cout<<"No legal move."<<endl;
    }
    return flag;
}
void move(int x,int y,int p)//放置新棋子
{
    char ch;
    if(p == 0)
        ch ='W';
    if(p == 1)
        ch = 'B';
    mp[x][y] = ch;
    for(int i = 0; i < 8; i++)
    {
        if(f[p][x][y] & 1<<i)
        {
            int tx = x - dx[i];
            int ty = y - dy[i];
            while(tx>=0 && ty>=0 &&tx < 8 && ty < 8&&mp[tx][ty]!=ch)
            {
                mp[tx][ty] = ch;
                tx -= dx[i];
                ty -= dy[i];
            }
        }
    }
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        memset(mp,0,sizeof mp);
        for(int i = 0; i < 8; i++)
        {
            cin>>mp[i];
        }
        char ch[5];
        int p ;
        cin>>ch;
        if(ch[0] == 'W') p =0;
        else p =1;
        while(ch[0] != 'Q')
        {
            memset(f,0,sizeof f);
            init();
            cin>>ch;
            if(ch[0] == 'L')
            {
                print(true,p);
            }
            else if(ch[0] == 'M')
            {
                if(!print(false,p))//如果没有当前棋色可以下的位置,那么换人
                    p ^= 1;
                move(ch[1]-'0'-1,ch[2]-'0'-1,p);
                p^=1;//下棋子后换人
                int wnum = 0;
                int bnum = 0;
                for(int i = 0; i < 8; i++)
                {
                    for(int j = 0; j < 8; j++)
                    {
                        if(mp[i][j] == 'W')
                            ++wnum;
                        if(mp[i][j] == 'B')
                            ++bnum;
                    }
                }
                printf("Black - %2d White - %2d\n", bnum, wnum);
            }
        }
        for(int i = 0; i <8; i++)
            cout<<mp[i]<<endl;
        if(n!=0)
            cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Healer66/article/details/83795241