Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告

  Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告。单击这里打开题目链接(国内访问需要那个,你懂的)。

  原题如下:

Square Detector

Problem Description

You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid.

You are given a grid of N×N square cells. Each cell is either white or black. Your task is to detect whether all the black cells form a square shape.

Input

The first line of the input consists of a single number T, the number of test cases.

Each test case starts with a line containing a single integer N. Each of the subsequent N lines contain N characters. Each character is either "." symbolizing a white cell, or "#" symbolizing a black cell. Every test case contains at least one black cell.

Output

For each test case i numbered from 1 to T, output "Case #i: ", followed by YES or NO depending on whether or not all the black cells form a completely filled square with edges parallel to the grid of cells.

Constraints

1 ≤ T ≤ 20
1 ≤ N ≤ 20

Example

Test cases 1 and 5 represent valid squares. Case 2 has an extra cell that is outside of the square. Case 3 shows a square not filled inside. And case 4 is a rectangle but not a square.

Sample Input

5
4
..##
..##
....
....
4
..##
..##
#...
....
4
####
#..#
#..#
####
5
#####
#####
#####
#####
.....
5
#####
#####
#####
#####
#####

Sample Output

Case #1: YES
Case #2: NO
Case #3: NO
Case #4: NO
Case #5: YES

 

 

 

 

  首先,置正方形状态为true,然后横向扫描所有行,判断从“.”到“#”的切换次数。切换次数大于一次的,置正方形状态为false,退出循环。如果每行的“#”起始坐标或结束坐标不一致,也置正方形状态为false,退出循环。接着,如果正方形状态为true,再纵向扫描所有列,与行扫描一样的做法。最终,如果正方形状态为true,输出YES;否则,输出NO。

  C++语言代码如下:

#include <cstdio>
#include <cstdlib>

#define MAX_LENGTH 100

using namespace std;

int main()
{
    bool square;
    int line_changes;
    int start_x, start_y, end_x, end_y;
    int T;
    static char s[MAX_LENGTH][MAX_LENGTH];
    scanf("%d", &T);
    for ( int i = 1; i <= T; ++ i )
    {
        int n;
        scanf("%d", &n);
        gets(s[0]);
        start_x = start_y = -1;
        for ( int j = 0; j < n; ++ j )
            gets(s[j]);
        square = true;
        for ( int j = 0; j < n; ++ j )
        {
            line_changes = 0;
            for ( int k = 0; k < n; ++ k )
            {
                if ( k == 0 && s[j][k] == '#' )
                    ++line_changes;
                if ( k > 0 && s[j][k] == '#' && s[j][k-1] == '.' )
                    ++line_changes;
                if ( line_changes > 1 )
                {
                    square = false;
                    break;
                }
                if ( s[j][k] == '#' )
                {
                    end_x = j;
                    end_y = k;
                    if ( start_x == -1 )
                    {
                        start_x = j;
                        start_y = k;
                    }
                }
            }
        }
        if ( end_y - start_y != end_x - start_x )
            square = false;
        for ( int col = 0; col < n; ++ col )
        {
            line_changes = 0;
            for ( int row = 0; row < n; ++ row )
            {
                if ( row == 0 && s[row][col] == '#' )
                    ++line_changes;
                if ( row > 0 && s[row][col] == '#' && s[row-1][col] == '.' )
                    ++line_changes;
                if ( line_changes > 1 )
                {
                    square = false;
                    break;
                }
            }
        }
        if ( square )
            printf("Case #%i: YES\n", i);
        else
            printf("Case #%i: NO\n", i);
    }
    return EXIT_SUCCESS;
}

 

 

  另外,Facebook Hacker Cup还公布了官方的解法。官方解法是用Python语言写的(用的是 Python 2 的语法),代码如下:

def solve():
    n = int(raw_input().strip())
    b = [raw_input().strip() for i in range(n)]
    black = 0
    minX, maxX, minY, maxY = n, 0, n, 0
    for x in range(0, n) :
        for y in range(0, n) :
            if b[x][y] == '#':
                minX, minY  = min(minX, x), min(minY, y)
                maxX, maxY = max(maxX, x), max(maxY, y)
                black += 1
    dx = maxX - minX + 1
    dy = maxY - minY + 1
    return dx == dy and dx * dy == black

if __name__ == '__main__':
    t = int(raw_input().strip())
    for i in range(1, t+1):
        res = solve()
        print 'Case #%d: %s' % (i, 'YES' if res else 'NO')
posted @ 2013-11-26 03:32  叶剑飞Victor  阅读(1243)  评论(0编辑  收藏  举报