搜索04 Mine Sweeper

题目链接

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...

描述

难点在于对题目的理解,使用普通的搜索就行。

#include<cstdio>
using namespace std;
char map[11][11],visit[11][11];
int count[11][11],n;
int dx[8]= {-1,-1,-1,0, 0,1,1, 1}; //这里定义8个方向
int dy[8]= {-1, 0, 1,1,-1,1,0,-1};
int bflag=0;
void clear() {
	for(int i=0; i<11; i++) for(int j=0; j<11; j++) count[i][j]=0;
}
void search(int x,int y) {//雷的位置是(x,y),从雷的位置出发,对雷周围的8个方格处理 
	for(int i=0; i<8; i++) {
		int tx = x+dx[i];
		int ty = y+dy[i];
		if(tx<0||tx>=n||ty<0||ty>=n) continue;
		if(visit[tx][ty]=='x') {
			count[tx][ty]++;
		}
	}
}
int main() {
	//freopen("in.txt","r",stdin);
	while(scanf("%d",&n)!=EOF) {
		bflag=0;
		clear();
		getchar();
		for(int i=0; i<n; i++) {//输入雷区分布图 
			for(int j=0; j<n; j++) {
				scanf("%c",&map[i][j]);
			}
			getchar();
		}
		for(int i=0; i<n; i++) {//输入已经踩过的区域 
			for(int j=0; j<n; j++) {
				scanf("%c",&visit[i][j]);
			}
			getchar();
		}
		for(int i=0; i<n; i++) {//搜寻含有雷的位置,进行计数统计 
			for(int j=0; j<n; j++) {
				if(map[i][j]=='*') {
					if(visit[i][j]=='x') bflag=1;
					search(i,j);
				}
			}
		}
		for(int i=0; i<n; i++) {//对vis数组进行进一步的处理 
			for(int j=0; j<n; j++) {
				if(bflag) {//如果碰到了雷 
					if(map[i][j]=='*') visit[i][j]='*';
					else {
						if(visit[i][j]=='x') visit[i][j]='0'+count[i][j];
					}
				} else {
					if(visit[i][j]=='x') visit[i][j]='0'+count[i][j];
				}
			}
		}
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				printf("%c",visit[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用uniapp编写的扫雷游戏的示例代码: <template> <div class="mine-sweeper"> <div class="mine-sweeper-header"> <div class="mine-sweeper-title">扫雷游戏</div> <div class="mine-sweeper-timer">{{ timer }}</div> <div class="mine-sweeper-reset" @click="reset">重新开始</div> </div> <div class="mine-sweeper-board"> <div class="mine-sweeper-row" v-for="(row, rowIndex) in board" :key="rowIndex"> <div class="mine-sweeper-cell" v-for="(cell, cellIndex) in row" :key="cellIndex" @click="clickCell(rowIndex, cellIndex)" :class="{ 'mine-sweeper-cell-revealed': cell.revealed, 'mine-sweeper-cell-flagged': cell.flagged }"> <div class="mine-sweeper-cell-content"> <div class="mine-sweeper-cell-mine" v-if="cell.revealed && cell.mine"></div> <div class="mine-sweeper-cell-number" v-if="cell.revealed && !cell.mine && cell.number > 0">{{ cell.number }}</div> </div> </div> </div> </div> </div> </template> <script> export default { data() { return { board: [], mines: 10, rows: 8, cols: 8, timer: 0, intervalId: null, gameOver: false, gameWon: false, }; }, created() { this.reset(); }, methods: { reset() { this.board = this.generateBoard(this.rows, this.cols, this.mines); this.timer = 0; this.gameOver = false; this.gameWon = false; clearInterval(this.intervalId); this.intervalId = setInterval(() => { this.timer++; }, 1000); }, generateBoard(rows, cols, mines) { const board = []; for (let i = 0; i < rows; i++) { const row = []; for (let j = 0; j < cols; j++) { row.push({ mine: false, revealed: false, flagged: false, number: 0 }); } board.push(row); } let minesPlaced = 0; while (minesPlaced < mines) { const row = Math.floor(Math.random() * rows); const col = Math.floor(Math.random() * cols); if (!board[row][col].mine) { board[row][col].mine = true; minesPlaced++; } } for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { if (board[i][j].mine) { continue; } let count = 0; for (let ii = Math.max(0, i - 1); ii <= Math.min(rows - 1, i + 1); ii++) { for (let jj = Math.max(0, j - 1); jj <= Math.min(cols - 1, j + 1); jj++) { if (board[ii][jj].mine) { count++; } } } board[i][j].number = count; } } return board; }, clickCell(row, col) { if (this.gameOver || this.gameWon) { return; } const cell = this.board[row][col]; if (cell.flagged) { return; } if (cell.mine) { this.gameOver = true; clearInterval(this.intervalId); alert('游戏结束'); return; } cell.revealed = true; if (cell.number === 0) { this.revealNeighbors(row, col); } if (this.checkWin()) { this.gameWon = true; clearInterval(this.intervalId); alert('你赢了'); } }, revealNeighbors(row, col) { for (let i = Math.max(0, row - 1); i <= Math.min(this.rows - 1, row + 1); i++) { for (let j = Math.max(0, col - 1); j <= Math.min(this.cols - 1, col + 1); j++) { const cell = this.board[i][j]; if (!cell.revealed && !cell.flagged) { cell.revealed = true; if (cell.number === 0) { this.revealNeighbors(i, j); } } } } }, checkWin() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.cols; j++) { const cell = this.board[i][j]; if (!cell.revealed && !cell.mine) { return false; } } } return true; }, }, }; </script> <style scoped> .mine-sweeper { font-family: Arial, sans-serif; text-align: center; } .mine-sweeper-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; } .mine-sweeper-title { font-size: 24px; font-weight: bold; } .mine-sweeper-timer { font-size: 24px; font-weight: bold; } .mine-sweeper-reset { font-size: 18px; cursor: pointer; } .mine-sweeper-board { display: inline-block; border: 1px solid black; border-radius: 5px; padding: 10px; } .mine-sweeper-row { display: flex; } .mine-sweeper-cell { width: 30px; height: 30px; border: 1px solid black; margin-right: -1px; margin-bottom: -1px; position: relative; cursor: pointer; } .mine-sweeper-cell:hover { background-color: #eee; } .mine-sweeper-cell-revealed { background-color: #ddd; cursor: default; } .mine-sweeper-cell-flagged:before { content: '🚩'; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; } .mine-sweeper-cell-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .mine-sweeper-cell-mine:before { content: '💣'; font-size: 24px; } .mine-sweeper-cell-number { font-size: 18px; font-weight: bold; } </style>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值