poj2993 与2996相反的模拟

本文详细解析了一个使用C++实现的棋盘布局程序,包括输入读取、棋子定位、排序和输出棋盘布局的过程。程序通过结构体、字符串操作、排序算法等关键步骤实现了棋盘的动态生成。

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

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
struct data {
       int r,l;
       char ch;
}chess[65];
string s;
char ch,ch1,ch2;
int tot,p,w;
bool cmp(data a,data b) {
     if (a.r>b.r) return true;
     else if (a.r==b.r && a.l<b.l) return true;
     return false;
}
int main() {
    tot = 0;
    cin >> s;
    scanf("%c",&ch);
    while (scanf("%c",&ch) && ch!='\n') {
          if (ch<97 && ch!=',') {
                     scanf("%c%c",&ch1,&ch2);
                     tot++;
                     chess[tot].ch = ch;
                     chess[tot].r = ch2-48;
                     chess[tot].l = ch1-96;
          }
          else if (ch!=',') {
                  scanf("%c",&ch1);
                  tot++;
                  chess[tot].ch = 'P';
                  chess[tot].r = ch1-48;
                  chess[tot].l = ch-96;
          }
    }
    cin >> s;
    scanf("%c",&ch);
    while (scanf("%c",&ch) && ch!='\n') {
          if (ch<97 && ch!=',') {
                     scanf("%c%c",&ch1,&ch2);
                     tot++;
                     chess[tot].ch = ch+32;
                     chess[tot].r = ch2-48;
                     chess[tot].l = ch1-96;
          }
          else if (ch!=',') {
                  scanf("%c",&ch1);
                  tot++;
                  chess[tot].ch = 'p';
                  chess[tot].r = ch1-48;
                  chess[tot].l = ch-96;
          }
    }
    sort(chess+1,chess+1+tot,cmp);
    p = 1;
    printf("+---+---+---+---+---+---+---+---+\n");
    for (int i=1;i<=8;i++) {
        for (int k=1;k<=8;k++) {
            if ((i+k)%2==0) w = 0;
            else w = 1;
            printf("|");
            if (w) printf(":"); else printf(".");
            if (chess[p].r==9-i && chess[p].l==k) {
                                printf("%c",chess[p].ch);
                                p++;
            }
            else if (w) printf(":"); else printf(".");
            if (w) printf(":"); else printf(".");
        }
        printf("|\n+---+---+---+---+---+---+---+---+\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值