smatskevich

PuzzlePosition

Mar 13th, 2017
612
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <assert.h>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. class CPosition {
  6. public:
  7.     explicit CPosition( const std::string& source );
  8.  
  9.     // Передвижение пустышки вверх.
  10.     CPosition Up() const;
  11.  
  12. private:
  13.     unsigned long long data; // 16 ячеек по 4 бита каждая.
  14.     int nullPlace;
  15.  
  16.     void setAt( int place, unsigned char value );
  17.     unsigned char getAt( int place ) const;
  18. };
  19.  
  20. const unsigned long long Masks[] = {
  21.     0x000000000000000F,
  22.     0x00000000000000F0,
  23.     0x0000000000000F00,
  24.     0x000000000000F000,
  25.     0x00000000000F0000,
  26.     0x0000000000F00000,
  27.     0x000000000F000000,
  28.     0x00000000F0000000,
  29.     0x0000000F00000000,
  30.     0x000000F000000000,
  31.     0x00000F0000000000,
  32.     0x0000F00000000000,
  33.     0x000F000000000000,
  34.     0x00F0000000000000,
  35.     0x0F00000000000000,
  36.     0xF000000000000000,
  37. };
  38.  
  39. const unsigned long long AntiMasks[] = {
  40.     0xFFFFFFFFFFFFFFF0,
  41.     0xFFFFFFFFFFFFFF0F,
  42.     0xFFFFFFFFFFFFF0FF,
  43.     0xFFFFFFFFFFFF0FFF,
  44.     0xFFFFFFFFFFF0FFFF,
  45.     0xFFFFFFFFFF0FFFFF,
  46.     0xFFFFFFFFF0FFFFFF,
  47.     0xFFFFFFFF0FFFFFFF,
  48.     0xFFFFFFF0FFFFFFFF,
  49.     0xFFFFFF0FFFFFFFFF,
  50.     0xFFFFF0FFFFFFFFFF,
  51.     0xFFFF0FFFFFFFFFFF,
  52.     0xFFF0FFFFFFFFFFFF,
  53.     0xFF0FFFFFFFFFFFFF,
  54.     0xF0FFFFFFFFFFFFFF,
  55.     0x0FFFFFFFFFFFFFFF
  56. };
  57.  
  58. CPosition::CPosition( const std::string& source ) :
  59.     data( 0 ),
  60.     nullPlace( 0 )
  61. {
  62.     std::istringstream stringStream( source );
  63.     for( char i = 0; i < 16; ++i ) {
  64.         unsigned short value = 0;
  65.         stringStream >> value;
  66.         setAt( i, static_cast<unsigned char>( value ) );
  67.         if( value == 0 ) {
  68.             nullPlace = i;
  69.         }
  70.     }
  71. }
  72.  
  73. // Установка значения в некоторую позицию.
  74. void CPosition::setAt( int place, unsigned char value )
  75. {
  76.     data = ( data & AntiMasks[place] ) | ( static_cast<long long>( value ) << ( place << 2 ) );
  77. }
  78.  
  79. // Получение того, что лежит в некоторой позиции.
  80. unsigned char CPosition::getAt( int place ) const
  81. {
  82.     return static_cast<unsigned char>( ( data & Masks[place] ) >> ( place << 2 ) );
  83. }
  84.  
  85. CPosition CPosition::Up() const
  86. {
  87.     assert( nullPlace >= 4 );
  88.  
  89.     CPosition result( *this );
  90.  
  91.     // Ставим пустышку выше.
  92.     result.setAt( nullPlace - 4, 0 );
  93.     // Ставим то, что было выше, на то место, где была пустышка.
  94.     result.setAt( nullPlace, getAt( nullPlace - 4 ) );
  95.     result.nullPlace -= 4;
  96.     return result;
  97. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×