2020ICPC上海 B Mine Sweeper II(思维)

链接

题目描述

A mine-sweeper map X {X} X can be expressed as an n × m n\times m n×m grid. Each cell of the grid is either a mine cell or a non-mine cell. A mine cell has no number on it. Each non-mine cell has a number representing the number of mine cells around it. (A cell is around another cell if they share at least one common point. Thus, every cell that is not on the boundary has 8 8 8 cells around it.) The following is a 16 × 30 16\times 30 16×30 mine-sweeper map where a flagged cell denotes a mine cell and a blank cell denotes a non-mine cell with number 0 0 0.

Given two mine-sweeper maps A , B {A, B} A,B of size n × m n\times m n×m, you should modify at most ⌊ n m 2 ⌋ \left\lfloor\frac{nm}{2}\right\rfloor 2nm (i.e. the largest nonnegative integer that is less than or equal to n m 2 \frac{nm}{2} 2nm ) cells in B {B} B (from a non-mine cell to a mine cell or vice versa) such that the sum of numbers in the non-mine cells in A {A} A and the sum of numbers in the non-mine cells in B {B} B are the same. (If a map has no non-mine cell, the sum is considered as 0 0 0.)

If multiple solutions exist, print any of them. If no solution exists, print ‘’-1’’ in one line.

输入描述:

The first line contains two integers n , m , ( 1 ≤ n , m ≤ 1000 ) n, m ,(1\le n,m \le 1000) n,m,(1n,m1000), denoting the size of given mine-sweeper maps.

The i {i} i-th line of the following n {n} n lines contains a length- m {m} m string consisting of “.” and “X” denoting the i {i} i-th row of the mine-sweeper map A {A} A. A “.” denotes for a non-mine cell and an “X” denotes for a mine cell.

The i {i} i-th line of the following n {n} n lines contains a length- m {m} m string consisting of “.” and “X” denoting the i {i} i-th row of the mine-sweeper map B {B} B. A “.” denotes for a non-mine cell and an “X” denotes for a mine cell.

输出描述:

If no solution exists, print “-1” in one line.

Otherwise, print n {n} n lines denoting the modified mine-sweeper map B {B} B. The i {i} i-th line should contain a length- m {m} m string consisting of “.” and “X” denoting the i {i} i-th row of the modified map B {B} B. A “.” denotes for a non-mine cell and an “X” denotes for a mine cell.

Please notice that you need not print the numbers on non-mine cells since these numbers can be determined by the output mine-sweeper map.

输入

2 4
X..X
X.X.
X.X.
.X..

输出

X.XX
.X..

说明

We modify one cell in B {B} B. Then the sums of the numbers on non-mine cells in A {A} A and B {B} B both equal 10 10 10.

思路

发现以下两个结论:

  1. 一张扫雷地图的数字之和等于与它 “互补” 的地图的数字之和(“互补” 就是把地雷全变成非地雷,把非地雷全变成地雷)。
  2. 地图 B 在修改不超过 ⌊ n m 2 ⌋ \left\lfloor\frac{nm}{2}\right\rfloor 2nm 个单元格的情况下,可以变成地图 A 或者变成与 A “互补” 的地图。

所以,若 A 与 B 不同单元格的数量小于等于 ⌊ n m 2 ⌋ \left\lfloor\frac{nm}{2}\right\rfloor 2nm 时,将 B 变成 A 即满足题意;否则将 B 变成与 A “互补” 的地图。

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
char a[N][N],b[N][N];
int n,m,ans,flag;

int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>b[i][j];
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
		if(a[i][j]!=b[i][j]) ans++;
	if(ans<=(m*n>>1)) flag=true;
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
		cout<<(char)(flag?a[i][j]:a[i][j]^118)<<(j==m?"\n":"");
}

代码中用到了 'X' ^ 118 = '.''.' ^ 118 = 'X' 这个性质。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值