Login:
Username:
Password:
Remember login

Create new account


Submission ID: 200889
Problem ID: 110102
Code:

#include <vector>
#include <string>
#include <iostream>

class InputProcessor
{
public:
	InputProcessor()			{}
	virtual ~InputProcessor()	{}

	virtual void							process()	= 0;
	virtual int								getDataIndex()			{ return -1; }
	virtual const std::vector<int>*			getDigits(int _nIndex)	{ return  0; }
	virtual const std::vector<std::string>*	getChars(int _nIndex)	{ return  0; }
};

class MultiLineCharInput : public InputProcessor
{
public:
	MultiLineCharInput(bool _bZeroBreak)	{ bZeroBreak_ = _bZeroBreak; }
	virtual ~MultiLineCharInput()			{ clearCharData_(); }

	virtual void process()
	{
		if( aVecVecCharData_.empty() == false )
			clearCharData_();

		char buf[512];
		while( gets( buf ) )
		{
			std::vector<std::string>*  pVector = new std::vector<std::string>;
			char* pSzEach = strtok(buf, " ");
			while (pSzEach!= 0)
			{
				pVector->push_back( pSzEach );
				pSzEach = strtok(0, " ");
			}

			if( bZeroBreak_ && pVector->size() == 2 )
			{
				if( (*pVector)[0] == "0" && (*pVector)[1] == "0" )
				{
					delete pVector;
					return;
				}
			}
			aVecVecCharData_.push_back( pVector );
		}
	}

	virtual const std::vector<std::string>* getChars(int _nIndex)
	{
		if( _nIndex < 0 || aVecVecCharData_.empty() || _nIndex >= getDataIndex() )
			return 0;

		return aVecVecCharData_[ _nIndex ];
	}

	virtual int getDataIndex(){ return (int)aVecVecCharData_.size(); }

private:
	std::vector< std::vector<std::string>* > aVecVecCharData_;
	void clearCharData_()
	{
		int nSize = (int)aVecVecCharData_.size();
		for( int i = 0; i < nSize; ++i)
		{
			delete aVecVecCharData_[i];
		}
		aVecVecCharData_.clear();
	}

	bool bZeroBreak_;
};

class GridSpace
{
public:
	GridSpace( int _nRow, int _nCol, int _nInitialValue )
	{
		nRow_ = _nRow;
		nCol_ = _nCol;
		int nCount = nRow_ * nCol_;
		pArray_ = new int[ nCount ];
		memset( pArray_, _nInitialValue, (size_t)(nCount*sizeof(int)) );
	}

	virtual ~GridSpace(){ delete[] pArray_; }

	int* getData( int _nRow, int _nCol )
	{
		if( _nRow < 0 || _nCol < 0 || _nRow >= nRow_ || _nCol >= nCol_ )
			return 0;

		return &pArray_[ _nRow * nCol_ + _nCol ];
	}

	int getRowCount(){ return nRow_; }
	int getColCount(){ return nCol_; }

private:
	int  nRow_;
	int  nCol_;
	int* pArray_; 
};

class CodeDojo
{
public:
	CodeDojo()			{}
	virtual ~CodeDojo()	{}

	virtual void solveTheProblem( InputProcessor* _pInput ) = 0;
};

class CodeDojoMineSweeper : public CodeDojo
{
public:
	virtual void solveTheProblem( InputProcessor* _pInput )
	{
		int nTotalLine = _pInput->getDataIndex();
		int nProgressLine = 0;

		std::vector< GridSpace* > vecGridSpace;
		while( true )
		{
			const std::vector< std::string >* pVecFirstLine = _pInput->getChars(nProgressLine);
			if( pVecFirstLine == 0 )
				break;

			if( pVecFirstLine->size() != 2 )
				break;

			int nRow = atoi( (*pVecFirstLine)[0].c_str() );
			int nCol = atoi( (*pVecFirstLine)[1].c_str() );

			if( nRow <= 0 || nCol <=0  || nTotalLine < nCol)
				break;

			// ù°¶óÀÎ ÇØ¼® ¿Ï·á
			nProgressLine++;

			GridSpace* pSpace = new GridSpace( nRow, nCol, 0 );
			for( int i = 0; i < nRow; ++ i )
			{
				const std::vector< std::string >* pVecCharData = _pInput->getChars(nProgressLine);
				const std::string& strData = (*pVecCharData)[0];
				for( int j = 0; j < nCol; ++j)
				{
					if( strData[j] == '*' )
					{
						// ÀÚ±â´Â -1
						int* pData = pSpace->getData( i, j );
						if(pData)
							*pData = -1;

						// ˤ
						pData = pSpace->getData( i-1, j );
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ¾Æ·¡
						pData = pSpace->getData( i + 1, j);
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ÁÂ
						pData = pSpace->getData( i, j-1);
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ¿ì
						pData = pSpace->getData( i, j+1);
						if( pData && *pData != -1)
							*pData = *pData + 1;


						// ´ë°¢¼± Á»ó
						pData = pSpace->getData( i-1, j-1);
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ´ë°¢¼± ¿ì»ó
						pData = pSpace->getData( i-1, j+1);
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ´ë°¢¼± ÁÂÇÏ
						pData = pSpace->getData( i+1, j-1);
						if( pData && *pData != -1)
							*pData = *pData + 1;

						// ´ë°¢¼± ¿ìÇÏ
						pData = pSpace->getData( i+1, j+1);
						if( pData && *pData != -1)
							*pData = *pData + 1;
					}
				}
				nProgressLine++;
			}
			vecGridSpace.push_back(pSpace);
		}

		int nGridSpaceCount = (int)vecGridSpace.size();
		for(int i = 0; i < nGridSpaceCount; ++i)
		{
			int nFiledNum = i + 1;
			GridSpace* pGridSpace = vecGridSpace[i];
			int nRow = pGridSpace->getRowCount();
			int nCol = pGridSpace->getColCount();
			printf("Field #%d:\n", nFiledNum);
			for( int j = 0; j < nRow; ++j)
			{
				for( int k = 0; k < nCol; ++k )
				{
					int nData = *pGridSpace->getData(j, k);
					if( nData == -1)
						printf( "*" );
					else
						printf( "%d", nData );
				}
				printf("\n");
			}
			delete pGridSpace;
if( i < nGridSpaceCount - 1)
			printf("\n");
		}
	}
};

int main()
{
	//InputProcessor* pInput = new OneLineDigitInput;
	//InputProcessor* pInput = new MultiLineDigitInput(false);
	InputProcessor* pInput = new MultiLineCharInput(true);
	pInput->process();

	//CodeDojo* pCodeDojo = new CodeDojo3nPlus1;
	CodeDojo* pCodeDojo = new CodeDojoMineSweeper;
	pCodeDojo->solveTheProblem( pInput );

	delete pCodeDojo;
	delete pInput;
	return 0;
}