本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
【ICPC】第 45 届ICPC区域赛(上海)B - Mine Sweeper II
题目连接
题目
A mine-sweeper map can be expressed as an 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 cells around it.) The following is a 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 of size , you should modify at most (i.e. the largest nonnegative integer that is less than or equal to nm2\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 and the sum of numbers in the non-mine cells in are the same. (If a map has no non-mine cell, the sum is considered as 0.)
If multiple solutions exist, print any of them. If no solution exists, print ''-1'' in one line.
题目大意
在一张 的地图上,有若干个位置有地雷 X,其他位置上是空地 .。
有地雷的位置的权值为 0,没有地雷的位置的权值为其八连通位置上的地雷的数量。一张地图的权值为其中所有格子的权值之和。
给两张地图 和 ,问能否改变 中不超过 个位置(地雷变成空地或者空地变成地雷),使得地图 和地图 的权值相等。有解输出修改后的 图,无解输出"-1".
思路
把每个地雷与其八连通的空地连边,容易发现地图的权值就是边的条数。
即如果我们把所有的地雷都变成空地,把所有的空地都变成地雷,不会影响地图的权值。
发现上述性质之后,我们只需要判断把地图 变成地图 的操作次数是否满足要求,如果满足要求,输出 图,否则输出 的反图即可。
代码
#include <bits/stdc++.h>
using namespace std;
int n,m,s;
char a[1005][1005],b;
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j <=m;j++)
scanf(" %c",&a[i][j]);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf(" %c",&b);
if (b!=a[i][j]) s++;
}
if (s<=n*m/2)
for (int i=1;i<=n;i++,printf("\n"))
for (int j=1;j <=m;j++)
printf("%c",a[i][j]);
else
for (int i=1;i<=n;i++,printf("\n"))
{
for (int j=1;j<=m;j++)
if (a[i][j]=='.')printf("X");
else printf(".");
}
return 0;
}