hdu1107+武林+模拟

题目意思:汉字描述的很清楚

注意事项:1、可能出现不能走动的情况;2、在初始状态时也可能比武;3、只有在同一个位置只有两个不同门派的弟子的时候才会比武,4、死掉的直接删除。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define ROWS 13
#define COLUMNS 13
//directions index 0 for down, 1 for up, 2 for right, 3 for left, 4 for right down, 5 for left up 
int dir[6][2] = {{1, 0}, {-1, 0},
                 {0, 1}, {0, -1},
                 {1, 1}, {-1, -1}};
// Position 
class Position
{
public:
    int row;
    int column;
public:
    Position(){}
    Position(int row, int column)
    :row(row), column(column){}
    ~Position(){}
    // judge where the current position is valid;
    bool valid()
    {
        return (row>0&&row<ROWS&&column>0&&column<COLUMNS);
    }
    //--------------------DebugS--------------------
    friend ostream&operator<<(ostream &out, const Position &p);
    friend istream&operator>>(istream&in, Position &p);
    //--------------------DebugE--------------------
};

//--------------------DebugS--------------------
ostream&operator<<(ostream &out, const Position &p)
{
    out << "(" << p.row << "," << p.column << ")";
    return out;
}
istream&operator>>(istream&in, Position &p)
{
    in>>p.row>>p.column;
    return in;
}
//--------------------DebugE--------------------
//wulin different school
class WLin
{
public:
    char school;
    Position curPos;
    int vitality;
    int interForce;
    int skill;
    int d;//current direction
    bool alive;//where alive
public:
    WLin()
    {
        if (school == 'S')d = 0;
        else if (school == 'W')d = 2;
        else d = 4;
        alive = true;
    }
    WLin(char school, Position p, int v, int i, int s)
    :school(school), curPos(p), vitality(v), interForce(i), skill(s)
    {
        if (school == 'S')d = 0;
        else if (school == 'W')d = 2;
        else d = 4;
        alive = true;
    }
    ~WLin(){}
    // 1 for 'S', 2 for 'W', 4 for 'E'
    int getValue()
    {
        if (school == 'S')return 1;
        if (school == 'W')return 2;
        if (school == 'E')return 4;
        return 1024;
    }

    bool isS()
    {
        return school == 'S';
    }

    bool isW()
    {
        return school == 'W';
    }

    bool isE()
    {
        return school == 'E';
    }
    /calculate the attack power
    int attackPower()
    {
        if (school == 'S'){
            return (int)((0.5*interForce+0.5*skill)*(vitality+10)/100);
        }
        if (school == 'W'){
            return (int)((0.8*interForce+0.2*skill)*(vitality+10)/100);
        }
        if (school == 'E'){
            return (int)((0.2*interForce+0.8*skill)*(vitality+10)/100);
        }
        return -1;
    }
    //--------------------DebugS--------------------
    friend ostream& operator<<(ostream &out, const WLin &wl);
    //--------------------DebugE--------------------
};
//--------------------DebugS--------------------
ostream& operator<<(ostream&out, const WLin &wl)
{
    out << wl.school << ": P->" << wl.curPos << "<- S->(" << wl.vitality
        << "," << wl.interForce << "," << wl.skill << ")<- L->" << wl.alive << "<- D->"
        << wl.d << "<-";
    return out;
}
//--------------------DebugE--------------------

vector <WLin > disciple;

int N;
//--------------------DebugS--------------------
ostream& operator<<(ostream&out, const vector<WLin > &d)
{
    for (int i = 0; i < (int)d.size(); ++i){
        out << d[i] << endl;
    }
    return out;
}
//--------------------DebugE--------------------
class Grid //grid
{
public:
    int ct;
    int value;//S=1, W=2, E=4;
    vector <int > diIndex;
public :
    Grid(){}
    Grid(int ct, int value)
    :ct(ct), value(value){}
    ~Grid(){}
    //two disciple with different school
    bool differentSchool()
    {
        return (ct==2&&(value==3||value==5||value==6));
    }
};

Grid grid[ROWS][COLUMNS];
//reset grid
void resetGrid()
{
    for (int i = 1; i < ROWS; ++i){
        for (int j = 1; j < COLUMNS; ++j){
            grid[i][j].diIndex.clear();
            grid[i][j].ct = grid[i][j].value = 0;
        }
    }
}
//go one step
void go()
{
    for (int i = 0; i < (int)disciple.size(); ++i){
        if (disciple[i].alive){
            Position curPos = disciple[i].curPos;
            int d = disciple[i].d;
            Position tmp(curPos.row+dir[d][0], curPos.column+dir[d][1]);
            if (!tmp.valid()){
                d ^= 1;
                tmp = Position(curPos.row+dir[d][0], curPos.column+dir[d][1]);
                if (!tmp.valid()){
                    grid[curPos.row][curPos.column].ct++;
                    grid[curPos.row][curPos.column].value += disciple[i].getValue();
                    grid[curPos.row][curPos.column].diIndex.push_back(i);
                    continue;
                }
                disciple[i].d = d;
            }
            disciple[i].curPos = tmp;
            grid[tmp.row][tmp.column].ct++;
            grid[tmp.row][tmp.column].value += disciple[i].getValue();
            grid[tmp.row][tmp.column].diIndex.push_back(i);
        }
    }
}
//count and print the final state
void coutAndPrintAns()
{
    int cts = 0, ctw = 0, cte = 0;
    int vs = 0, vw = 0, ve = 0;
    for (int i = 0; i < (int)disciple.size(); ++i){
        if (disciple[i].alive){
            if (disciple[i].isS()){
                cts++;
                vs += disciple[i].vitality;
            }
            else if (disciple[i].isW()){
                ctw++;
                vw += disciple[i].vitality;
            }
            else if (disciple[i].isE()){
                cte++;
                ve += disciple[i].vitality;
            }
        }
    }
    printf ("%d %d\n%d %d\n%d %d\n***\n", cts, vs, ctw, vw, cte, ve);
}
//solve the problem
void solve()
{
    do{
        for (int i = 1; i < ROWS; ++i){
            for (int j = 1; j < COLUMNS; ++j){
                if (grid[i][j].differentSchool()){
                    int ii1 = grid[i][j].diIndex[0];
                    int ii2 = grid[i][j].diIndex[1];

                    int ii1ap = disciple[ii1].attackPower();
                    int ii2ap = disciple[ii2].attackPower();

                    disciple[ii1].vitality -= ii2ap;
                    if (disciple[ii1].vitality <= 0){
                        disciple[ii1].alive = false;
                    }

                    disciple[ii2].vitality -= ii1ap;
                    if (disciple[ii2].vitality <= 0){
                        disciple[ii2].alive = false;
                    }
                }
            }
        }
        resetGrid();
        go();

        N--;
        //--------------------DebugS--------------------
        //cout << disciple << endl;
        //--------------------DebugE--------------------
    }while (N>0);
    coutAndPrintAns();
}

int main()
{
    int t;
    cin >> t;
    while (t--){
        cin >> N;
        disciple.clear();
        char school;
        Position p;
        int v, s, i, index = 0;
        resetGrid();
        while (cin >> school){
            if (school == '0')break;
            cin >> p >> i >> s >> v;
            //--------------------DebugS--------------------
            //cout << "out : " << school << " " << p << " " << i << " " << s << " " << v << endl;
            //--------------------DebugE--------------------
            WLin tmp(school, p, v, i, s);
            disciple.push_back(tmp);
            grid[p.row][p.column].ct++;
            grid[p.row][p.column].value += tmp.getValue();
            grid[p.row][p.column].diIndex.push_back(index);
            index++;
        }
        //--------------------DebugS--------------------
        //cout << disciple << endl;
        //--------------------DebugE--------------------
        solve();
    }
    return 0;
}


你可能感兴趣的:(模拟)