Python练习

Codingame 上打代码冲突

上题

Paint is a magical tool and birthplace to amazing early 2000s’ artists. One functionality, in particular has revolutionized the way to make art: “Fill the color”. This feature allows to change the color of a pixel and all neighboring pixels which share the same color.

Example:

.—.---.—.---.
| R | R | R | R |
:—±–±--±–:
| B | R | R | G |
:—±–±--±–:
| P | P | P | R |
‘—’---’—’---’

Suppose we have a canvas like the one shown above and we want to change the upper row pixels from color ‘R’ to ‘K’. If we click on the pixel (0,0), then it will change the color of the pixel (0,0) and all its vertical and horizontal neighbors to the desired color, like shown below:

.—.---.—.---.
| K | K | K | K |
:—±–±--±–:
| B | K | K | G |
:—±–±--±–:
| P | P | P | R |
‘—’---’—’---’

Your goal is to replicate the “fill the color” feature as shown above. You’ll be given the canvas as a 2D array, the coordinates of the mouse click by the user, and the new color.

Here are the following feature constraints:

  • The colors are represented by integers
  • The user’s mouse click is always on the canvas
  • The spread is only in horizontal, and vertical
  • The y axis is vertical, and the x axis is horizontal
  • The canvas is never empty
  • The canvas is always a rectangle
    Input
    Line 1: the space-separated Y and X integer coordinates of the user’s mouse click.
    Line 2: the new integer color to apply.
    Line 3: H, the height the canvas and W, the width of the canvas.
    Next H lines: A sequence of space-separated integers that represents a row of the image.
    Output
    H lines: The sequences of space-separated integers that represent the rows of the new image.
    Constraints
    1 ≤ H, W ≤ 15
    0 ≤ Y < H
    0 ≤ X < W
    Example
    Input
    1 0
    4
    2 3
    1 2 3
    9 5 6
    Output
    1 2 3
    4 5 6

代码如下

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

way = [[1,0],[0,1],[-1,0],[0,-1]]

queue = []
hh = 0
tt = 0

y_click, x_click = [int(i) for i in input().split()]
new_color = int(input())
height, width = [int(i) for i in input().split()]
mmap = [[0]*width for i in range(height)]
for i in range(height):
    t = 0
    for j in input().split():
        color = int(j)
        mmap[i][t] = int(color)
        t+=1
queue.append([y_click,x_click])
hh+=1
color = mmap[y_click][x_click]
mmap[y_click][x_click] = new_color
while hh!=tt:
    y,x = queue[tt][0],queue[tt][1]
    tt+=1
    for i in range(4):
        if 0 <= y+way[i][0] <height and 0 <= x+way[i][1] < width:
            if mmap[y+way[i][0]][x+way[i][1]] == color:
                mmap[y+way[i][0]][x+way[i][1]] = new_color
                app =[y+way[i][0],x+way[i][1]]
                queue.append(app)
                hh+=1
for i in range(height):
    for j in range(width):
        print(mmap[i][j],end = " ")
    print()
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)

写的有点慢,答题没交上不知道对不对,不过我自己写的几个样例都是没问题的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值