UVa220 黑白棋游戏

本地运行过,但就是WA,又调了很久,还是没找到错误,从网上找了篇AC代码想对比一下,但是细节变动有点大对比难度让人抓狂,不过很明显的是,两篇代码都还有很多值得优化的地方,索性合在一起大面积改动了一下,AC了,改动后的代码最大的特点就是思路更加清晰!说明模拟题,一要审好题意,二一定要把思路写清,重复的部分就拿出来合在一起,思路就会变得更清晰,代码也更简洁。

另外,没事少用全局变量。。。。。。。。。。。

代码:

#include <bits/stdc++.h>
using namespace std;
char cur,cur_con;
char tab[10][10];
int flag=1;
int flag_1=0;
const int dir[9][2]={{0,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};
void now()
{
        char t=cur;
        cur=cur_con;
        cur_con=t;
}
bool hefa(int r, int c)
{
    if(tab[r][c]!='-') return false;
    for(int i = 1; i <= 8; i++){
        int rr = r+dir[i][0], cc = c+dir[i][1];
        if(tab[rr][cc]==cur_con){
            while(tab[rr][cc]!=0){
                rr += dir[i][0]; cc += dir[i][1];
                if(tab[rr][cc]==cur){
                    return true;
                }
                if(tab[rr][cc]=='-' ) break;
            }
        }
    }
    return false;
}
void L()
{
    int numm=0;
    for(int i = 1; i <= 8; i++)
        for(int j = 1; j <= 8; j++){
            if(hefa(i, j)){
                if(numm++) printf(" (%d,%d)", i, j);
                else printf("(%d,%d)", i, j);
            }
        }
    if(!numm) printf("No legal move.\n");
    else printf("\n");
}
void out_cnt()
{
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= 8; i++){
        for(int j = 1; j <= 8; j++){
            if(tab[i][j]=='B') cnt1++;
            if(tab[i][j]=='W') cnt2++;
        }
    }
    printf("Black - %2d White - %2d\n", cnt1, cnt2);
}
void Mrc(const int r, const int c)
{
    tab[r][c] = cur;
    for(int i = 1; i <= 8; i++){
        bool ok = false;
        int rr = r+dir[i][0], cc = c+dir[i][1];
        if(tab[rr][cc]==cur_con){
            while(tab[rr][cc]!=0){
                rr += dir[i][0]; cc += dir[i][1];
                if(tab[rr][cc]==cur){
                    ok = true;
                    break;
                }
                if(tab[rr][cc]=='-' )
                    break;
            }
        }
        if(ok){
            rr = r, cc = c;
            while(true){
                rr += dir[i][0]; cc += dir[i][1];
                if(tab[rr][cc]==cur) break;
                else tab[rr][cc] = cur;
            }
        }
    }
    out_cnt();
}
void print()
{
    for(int i = 1; i <= 8; i++)
        puts(tab[i]+1);//还会自动换行啊?、
}
int main()
{
    freopen("data_wang.txt", "r", stdin);
    freopen("out_wang.txt", "w", stdout);
    int T; cin >> T; //cin.get();
    while(T--){
        memset(tab, 0, sizeof(tab));
        for(int i = 1; i <= 8; i++)
            scanf("%s", tab[i]+1);
        cin >> cur;
        if(cur=='W')
            cur_con='B';
        else
            cur_con='W';
        string cmd;
        while(cin >> cmd){
            if(cmd=="L"){
                L();
            }
            else if(cmd[0]=='M'){
                int r = cmd[1]-'0', c = cmd[2]-'0';
                if(!hefa(r, c)){
                    now();
                    Mrc(r, c);
                    now();
                }
                else if(hefa(r,c)){
                    Mrc(r, c);
                    now();
                }
            }
            else if(cmd=="Q"){
                    print();
                    break;
            }
        }
        if(T) printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值