U E D R , A S I H C RSS

Mine Sweeper/허아영

코드

2006-01-07 14:23:36 0.008 Minimum

~cpp
#include <iostream>

using namespace std;

int main() 
{
	int m, n, i, j;		// field size
	int fieldNum = 1; // field number
	char ** inputField;
	int ** outputField;
	while(cin >> m >> n && m != 0 && n != 0){
		inputField = new char *[m+2];
		outputField = new int *[m+2];
		for(i = 0; i < m+2; i++)
		{
			inputField[i] = new char[n+2];
			outputField[i] = new int[n+2];
		}
		for(i = 1; i <= m; i++)		//input 
		{
			cin >> inputField[i] + 1;
		}
		for(i = 0; i < m+2; i++)
		{
			for(j = 0; j < n+2; j++)
			{
				outputField[i][j] = 0;				
			}
		}
		
		for(i = 1; i <= m; i++)
		{
			for(j = 1; j <= n; j++)
			{
				if(inputField[i][j] == '*')		//주의 : * + 1 
				{
					outputField[i][j] = '*';
					outputField[i-1][j-1] += 1;	// 대각선 왼쪽 위
					outputField[i-1][j] += 1;	// 위쪽
					outputField[i-1][j+1] += 1; // 대각선 오른쪽 위
					outputField[i][j+1] += 1;	// 오른쪽
					outputField[i+1][j+1] += 1;	// 대각선 오른쪽 아래
					outputField[i+1][j] += 1;	// 아래
					outputField[i+1][j-1] += 1;	// 대각선 왼쪽 아래
					outputField[i][j-1] += 1;	// 왼쪽
				}
			}
		}
		cout << "Field #" << fieldNum << ":" << endl;
		for( i = 1; i <= m; i++)		//output 
		{
			for(int j = 1; j <= n; j++)	
			{
				if(outputField[i][j] >= 10)
					cout << '*';
				else
					cout << outputField[i][j] ;
			}
			cout << endl;
		}
		fieldNum++;
	}	
	cout << endl;
	
	for(i = 0; i < m+2; i++)
	{
		delete[] inputField[i];
		delete[] outputField[i];
	}
	delete inputField;
	delete outputField;
	return 0;
}


----
MineSweeper
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0216 sec