sgstep
BAN USERmore concisely
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool IsZero(char c)
{
return c == '0';
}
int main()
{
string s = "01101100111010010101001100010";
if (s.empty())
return 0;
cout << s << endl;
partition(s.begin(), s.end(), IsZero);
cout << s << endl;
}
#include <iostream>
#include <cstdlib>
using namespace std;
class BucketFill
{
const int m_XLen;
const int m_YLen;
int* m_bitmap;
int m_oldColor;
int m_newColor;
int& Color(int x, int y)
{
return m_bitmap[x * m_YLen + y];
}
typedef int Direction; // N = 0, NE = 1, E = 2, SE = 3, S = 4, SW = 5, W = 6, NW = 7, NOWHERE = 8
static const int NOWHERE = 8;
Direction Opposite(Direction d)
{
if (d >= 0 && d < NOWHERE)
return (d + 4) % 8;
else
return NOWHERE;
}
Direction FindNext(int x, int y)
{
for (Direction d = 0; d <= 7; ++d)
{
int xx = x;
int yy = y;
if (Move(xx, yy, d) && Color(xx, yy) == m_oldColor)
return d;
}
return NOWHERE;
}
bool Move(int& x, int& y, Direction d)
{
int xx = x;
int yy = y;
switch (d)
{
case 0: --yy; break; // N
case 1: ++xx; --yy; break; // NE
case 2: ++xx; break; // E
case 3: ++xx; ++yy; break; // SE
case 4: ++yy; break; // S
case 5: --xx; ++yy; break; // SW
case 6: --xx; break; // W
case 7: --xx; --yy; break; // NW
default: break; // NOWHERE
}
if (xx >= 0 && xx < m_XLen && yy >= 0 && yy < m_YLen)
{
x = xx;
y = yy;
return true;
}
return false;
}
int Base()
{
return ~m_oldColor & 0xFF;
}
void SetPrevDirection(int x, int y, Direction d)
{
Color(x, y) = Base() + d;
}
Direction GetPrevDirection(int x, int y)
{
return Color(x, y) - Base();
}
bool FillSome(int& x, int& y)
{
Direction d = FindNext(x, y);
if (d != NOWHERE)
{
Move(x, y, d);
SetPrevDirection(x, y, Opposite(d));
}
else
{
const Direction d = GetPrevDirection(x, y);
Color(x, y) = m_newColor;
if (d == NOWHERE)
return false;
Move(x, y, d);
}
return true;
}
public:
BucketFill(int* bitmap, int XLen, int YLen)
: m_bitmap(bitmap)
, m_XLen(XLen)
, m_YLen(YLen)
{}
void Fill(int newColor, int x0, int y0)
{
m_newColor = newColor;
m_oldColor = Color(x0, y0);
SetPrevDirection(x0, y0, NOWHERE);
while (FillSome(x0, y0))
;
}
};
void Print(int* bitmap, int XLen, int YLen, const char* msg)
{
cout << msg << endl;
for (int y = 0; y < YLen; ++y)
{
for (int x = 0; x < XLen; ++x)
{
cout << (char)(bitmap[x * YLen + y] & 0xFF);
}
cout << endl;
}
cout << endl;
}
int main()
{
const int oldColor = 'X';
const int newColor = '+';
const int otherColor = ' ';
const int x0 = 0;
const int y0 = 0;
const int XLen = 40;
const int YLen = 20;
int bitmap[XLen][YLen] = {0};
for (int x = 0; x < XLen; ++x)
for (int y = 0; y < YLen; ++y)
{
const bool isOld = rand() < RAND_MAX / 2;
bitmap[x][y] = isOld ? oldColor : otherColor;
}
Print(&bitmap[0][0], XLen, YLen, "before:");
BucketFill bf(&bitmap[0][0], XLen, YLen);
bf.Fill(newColor, x0, y0);
Print(&bitmap[0][0], XLen, YLen, "after:");
return 0;
}
Repalicesreedg, Accountant at ADP
Hi my name is Alice and i am working in an IT company. I am working here from last 5 ...
RepHi, I am Maria from Worcester, USA. I have been working as a Blogger in Enticing Express from last 2 ...
Repsherrymrex, Computer Scientist at CGI-AMS
I am Sherry from West Palm Beach USA, I started my journey in 2016 as a yoga teacher. I like ...
Repamysamson688, Accountant
Hi, I am an art teacher, good in all areas of art history, from ancient art through to contemporary art ...
Rephallieriddic, HR Executive Trainee at Barclays Capital
I am Hallie, Dedicated and experienced administrative secretary who excels at prioritizing , completing multiple tasks simultaneously and following through to ...
Repileenjconnelly, Cloud Support Associate at Absolute Softech Ltd
I have extensive experience in providing breaking news and giving precise details to the viewers. Interested in leading/working with ...
Repbrandysolsen, Area Sales Manager at AMD
I am a Social butterflies who enjoy travel.Have fun doing this job and project an atmosphere that reflects Amazon ...
Repanitajrouse, Kuwait airways reservations at American Airlines
I am Anita from Hastings USA. I am working as a manager in Sofa Express company. I Managed the schedules ...
- sgstep January 07, 2013