2017微软秋季校园招聘在线编程笔试-#1402 : MS Recognition

http://hihocoder.com/problemset/problem/1402

题意理解:M和S都很大,并且相互离的很远,M和S的两个端点不一样,M不平衡,所以端点的中点到重心的距离和端点的距离比值》0.5就是M。很奇怪为什么画一条支线看个数会不行,或者是参数设置的不好?

急转弯:端点和重心,距离关系

算法:bfs找端点

数据结构:无

from __future__ import print_function
#
#

'dfs and math'

__author__ = 'hjkruclion'

import sys
import math

def read_int():
    return list(map(int, sys.stdin.readline().split()))
def read_str():
    return sys.stdin.readline().split()[0]

maxn = 500 + 100
dx = [-1, -1, -1, 0, 1, 1, 1, 0]
dy = [-1, 0, 1, 1, 1, 0, -1, -1]

def dcmp(x):
    if math.fabs(x) < 1e-4:
        return 0
    if x > 0:
        return 1
    return -1

a = [['.' for j in range(maxn)] for i in range(maxn)]
vis = [[0 for j in range(maxn)] for i in range(maxn)]
G = []

N, M = read_int()

for i in range(N):
    a[i] = read_str()
    # for j in range(M):
    #     a[i][j] = t[j]
    # if i <= 5:
    #     print(len(a[i]))

# print(a[7][7] == '#')

resM = 0
resS = 0

def calcu(x, y, x1, y1, x2, y2):
    if x1 == x2:
        if dcmp((x1 - x)) == 0:
            return 1
        elif dcmp((x1 - x)) > 0:
            return 1
        else:
            return -1

    else:
        t = (y2 - y1) / (x2 - x1) * (x - x1) + y1 - y
        if dcmp(t) > 0:
            return 1
        elif dcmp(t) == 0:
            return 1
        else:
            return -1

def dist(x1, y1, x2, y2):
    return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))

def bfs(xs, ys, tag):
    global G, vis
    q = [(xs, ys)]
    vis[xs][ys] = tag
    ansx = xs
    ansy = ys
    if tag == 1:
        G.append((xs, ys))
    while(len(q) != 0):
        x, y = q.pop(0)
        for k in range(8):
            nx = x + dx[k]
            ny = y + dy[k]
            if nx >= 0 and nx < N and ny >= 0 and ny < M and vis[nx][ny] != tag and a[nx][ny] == '#':
                q.append((nx, ny))
                vis[nx][ny] = tag
                if tag == 1:
                    G.append((nx, ny))
                ansx = nx
                ansy = ny
    return ansx, ansy

for i in range(N):
    for j in range(M):
        if vis[i][j] != 0 or a[i][j] == '.':
            continue
        # print('hh')
        G = []
        x1, y1 = bfs(i, j, 1)
        x2, y2 = bfs(x1, y1, 2)
        num = len(G)
        ans = 0
        zx = 0
        zy = 0
        for _ in range(num):
            # ans += calcu(G[_][0], G[_][1], x1, y1, x2, y2)
            zx += G[_][0]
            zy += G[_][1]
        zx /= num
        zy /= num
        dist1 = dist(zx, zy, (x1 + x2) / 2, (y1 + y2) / 2)
        dist2 = dist(x1, y1, x2, y2)
        if dist1 / (dist2 / 2) >= 0.5:
            resM += 1
        else:
            resS += 1
        # if math.fabs(ans) >= num / 2:
        #     resM += 1
        # else:
        #     resS += 1
print(resM, resS)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值