【UVA 220 --- Othello】模拟

【UVA 220 --- Othello】模拟

题目来源:点击进入【UVA 220 — Othello】

Description

Input

Output

Sample Input

2
--------
--------
--------
---WB---
---BW---
--------
--------
--------
W
L
M35
L
Q
WWWWB---
WWWB----
WWB-----
WB------
--------
--------
--------
--------
B
L
M25
L
Q

Sample Output

(3,5) (4,6) (5,3) (6,4)
Black - 1 White - 4
(3,4) (3,6) (5,6)
--------
--------
----W---
---WW---
---BW---
--------
--------
--------
No legal move.
Black - 3 White - 12
(3,5)
WWWWB---
WWWWW---
WWB-----
WB------
--------
--------
--------
--------

解题思路

根据题意模拟即可。

AC代码:

#include <stdio.h>
#include <string.h>
const int MAXN = 10;
char str[MAXN][MAXN],s[MAXN],ch;
int dir[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
bool vis[8];

bool fun(int a,int b)
{
    bool flag=false;
    for(int i=0;i<8;i++)
    {
        int x=a,y=b,num=0;
        while(true)
        {
            x+=dir[i][0];
            y+=dir[i][1];
            num++;
            if(x<0 || x>=8 || y<0 || y>=8 || str[x][y]=='-') break;
            if(str[x][y]==ch)
            {
                if(num>1) vis[i]=true,flag=true;
                break;
            }
        }
    }
    if(flag) return true;
    return false;
}

bool compare(bool flag)
{
    int num=0;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(str[i][j]=='-' && fun(i,j))
            {
                num++;
                if(flag)
                {
                    if(num>1) printf(" ");
                    printf("(%d,%d)",i + 1,j + 1);
                }
            }
        }
    }
    if(num) return true;
    return false;
}

void down(int a,int b)
{
    memset(vis,false,sizeof(vis));
    fun(a,b);
    str[a][b]=ch;
    int x,y;
    for(int i=0;i<8;i++)
    {
        if(!vis[i]) continue;
        x=a;y=b;
        while(true)
        {
            x+=dir[i][0];
            y+=dir[i][1];
            if(str[x][y]==ch) break;
            str[x][y]=ch;
        }
    }
    x=0; y=0;
    for(int i=0;i<8;i++)
        for(int j=0;j<8;j++)
            if(str[i][j]=='B') x++;
            else if(str[i][j]=='W') y++;
    printf("Black - %2d White - %2d\n",x,y);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<8;i++)
            scanf("%s",str[i]);
        getchar();
        scanf("%c",&ch);
        while(true)
        {
            scanf("%s",s);
            if(s[0]=='Q') break;
            else if(s[0]=='L')
            {
                if(compare(true)) printf("\n");
                else printf("No legal move.\n");
            }
            else
            {
                if(!compare(false))
                {
                    if(ch=='B') ch='W';
                    else ch='B';
                }
                down(s[1]-'0'-1,s[2]-'0'-1);
                if(ch=='B') ch='W';
                else ch='B';
            }
        }
        for(int i=0;i<8;i++)
            printf("%s\n",str[i]);
        if(T) printf("\n");
    }
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104262697