uva 220

本文详细解析了UVA220问题的逆推算法实现过程,包括使用C++实现的具体代码示例。该算法涉及棋盘游戏状态评估、合法走法判断及棋子翻转等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uva 220

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

string a[8],s;
int T;
char ch;
int dist[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int f[8];

int pan(int x,int y)
{
    bool fi = 0;
    for(int i = 0;i < 8; ++i)
    {
        int xx = x,yy = y;
        int t = 0;
        while(1)
        {
            xx += dist[i][0];
            yy += dist[i][1];
            t++;
            if(xx < 0 || xx >= 8 || yy < 0 || yy >= 8) break;
            if(a[xx][yy] == '-') break;
            if(a[xx][yy] == ch)
            {
                if(t > 1) f[i] = 1,fi = 1;
                break;
            }
        }
    }
    if(fi) return 1;
    return 0;
}

int check(int k)
{
    int ans = 0;
    for(int i = 0;i < 8; ++i)
        for(int j = 0;j < 8; ++j)
            if(a[i][j] == '-' && pan(i,j))
            {
                ans++;
                if(!k)
                {
                    if(ans > 1) printf(" ");
                    printf("(%d,%d)",i + 1,j + 1);
                }
            }
    return ans;
}

void work(int x,int y)
{
    a[x][y] = ch;
    for(int t = 0;t < 8; ++t) f[t] = 0;
    pan(x,y);
    for(int t = 0;t < 8; ++t)
        if(f[t])
        {
            int xx = x,yy = y;
            while(1)
            {
                xx += dist[t][0];
                yy += dist[t][1];
                if(a[xx][yy] == ch) break;
                a[xx][yy] = ch;
            }
        }
    int b = 0,w = 0;
    for(int i = 0;i < 8; ++i)
        for(int j = 0;j < 8; ++j)
            if(a[i][j] == 'B') b++;
            else if(a[i][j] == 'W') w++;
    printf("Black - %2d White - %2d\n",b,w);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        for(int i = 0;i < 8; ++i) cin>>a[i];
        cin>>ch;
        while(1)
        {
            cin>>s;
            if(s == "Q") break;
            if(s == "L")
                if(!check(0)) printf("No legal move.\n");  //没有合法操作
                else printf("\n");
            else
            {
                if(!check(1))
                {
                    if(ch == 'B') ch = 'W';
                    else ch = 'B';
                } //切换游戏者
                work(s[1] - '1',s[2] - '1'); //放棋子
                if(ch == 'B') ch = 'W';
                    else ch = 'B';
            }
        }
        for(int i = 0;i < 8; ++i)
            cout<<a[i]<<'\n';   //打印棋盘
        if(T) printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值