HDU4414 2012ACM/ICPC杭州赛区网络赛E Finding crosses

题目:HDU4414

这是我成为兼职ACMer以来A的第一道赛题。

思路清晰,代码如下:

#include <iostream>
#include <fstream>
using namespace std;

char matrix[55][55];
int n;

inline bool check(const int& i,const int& j,char drirection)
{
    switch(drirection)
    {
    case 'h':
        if(matrix[i-1][j] != '#' && matrix[i+1][j] != '#') return true;
        return false;
    case 'z':
        if(matrix[i][j-1] != '#' && matrix[i][j+1] != '#') return true;
        return false;
    }
}

bool judge(const int& i,const int& j)
{
    int left = 0,
        right = 0,
        top = 0,
        bottom = 0;
    int t;
    t = j;
    while(t --,t >= 0 && matrix[i][t] == '#') left ++;
    if(!left) return false;
    t = j;
    while(t ++,t < n && matrix[i][t] == '#') right ++;
    if(!right || (left != right)) return false;
    t = i;
    while(t --,t >= 0 && matrix[t][j] == '#') top ++;
    if(!top || top != right) return false;
    t = i;
    while(t ++,t < n && matrix[t][j] == '#') bottom ++;
    if(!bottom || bottom != top) return false;

    //横向
    for(int k = j - left; k <= j + right; k ++)
    {
        if(k == j) continue;
        if(!check(i,k,'h')) return false;
    }
    //纵向
    for(int k = i - top ; k <= i + bottom; k ++)
    {
        if(k == i) continue;
        if(!check(k,j,'z'))    return false;
    }
    return true;
}

int main()
{
    int ans;
    while(cin>>n && n>= 3 && n<= 50)
    {        
        ans = 0;
        for(int i = 0; i < n; i ++)
        {
            scanf("%s",matrix[i]);
        }
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < n ;j ++)
                if(matrix[i][j] == '#' && judge(i,j)) ans ++;
        printf("%d\n",ans);
    }
}
posted @ 2012-09-28 00:22  iFinVer  阅读(378)  评论(0编辑  收藏  举报