hdu 4414 Finding crosses

hdu 4414 Finding crosses

数据范围这么小,随便暴搜就可以过


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int maxn=55;
char mp[maxn][maxn];
vector<pair<int,int> > g;
bool vis[maxn][maxn];
int dx[]={0,1,0,-1},n;
int dy[]={1,0,-1,0};
int Mx,My;

void dfs(int x,int y)
{
    if(x<Mx) Mx=x,My=y;
    g.push_back(make_pair(x,y));
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(nx<0||nx>=n||ny<0||ny>=n||vis[nx][ny]||mp[nx][ny]!='#') continue;
        dfs(nx,ny);
    }
}
bool judge(int x,int y,int r)
{
    if(!vis[x][y]) return 0;
    int nx,ny;
    for(int i=1;i<=r/2;i++)
    {
        nx=x,ny=y-i;
        if(nx<0||nx>=n||ny<0||ny>=n||!vis[nx][ny]) return 0;
    }
    return 1;
}
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        int ans=0;
        for(int i=0;i<n;i++) scanf("%s",mp[i]);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
          if(!vis[i][j]&&mp[i][j]=='#')
          {
              Mx=100;
              g.clear();
              dfs(i,j);
             // cout<<g.size()<<endl;
              for(int r=3;r+r-1<=g.size();r+=2)
                if(r+r-1==g.size())
                {
                   int x=Mx+r/2,y=My;
                   if(judge(x,y,r)) ans++;
                }
          }
        printf("%d\n",ans);
    }
    return 0;
}



你可能感兴趣的:(hdu 4414 Finding crosses)