U E D R , A S I H C RSS

Mine Sweeper/신재동

MineSweeper/신재동

~cpp 
#include <iostream>
#include <vector>
using namespace std;

const int MINE = -1;
const int MOVE[3] = {-1, 0, 1};

void initializeBoard(int maxRow, int maxCol, vector< vector<int> >& board)
{
	board.resize(maxRow);
	for(int i = 0; i < maxRow; i++)
		board[i].resize(maxCol);
}

bool isInBoard(int row, int col, int moveRow, int moveCol, const vector< vector<int> >& board)
{
	return (row + MOVE[moveRow] < 0 || row + MOVE[moveRow] >= board.size()) ||
		(col + MOVE[moveCol] < 0 || col + MOVE[moveCol] >= board[0].size());
}

void checkMine(int row, int col, vector< vector<int> >& board)
{	
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			if((MOVE[i] == 0 && MOVE[j] == 0) ||
				isInBoard(row, col, i, j, board) ||
				(board[row + MOVE[i]][col + MOVE[j]] == MINE))
				continue;
			
			board[row + MOVE[i]][col + MOVE[j]] += 1;
		}
	}
}

void setMinesOnBoard(vector< vector<int> >& board)
{	
	for(int i = 0; i < board.size(); i++)
	{
		cin.get(); // 이 한 줄이 삽질의 원흉!!
		for(int j = 0; j < board[0].size(); j++)
		{
			char c = cin.get();
			if(c == '*')
			{
				board[i][j]	= MINE;
				checkMine(i, j, board);
			}
		}
	}
}

void showBoard(const vector< vector<int> >& board)
{
	for(int i = 0; i < board.size(); i++)
	{
		for(int j = 0; j < board[0].size(); j++)
		{		
			if(board[i][j] == MINE)
				cout << "*" << " ";
			else
				cout << board[i][j] << " ";
		}
		cout << endl;
	}
}

int main()
{
	int maxRow, maxCol;
	cin >> maxRow >> maxCol;

	vector< vector<int> > board;
	initializeBoard(maxRow, maxCol, board);

	setMinesOnBoard(board);

	showBoard(board);
	return 0;
}

cin.get()으로 개행문자(\n)를 먹어야 하는 걸 깜박해서 계속 입력과 다른 출력이 나와 상당히 삽질... --재동
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0209 sec