You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.
You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the depot field.
The next n lines contain m symbols "." and "*" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall.
If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.
3 4
.*..
....
.*..
YES
1 2
3 3
..*
.*.
*..
NO
6 5
..*..
..*..
*****
..*..
..*..
..*..
YES
3 3
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
char a[1003][1003];
int y[1003],x[1003]; //记录每行,每列中'*'的个数;
//然后枚举每个点,计算它所在行列的*数是否和总*数相同
int main()
{
int n,m,sum=0,ct=0,flag=0,py,px;
cin>>n>>m;
for(int i=1; i<=n; i++)
{
scanf("%s",a[i]+1);
for(int j=1; j<=m; j++)
if(a[i][j]=='*')
sum++;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='*')
y[i]++;
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(a[j][i]=='*')
x[i]++;
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
ct=y[i]+x[j];
if(a[i][j]=='*')
ct--;
if(ct==sum)
{
py=i;
px=j;
flag=1;
break;
}
}
if(flag==1) break;
}
if(flag)
printf("YES\n%d %d\n",py,px);
else
printf("NO\n");
return 0;
}
本文介绍了一个关于在给定矩阵中放置炸弹以摧毁所有墙壁的问题。通过遍历矩阵并计算每行每列的墙壁数量,判断是否存在一个位置可以一次性摧毁所有墙壁。文章提供了完整的算法思路及C++实现代码。
203

被折叠的 条评论
为什么被折叠?



