ABC075のB問題をC#で解く
AtCorder、おかげさまで毎日楽しくやっている。
やっと茶色になれた!
目指せ今年中に緑色!!
さて、今回はABC075のB問題。
入力が典型だと思うけど今だにどうやって読み込もうかと迷ってしまう。
次回以降は絶対に迷いたくない!なのでこれでやる。
1 | List<char[]> graph = new List<char[]>(); |
これList<string>でやると
graph[i][j]=’a’のように上書きできない。
途中でインデックス指定で上書きできるのは何気に使い勝手が良い。
回答したコードは以下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using Console = System.Console; namespace Atcoder { class Program { static void Main( string [] args) { var HW = ReadIntArray(); int [] difX = new int [] { 0, 1, 1, 1, 0, -1, -1, -1 }; int [] difY = new int [] { -1, -1, 0, 1, 1, 1, 0, -1 }; List< char []> graph = new List< char []>(); for ( int i = 0; i < HW[0]; i++) { graph.Add(Read().ToCharArray()); } for ( int height = 0; height < HW[0]; height++) { for ( int width = 0; width < HW[1]; width++) { if (graph[height][width] == '.' ) { int countBom = 0; for ( int i = 0; i < difX.Length; i++) { if (width + difX[i] < 0 || width + difX[i] >= HW[1] || height + difY[i] < 0 || height + difY[i] >= HW[0]) { continue ; } if (graph[height + difY[i]][width + difX[i]] == '#' ) { countBom++; } } graph[height][width] = countBom.ToString().ToCharArray()[0]; } } } foreach ( var row in graph) { Console.WriteLine( new string (row)); } } private static readonly Func< string > Read = () => Console.ReadLine(); private static readonly Func< string []> ReadStringArray = () => Console.ReadLine().Split(); private static readonly Func< int > ReadInt = () => int .Parse(Console.ReadLine()); private static readonly Func< long > ReadLong = () => long .Parse(Console.ReadLine()); private static readonly Func< int []> ReadIntArray = () => Console.ReadLine().Split().Select( int .Parse).ToArray(); private static readonly Func< long []> ReadLongArray = () => Console.ReadLine().Split().Select( long .Parse).ToArray(); private static readonly Action< string > Cw = str => Console.WriteLine(str); } } |
ディスカッション
コメント一覧
まだ、コメントがありません