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 = ; i <= T; ++ i )
{
int n;
scanf("%d", &n);
gets(s[]);
start_x = start_y = -;
for ( int j = ; j < n; ++ j )
gets(s[j]);
square = true;
for ( int j = ; j < n; ++ j )
{
line_changes = ;
for ( int k = ; k < n; ++ k )
{
if ( k == && s[j][k] == '#' )
++line_changes;
if ( k > && s[j][k] == '#' && s[j][k-] == '.' )
++line_changes;
if ( line_changes > )
{
square = false;
break;
}
if ( s[j][k] == '#' )
{
end_x = j;
end_y = k;
if ( start_x == - )
{
start_x = j;
start_y = k;
}
}
}
}
if ( end_y - start_y != end_x - start_x )
square = false;
for ( int col = ; col < n; ++ col )
{
line_changes = ;
for ( int row = ; row < n; ++ row )
{
if ( row == && s[row][col] == '#' )
++line_changes;
if ( row > && s[row][col] == '#' && s[row-][col] == '.' )
++line_changes;
if ( line_changes > )
{
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')

Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告的更多相关文章

  1. Facebook Hacker Cup 2014 Qualification Round

    2014 Qualification Round Solutions 2013年11月25日下午 1:34 ...最简单的一题又有bug...自以为是真是很厉害! 1. Square Detector ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. 51Nod 1182 完美字符串(字符串处理 贪心 Facebook Hacker Cup选拔)

    1182 完美字符串             题目来源:                         Facebook Hacker Cup选拔         基准时间限制:1 秒 空间限制:1 ...

  4. Facebook Hacker Cup 2015 Round 1--Corporate Gifting(树动态规划)

    原标题:https://www.facebook.com/hackercup/problems.php?pid=759650454070547&round=344496159068801 题意 ...

  5. Facebook Hacker Cup 2015 Round 1--Winning at Sports(动态规划)

    原题:pid=688426044611322&round=344496159068801">https://www.facebook.com/hackercup/problem ...

  6. Facebook Hacker Cup 2015 Round 1--Homework(筛选法求素数)

    题意:给定A,B,K(A<=B)三个数,问在[A,B]范围内的数素数因子个数为K的个数. 题解:典型的筛选法求素数.首先建立一个保存素数因子个数的数组factorNum[],以及到n为止含有素数 ...

  7. VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

    C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  8. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  9. VK Cup 2012 Qualification Round 1---C. Cd and pwd commands

    Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. hdu 5861 Road 两棵线段树

    传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...

  2. MySQL数据库学习笔记(八)----JDBC入门及简单增删改数据库的操作

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. box unboxing(装箱 拆箱) C#编程指南

    box(装箱)消耗大 box在堆栈中创建一个新的对象,性能消耗大 int i = 123; // Boxing copies the value of i into object o. object ...

  4. 解决Gradle DSL method not found: ‘android()’

    最近导入as的项目出了这样的问题 这个问题困扰了我很长时间,好吧,搜了半天全都是runProguard的,最后在stackoverflow上搜到解决办法了: http://stackoverflow. ...

  5. C和指针笔记 3.7 存储类型

    变量的破碎类型是指存储变量值的内存类型.变量的存储类型决定变量何时创建.何时销毁以及它的值将保持多久. 有三个地方可以用于存在变量:普通内存.运行时堆栈.硬件寄存器. 变量的缺省存储类型取决于它的声明 ...

  6. 【.NET进阶】函数调用--函数栈

    原文:http://www.cnblogs.com/rain-lei/p/3622057.html 函数调用大家都不陌生,调用者向被调用者传递一些参数,然后执行被调用者的代码,最后被调用者向调用者返回 ...

  7. SDRAM 学习(三)之command

    command 模块总述 SDRAM 的 command 模块的内容包括如下: 1.对初始化请求.配置模式寄存器.读/写.刷新.预充电等命令的一个优先级的控制. 2.对命令执行时间进行控制,依据如图1 ...

  8. RequiredFieldValidator 根据group组来触发验证

    今天在开发过程中遇到了这样一个问题 在这个用户添加界面中,我使用了多个验证控件RequiredFieldValidator,分别控制用户名密码.在默认情况下,当单击“检查用户名”时,密码的验证控件也被 ...

  9. matlab取消和添加注释以及一些快捷键

    1 matlab中关于注释: 多行注释: 选中要注释的若干语句,工具栏菜单Text->Comment,或者鼠标右击选"Comment",或者快捷键Ctrl+R 取消注释: 选 ...

  10. [CareerCup] 14.5 Object Reflection 对象反射

    14.5 Explain what object reflection is in Java and why it is useful. Java中的对象反射机制可以获得Java类和对象的反射信息,并 ...