⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pku2612.txt

📁 北京大学 在线acm在线判题系统 一些题目解答
💻 TXT
字号:
Mine Sweeper 
Time Limit:1000MS  Memory Limit:65536K
Total Submit:1921 Accepted:761 

Description
The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses. If a positon not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below. 
 
Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the figures resembling asterisks represent mines. The leftmost image represents the partially played game. From the first image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines. 

Your job is to read the information for a partially played game and to print the corresponding board. 


Input
The first line of input contains a single postitive integer n <= 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined positon while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an x, and untouched positions by a period. The sample input corresponds to the middle figure above.

Output
Your output should represent the board, with each position filled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period. 

Sample Input


8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...


Sample Output


001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...


Source
Waterloo local 1999.10.02


Source

Problem Id:2612  User Id:Drizzle 
Memory:184K  Time:30MS
Language:G++  Result:Accepted

Source 

#include <iostream>
#include <cstring>

using namespace std;

int n;
char map[11][11];
char touch[11][11];
int result[11][11];
bool visited[11][11];
bool touched = false;

const int delta[8][2] = 
{
	{-1, 1}, {0, 1},{1, 1},
	{-1, 0}, {1, 0},
	{-1, -1},{0, -1}, {1, -1}
};

void Output()
{
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(touched && map[i][j] == '*')
			{
				cout << '*';
				continue;
			}

			if(touch[i][j] == 'x')
			{
				cout << result[i][j];
			}
			else
			{

					cout << ".";
			}
		}
		cout << endl;
	}
}

bool InRange(int x, int y)
{
	return x >= 0 && x < n && y >= 0 && y < n;
}

void Calc(int x, int y)
{
	for(int i = 0; i < 8; i++)
	{
		int xx = x + delta[i][0];
		int yy = y + delta[i][1];
		if(InRange(xx, yy) && map[xx][yy] == '*')
			result[x][y]++;
	}
}

void Go()
{
	int i, j;
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n; j++)
		{
			if(touch[i][j] == 'x')
			{
				if(map[i][j] == '*')
				{
					touched = true;
					result[i][j] = -1;
				}
				else
				{
					Calc(i, j);
				}
			}
		}
	}
}



int main()
{
	int i, j;
	memset(result, 0, sizeof(result));
	memset(visited, false, sizeof(visited));
	cin >> n;
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n; j++)
			cin >> map[i][j];
	}
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n; j++)
			cin >> touch[i][j];
	}
	Go();
	Output();

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -