插火把(洛谷)

2023-12-22 16:51:24

题目描述

话说有一天 linyorson 在“我的世界”开了一个 n × n n \times n n×n 的方阵,现在他有 m m m 个火把和 k k k 个萤石,分别放在 ( x 1 , y 1 ) ~ ( x m , y m ) (x_1, y_1) \sim (x_m, y_m) (x1?,y1?)(xm?,ym?) ( o 1 , p 1 ) ~ ( o k , p k ) (o_1, p_1) \sim (o_k, p_k) (o1?,p1?)(ok?,pk?) 的位置,没有光并且没放东西的地方会生成怪物。请问在这个方阵中有几个点会生成怪物?

P.S. 火把的照亮范围是:

    |暗|暗| 光 |暗|暗|
    |暗|光| 光 |光|暗|
    |光|光|火把|光|光|
    |暗|光| 光 |光|暗|
    |暗|暗| 光 |暗|暗|

萤石:

    |光|光| 光 |光|光|
    |光|光| 光 |光|光|
    |光|光|萤石|光|光|
    |光|光| 光 |光|光|
    |光|光| 光 |光|光|

输入格式

输入共 m + k + 1 m + k + 1 m+k+1 行。
第一行为 n , m , k n, m, k n,m,k
2 2 2 到第 m + 1 m + 1 m+1 行分别是火把的位置 x i , y i x_i, y_i xi?,yi?
m + 2 m + 2 m+2 到第 m + k + 1 m + k + 1 m+k+1 行分别是萤石的位置 o i , p i o_i, p_i oi?,pi?

注:可能没有萤石,但一定有火把。

输出格式

有几个点会生出怪物。

样例 #1

样例输入 #1

5 1 0
3 3

样例输出 #1

12

提示

数据保证, 1 ≤ n ≤ 100 1 \le n \le 100 1n100 1 ≤ m + k ≤ 25 1 \leq m+k \leq 25 1m+k25 1 ≤ m ≤ 25 1 \leq m \leq 25 1m25 0 ≤ k ≤ 5 0 \leq k \leq 5 0k5

n,m,k=map(int,input().split())
mapp=[[0]*(n+1) for _ in range(n+1)]
for item in range(m):
    x,y=map(int,input().split())
    mapp[x][y]=2
    if x-1>=1:
        mapp[x-1][y]=1
        if y-1>=1:
            mapp[x-1][y-1]=1
            pass
        pass
    if x+1<=n:
        mapp[x+1][y]=1
        if y+1<=n:
            mapp[x+1][y+1]=1
            pass
        pass
    if x-2>=1:
        mapp[x-2][y]=1
        pass
    if x+2<=n:
        mapp[x+2][y]=1
        pass
    if y-1>=1:
        mapp[x][y-1]=1
        if x+1<=n:
            mapp[x+1][y-1]=1
            pass
        pass
    if y+1<=n:
        mapp[x][y+1]=1
        if x-1>=1:
            mapp[x-1][y+1]=1
            pass
        pass
    if x-2>=1:
        mapp[x][y-2]=1
        pass
    if x+2<=n:
        mapp[x][y+2]=1
        pass
    pass
for item in range(k):
    x, y = map(int, input().split())
    mapp[x][y]=3
    if x+1<=n:
        mapp[x+1][y]=1
        if y+1<=n:
            mapp[x+1][y+1]=1
            pass
        if y-1>=1:
            mapp[x+1][y-1]=1
            pass
        if y+2<=n:
            mapp[x+1][y+2]=1
            pass
        if y-2>=1:
            mapp[x+1][y-2]=1
        pass
    if x-1>=1:
        mapp[x-1][y]=1
        if y+1<=n:
            mapp[x-1][y+1]=1
            pass
        if y-1>=1:
            mapp[x-1][y-1]=1
            pass
        if y+2<=n:
            mapp[x-1][y+2]=1
            pass
        if y-2>=1:
            mapp[x-1][y-2]=1
        pass
    if y+1<=n:
        mapp[x][y+1]=1
        if x+1<=n:
            mapp[x+1][y+1]=1
            pass
        if x-1>=1:
            mapp[x-1][y+1]=1
            pass
        if x+2<=n:
            mapp[x+2][y+1]=1
            pass
        if x-2>=1:
            mapp[x-2][y+1]=1
        pass
    if y-1>=0:
        mapp[x][y-1]=1
        if x+1<=n:
            mapp[x+1][y-1]=1
            pass
        if x-1>=1:
            mapp[x-1][y-1]=1
            pass
        if x+2<=n:
            mapp[x+2][y-1]=1
            pass
        if x-2>=1:
            mapp[x-2][y-1]=1
        pass
    if x+2<=n:
        mapp[x+2][y]=1
        if y+2<=n:
            mapp[x+2][y+2]=1
            pass
        if y-2>=1:
            mapp[x+2][y-2]=1
            pass
        pass
    if x-2>=1:
        mapp[x-2][y]=1
        if y+2<=n:
            mapp[x-2][y+2]=1
            pass
        if y-2>=1:
            mapp[x-2][y-2]=1
            pass
        pass
    if y+2<=n:
        mapp[x][y+2]=1
        pass
    if y-2>=1:
        mapp[x][y-2]=1
        pass
    pass
anss=0
for item in range(1,n+1):
    for jtem in range(1,n+1):
        if mapp[item][jtem]==0:
            anss+=1
            pass
        pass
    pass
print(anss)

一眼放去全是if,不难的题,注意边界超出以及题目中数组起始值为1就行,再就是有耐心把代码写完,这些判断应该还可以优化很多,但是不太爱动脑子,快点把入门刷完

文章来源:https://blog.csdn.net/KLSZM/article/details/135154580
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。