[思维]Mine Sweeper II 第45届icpc区域赛上海站B

59 篇文章 0 订阅

A mine-sweeper map XX can be expressed as an n\times mn×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 88 cells around it.) The following is a 16\times 3016×30 mine-sweeper map where a flagged cell denotes a mine cell and a blank cell denotes a non-mine cell with number 0.

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

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

Input

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

The ii-th line of the following nn lines contains a length-mm string consisting of "." and "X" denoting the ii-th row of the mine-sweeper map AA. A "." denotes for a non-mine cell and an "X" denotes for a mine cell.

The ii-th line of the following nn lines contains a length-mm string consisting of "." and "X" denoting the ii-th row of the mine-sweeper map BB. A "." denotes for a non-mine cell and an "X" denotes for a mine cell.

Output

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

Otherwise, print nn lines denoting the modified mine-sweeper map BB. The ii-th line should contain a length-mm string consisting of "." and "X" denoting the ii-th row of the modified map BB. 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.

Example

Input

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

Output

X.XX
.X..

Note

We modify one cell in BB. Then the sums of the numbers on non-mine cells in AA and BB both equal 1010.

题意: 有两个n*m的扫雷地图A,B,每次可以对B进行操作,将B上的一个地雷'X'改为空地'.'或者将B上的一个空地'.'改为地雷'X',且进行操作次数不超过n*m/2,求对B进行操作后的新地图,使得B上的空地权值和等于A上的空地权值和。定义空地上的权值为相邻8个格子中地雷个数。

分析: 读错了题,vice versa原来是反之亦然的意思。既然可以对B随便修改,那么直接改成A的话权值和一定和A相同了,这时候考虑一下操作次数,如果A和B不同位置<=n*m/2,那么直接改成A是没有问题的,如果>n*m/2那可以改成A取反以后的地图。因为改成A取反以后的地图与A本身不同的位置一定<=n*m/2,且权值和不变,还是A的权值和,证明可考虑权值和的两种求法,一种求法是每个空地八方向上地雷的个数,对于每个空地求和,第二种求法是每个地雷八方向上空地的个数,对每个地雷处求和,因此对于A取反以后显然权值和是不变的。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

char mpA[1005][1005], mpB[1005][1005];

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m, cnt = 0;
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) 
			cin >> mpA[i][j];
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) 
		{
			cin >> mpB[i][j];
			if(mpB[i][j] != mpA[i][j])
				cnt++;
		}
	if(cnt <= n*m/2)
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= m; j++) 
				cout << mpA[i][j];
			cout << endl;
		}
	else
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= m; j++) 
				if(mpA[i][j] == 'X')
					cout << '.';
				else
					cout << 'X';
			cout << endl;
		}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值