B-Mine Sweeper II

2020 ICPC 上海站 B
原题链接:https://ac.nowcoder.com/acm/contest/10330/B
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意
给你两个大小为 n * m 的扫雷图 A, B,要求通过最多 n * m/2 次转换(转换指 把一个不是雷的格子变成雷, 或者把一个是雷的格子变成空白格),使得 B图的值 和 A图的值相等(值 就是指 扫雷图上的所有数字之和,没有数字或者雷的地方的值为0)

思路
其实这道题,只要相通一件事情,就可以了。
(一个地雷对于他对他周围的空白格的影响) 等价于 (把他周围的空白格都变成雷,自己变成空白格的的影响),也就是说,这个把这个图置反(雷变空白,空白变雷),和原图其实是一样的。且题目说,最多可以进行 mn/2 次转换操作,那么B图必然可以转化成A图或A的反(这个应该称为补图吧),也就是说,不存在 -1 的情况,所以只要把补图求出来,然后看看 A 或者 A的补图 哪个转换成B的次数小于 nm/2, 那个图就行了,可能讲的不是很清楚,见谅

AC代码

#include <bits/stdc++.h>
using namespace std;
#define maxn 1005

char G[maxn][maxn];// 图A
char G2[maxn][maxn];// 图A 的 补图
char K[maxn][maxn];// 图B
int n, m;

int main(){
	scanf("%d %d", &n, &m);
	for(int i=1;i<=n;++i) scanf("%s", G[i]+1);
	for(int i=1;i<=n;++i) scanf("%s", K[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			G2[i][j]=(G[i][j]=='.'?'X':'.'); // 求图A 的 补图
	int cnt=0, cnt2=0; //分别用来记录 把图A 转换 成图B 和 把图A的补图 转换成B 所需的转换次数
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(G[i][j]^K[i][j]) cnt++;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(G2[i][j]^K[i][j]) cnt2++;
	if(cnt<=n*m/2){//只要 转换次数符合条件 就输出
		for(int i=1;i<=n;++i) printf("%s\n", G[i]+1);
	}
	else{
		for(int i=1;i<=n;++i) printf("%s\n", G2[i]+1);
	}

	return 0;
}```

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,以下是一个使用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>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值