Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created February 5, 2011 02:21
Show Gist options
  • Save Surgo/812139 to your computer and use it in GitHub Desktop.
Save Surgo/812139 to your computer and use it in GitHub Desktop.
SRM496 - div2 - level two
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""SRM496 - div2 - level two
Manao has a bitmap H pixels high and W pixels wide.
Initially, each of the pixels is white.
Manao draws several (possibly zero) horizontal and/or
vertical strokes. A stroke is a line segment 1 pixel
thick and 1 or more pixels long. Manao only draws horizontal
strokes with red color and vertical strokes with blue.
He can paint a one pixel long stroke with either red or
blue color, and the stroke will be considered horizontal
if red and vertical if blue. Manao never draws two
horizontal or two vertical strokes that overlap.
If a horizontal stroke and a vertical stroke cross,
the pixel at their intersection becomes green.
You're given a String[] picture denoting the bitmap after
Manao's drawing experiments. The x-th character of
the y-th element of picture describes the color of
the pixel at coordinates (x, y) of the bitmap,
where (0, 0) is the pixel at the top left corner
and (W-1, H-1) is the pixel at the bottom right corner.
'R' is red, 'G' is green, 'B' is blue and '.' is white.
Return the least possible number of strokes needed to
obtain the given picture.
Definition:
Class: ColoredStrokes
Method: getLeast
Parameters: String[]
Returns: int
Method signature: int getLeast(String[] picture)
"""
class ColoredStrokes(object):
"""Anagram Free
Constraints:
- picture will contain between 1 and 50 elements,
inclusive.
- Each element of picture will be between 1 and 50
characters long, inclusive.
- All elements of picture will have equal length.
- Each character in picture will be 'R', 'G', 'B' or '.'.
>>> strokes = ColoredStrokes()
>>> print strokes.getLeast(["...", "...", ])
0
>>> print strokes.getLeast(["..B.", "..B.", ])
1
>>> print strokes.getLeast([".BB.", ])
2
>>> print strokes.getLeast(["...B..", ".BRGRR", ".B.B..", ])
3
>>> print strokes.getLeast(["...B..", ".BRBRR", ".B.B..", ])
4
>>> print strokes.getLeast(["GR", "BG", ])
4
"""
def __init__(self):
pass
def getLeast(self, picture):
res = 0
for y in range(len(picture)):
for x in range(len(picture[0])):
# Check red
if picture[y][x] in ['R', 'G', ] and \
(x == 0 or picture[y][x - 1] in ['B', '.', ]):
res += 1
# Check blue
if picture[y][x] in ['B', 'G', ] and \
(y == 0 or picture[y - 1][x] in ['R', '.', ]):
res += 1
return res
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment