POJ-2996 Help Me with the Game 模拟

该题为一道纯模拟题,不需要任何算法。在选择棋子的时候定义好排序规则,将其一次排序出来。

代码如下:

#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int pos = 0;

int hash[255];

struct Node
{
    char kind;
    int x, y;
    bool operator < (Node t) const
    {
        if (hash[kind] != hash[t.kind]) {
            return hash[kind] > hash[t.kind];
        }
        else if (isupper(kind)){
            if (y != t.y) {
                return y < t.y;
            }
            else {
                return x < t.x;
            }
        }
        else {
            if (y != t.y) {
                return y > t.y;
            }
            else {
                return x < t.x;
            }
        }
    }
}e[100];

char s[100];

void pre()
{
    hash['K'] = 100, hash['Q'] = 99, hash['R'] = 98;
    hash['B'] = 97, hash['N'] = 96, hash['P'] = 95;
    hash['k'] = 94, hash['q'] = 93, hash['r'] = 92;
    hash['b'] = 91, hash['n'] = 90, hash['p'] = 89;    
}

void deal(int y)
{
    int len = strlen(s);
    for (int i = 0; i < len; ++i) {
        if (isalpha(s[i])) {
            ++pos;
            e[pos].y = y;
            e[pos].x = ((i-2)>>2)+1; 
            e[pos].kind = s[i];
        }
    }
}

void print()
{
    int i;
    printf("White: ");
    for (i = 1; i <= pos && isupper(e[i].kind); ++i) {
        if (i == 1) {
            if (e[i].kind != 'P') {
                printf("%c%c%d", e[i].kind, e[i].x-1+'a', e[i].y);
            }
            else {
                printf("%c%d", e[i].x-1+'a', e[i].y);
            }
        }
        else {
            if (e[i].kind != 'P') {
                printf(",%c%c%d", e[i].kind, e[i].x-1+'a', e[i].y);
            }
            else {
                printf(",%c%d", e[i].x-1+'a', e[i].y);
            }
        }
    }    
    printf("\nBlack: ");
    for (int j = i; j <= pos; ++j) {
        if (j == i) {
            if (e[j].kind != 'p') {
                printf("%c%c%d", e[j].kind-32, e[j].x-1+'a', e[j].y);
            }
            else {
                printf("%c%d", e[j].x-1+'a', e[j].y);
            }
        }
        else {
            if (e[j].kind != 'p') {
                printf(",%c%c%d", e[j].kind-32, e[j].x-1+'a', e[j].y);
            }
            else {
                printf(",%c%d", e[j].x-1+'a', e[j].y);
            }
        }
    }
    puts("");
}

int main()
{
    pre();
    for (int i = 1; i <= 17; ++i) {
        gets(s);
        if ((i & 1) == 0) {  // 说明该行为有效行 
            deal(9-(i>>1));
        }
    }
    sort(e+1, e+1+pos);
    print();
//    system("pause");
    return 0;    
}
posted @ 2012-06-30 11:16  沐阳  阅读(176)  评论(0编辑  收藏  举报