【CF 1049B】Yet Another Crosses Problem
(以下代码时间超限了,我还不怎么会估算时间复杂度~~~~~~~)
(下面代码的思路是:遍历整个二维数组,依次枚举每个点,计数每个点上下左右的 . 的数量)。
如代码里的一段注释:memset函数,最后一个参数只能写成sizeof()的形式,不能写成整数值,否则置入数值时会出错。
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
const int maxn=5*(1e4);
string ch[maxn];
int s[4],Min=1e5;
int n,m;
int up(int x,int y)
{
while(x>=0)
{
if(ch[x][y]=='.') s[0]++;
x--;
}
return s[0];
}
int down(int x,int y)
{
while(x<n)
{
if(ch[x][y]=='.') s[1]++;
x++;
}
return s[1];
}
int right(int x,int y)
{
while(y<m)
{
if(ch[x][y]=='.') s[2]++;
y++;
}
return s[2];
}
int left(int x,int y)
{
while(y>=0)
{
if(ch[x][y]=='.') s[3]++;
y--;
}
return s[3];
}
int main()
{
int q;
cin>>q;
while(q--)
{
Min=1e5;
int i=0;
cin>>n>>m;
while(i<n)
{
cin>>ch[i];
i++;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
//memset(s,0,4); 首先,这里最后的size不能用数字,应该写成sizeof的函数。
memset(s,0,sizeof(s));
if(Min>up(i,j)+down(i+1,j)+
left(i,j-1)+right(i,j+1))
//up(i-1,j)+down(i=1,j)+left(i,j-1)+right(i,j+1)
Min=s[0]+s[1]+s[2]+s[3];
if(Min==0) break;
}
cout<<Min<<endl;
}
return 0;
}