poj 2996 Help Me with the Game

/*
 * Author: stormdpzh
 * POJ: 2996 Help Me with the Game
 * Created Time: 2012/5/14 17:45:55
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long

using namespace std;

const char piece[5] = {'K', 'Q', 'R', 'B', 'N'};

struct Black {
    int kind;
    int column, row;
    
    Black(int _kind, int _column, int _row) : kind(_kind), column(_column), row(_row) {} 
    
    bool operator < (const Black &t) const {
        if(kind != t.kind)
            return kind < t.kind;
        else if(row == t.row)
            return column < t.column;
        else
            return row > t.row;
    }
};

struct White {
    int kind;
    int column, row;
    
    White(int _kind, int _column, int _row) : kind(_kind), column(_column), row(_row) {} 
    
    bool operator < (const White &t) const {
        if(kind != t.kind)
            return kind < t.kind;
        else if(row == t.row)
            return column < t.column;
        else
            return row < t.row;
    }
};

set<Black> black;
set<White> white;
char str[100];

int getPiece(char c)
{
    if(c == 'K' || c == 'k') return 0;
    else if(c == 'Q' || c == 'q') return 1;
    else if(c == 'R' || c == 'r') return 2;
    else if(c == 'B' || c == 'b') return 3;
    else if(c == 'N' || c == 'n') return 4;
    return 5;
}

int main()
{
    black.clear();
    white.clear();
    bool flag = true;
    int line = 0;
    gets(str);
    while(gets(str)) {
        if(flag) {
            rep(i, 8) {
                int id = 4 * i + 2;
                char ch = str[id];
                if(ch < 'Z' && ch > 'A') {
                    int tmp = getPiece(ch);
                    white.insert(White(tmp, i, 8 - line));
                }
                else if(ch < 'z' && ch > 'a') {
                    int tmp = getPiece(ch);
                    black.insert(Black(tmp, i, 8 - line));
                }
            }
            line++;
        }
        flag = !flag;
    }
    
    set<White>::iterator it = white.begin();
    printf("White: ");
    if(it != white.end()) {
        if(it->kind < 5)
            printf("%c%c%d", piece[it->kind], it->column + 'a', it->row);
        else
            printf("%c%d", it->column + 'a', it->row);
        it++;
    }
    while(it != white.end()) {
        if(it->kind < 5)
            printf(",%c%c%d", piece[it->kind], it->column + 'a', it->row);
        else
            printf(",%c%d", it->column + 'a', it->row);
        it++;
    }
    printf("\n");
    
    set<Black>::iterator it1 = black.begin();
    printf("Black: ");
    if(it1 != black.end()) {
        if(it1->kind < 5)
            printf("%c%c%d", piece[it1->kind], it1->column + 'a', it1->row);
        else
            printf("%c%d", it1->column + 'a', it1->row);
        it1++;
    }
    while(it1 != black.end()) {
        if(it1->kind < 5)
            printf(",%c%c%d", piece[it1->kind], it1->column + 'a', it1->row);
        else
            printf(",%c%d", it1->column + 'a', it1->row);
        it1++;
    }
    printf("\n");
    return 0;
}

模拟题还是很好写的,也没有什么算法的东西,不过得写的快一点才好。


你可能感兴趣的:(poj 2996 Help Me with the Game)