B. One Bomb
Examples
input
3 4
.*..
....
.*..
output
YES
1 2
input
3 3
..*
.*.
*..
output
NO
input
6 5
..*..
..*..
*****
..*..
..*..
..*..
output
YES
3 3
题目大意:
在坐标上放一个炸弹,使这一行和这一列的墙都炸掉,判断是否有这么一个炸弹可以刚好炸完地图上全部的墙。如果有则输出YES和坐标,否则输出NO。
思路:
刚开始看到感觉可以用dfs,实际上不用这么麻烦,借助dfs的思想,枚举每一个格子,求这行和这列的墙数之和,判断是否是全部墙的数量,如果直接这样模拟会TLE,所以再借助前缀和的思想,在输入的同时计算每行每列的和,求和时用到相容排斥定理。
当地图上没有炸弹的时候,随便输出一个坐标就行了。
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#define int long long
#define endl '\n'
using namespace std;
void fastio(){
ios::sync_with_stdio(0),cin.tie(0);cout.tie(0);}
const int N=1007;
int a[N][N];
int n,m;
int sum=0;
bool check(int i,int j)
{
if(sum==0)return 1;//特判
int num=a[i][0]+a[0][j]-a[i][j];//容斥
return num==sum;
}
signed main()
{
fastio();
cin>>n>>m;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
char b;
cin>>b;
if(b=='*')
{
a[i][0]+=1;
a[0][j]+=1;
a[i][j]=1;
++sum;
}
else a[i][j]=0;
}
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
if(check(i,j))
{
cout<<"YES"<<endl;
cout<<i<<' '<<j<<endl;
return 0;
}
}
}
cout<<"NO"<<endl;
return 0;
}