日々精進

新しく学んだことを書き留めていきます

SRM496 Div1 Easy

問題
白紙の画用紙に絵を描きます。青色(B)のペンは縦に塗ることしかできず、赤色(R)のペンは横に塗ることしかできません。
青色のペンと赤色のペンで両方塗ると緑色(G)になります。
与えられた絵を描きたい時に、必要な塗る回数を答えなさい。
ただし、一直線に塗るのは1回とカウントして良いものとし、サイズは50*50以下とします。

方針
R,Gを横に走査していき、B,Gを縦に走査していけばOK.

using System;
using System.Collections.Generic;
using System.Text;

public class ColoredStrokes
{
    public int getLeast(string[] picture)
    {
        int ans = 0;
        for (int y = 0; y < picture.Length; y++)
        {
            char pre = new char();
            for (int x = 0; x < picture[0].Length; x++)
            {
                if(!(pre.Equals('R') || pre.Equals('G')) && (picture[y][x].Equals('R') || picture[y][x].Equals('G')))
                    ans++;
                pre = picture[y][x];
            }
        }
        for (int x = 0; x < picture[0].Length; x++)
        {
            char pre = new char();
            for (int y = 0; y < picture.Length; y++)
            {
                if (!(pre.Equals('B') || pre.Equals('G')) && (picture[y][x].Equals('B') || picture[y][x].Equals('G')))
                    ans++;
                pre = picture[y][x];
            }
        }
        return ans;
    }
}