0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第8回オフラインリアルタイムどう書くの問題の回答例。C++で。

Posted at

周回遅れ再び。
大力技。

お題はこちら http://qiita.com/items/709d61dff282cff7a890


# include <iostream>
# include <sstream>
# include <iomanip>
# include <string>
# include <vector>
# include <utility>

typedef std::pair<int, int> Pos;
typedef std::vector<Pos>    Positions;

std::string solve(const std::string& input)
{
    std::istringstream iss(input);
    unsigned long wall;
    unsigned long bomb;
    char          delimiter;
    iss >> std::hex >> wall >> delimiter >> bomb;

    int       field[5][6];
    Positions bombs;
    for(int i = 0; i < 30; ++i)
    {
        field[i / 6][i % 6] = (wall >> (31 - i)) & 1;
        if(((bomb >> (31 - i)) & 1) != 0)
        {
            bombs.push_back(std::make_pair(i / 6, i % 6));
        }
    }

    for(Positions::iterator i = bombs.begin(); i != bombs.end(); ++i)
    {
        field[i->first][i->second] = 2;

        for(int d = 1; d < 5; ++d)
        {
            int row = i->first + d;
            int col = i->second;
            if((row >= 5) || (field[row][col] == 1))
            {
                break;
            }
            else
            {
                field[row][col] = 2;
            }
        }

        for(int d = 1; d < 5; ++d)
        {
            int row = i->first - d;
            int col = i->second;
            if((row < 0) || (field[row][col] == 1))
            {
                break;
            }
            else
            {
                field[row][col] = 2;
            }
        }

        for(int d = 1; d < 6; ++d)
        {
            int row = i->first;
            int col = i->second + d;
            if((col >= 6) || (field[row][col] == 1))
            {
                break;
            }
            else
            {
                field[row][col] = 2;
            }
        }

        for(int d = 1; d < 6; ++d)
        {
            int row = i->first;
            int col = i->second - d;
            if((col < 0) || (field[row][col] == 1))
            {
                break;
            }
            else
            {
                field[row][col] = 2;
            }
        }
    }

    unsigned long result = 0;

    for(int i = 0; i < 30; ++i)
    {
        result <<= 1;
        result |= (field[i / 6][i % 6] == 2 ? 1 : 0);
    }
    result <<= 2;

    std::ostringstream oss;
    oss.fill('0');
    oss << std::hex << std::setw(8) << result;

    return oss.str();
}

void test(const std::string& input, const std::string& expected)
{
    std::string actual = solve(input);
    std::cout
        << "expected: " << expected
        << " / actual: " << actual
        << " => " << (actual == expected ? "o" : "x") << "\n";
}

int main(int, char* [])
{
    /*0*/ test( "802b1200/01400c20", "53c40cfc" );
    /*1*/ test( "28301068/84080504", "d64fef94" );
    /*2*/ test( "100a4010/80010004", "e241850c" );
    /*3*/ test( "81020400/000000fc", "0e3cfbfc" );
    /*4*/ test( "80225020/7e082080", "7fdd24d0" );
    /*5*/ test( "01201200/40102008", "fe1861fc" );
    /*6*/ test( "00201000/01000200", "43c48f08" );
    /*7*/ test( "00891220/81020408", "ff060c1c" );
    /*8*/ test( "410033c0/0c300000", "3cf0c000" );
    /*9*/ test( "00000000/01400a00", "7bf7bf78" );
    /*10*/ test( "00000000/20000a00", "fca2bf28" );
    /*11*/ test( "00000000/00000000", "00000000" );
    /*12*/ test( "00cafe00/00000000", "00000000" );
    /*13*/ test( "aaabaaaa/50000000", "51441040" );
    /*14*/ test( "a95a95a8/56a56a54", "56a56a54" );
    /*15*/ test( "104fc820/80201010", "ea30345c" );
    /*16*/ test( "4a940214/05000008", "05000008" );
    /*17*/ test( "00908000/05000200", "ff043f48" );
    /*18*/ test( "00c48c00/fe1861fc", "ff3873fc" );
    /*19*/ test( "00000004/81020400", "fffffff0" );
    /*20*/ test( "111028b0/40021100", "e08fd744" );
    /*21*/ test( "6808490c/01959000", "17f7b650" );
    /*22*/ test( "30821004/81014040", "c75de5f8" );
    /*23*/ test( "0004c810/10003100", "fe4937c4" );
    /*24*/ test( "12022020/88200000", "edf08208" );
    /*25*/ test( "2aa92098/01160000", "45165964" );
    /*26*/ test( "00242940/10010004", "fc43c43c" );
    /*27*/ test( "483c2120/11004c00", "33c3de10" );
    /*28*/ test( "10140140/44004a04", "eda3fe3c" );
    /*29*/ test( "0c901d38/72602200", "f36da280" );

    return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?