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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;

namespace どう書く_org文字列の八方向検索 {
    class Program {
        static string search;
        static List<string> list;
        static void Main(string[] args) {
            string sample = 
@"リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ";
            search = "ウオリ";
            char[] sp = new char[] { '\n' };
            int width = sample.Split(sp)[0].Length;
            int height = sample.Split(sp).Length;
            list = new List<string>(sample.Split(sp));
            for(int y = 0; y < list.Count; y++) {
                for(int x = 0; x < list[y].Length; x++) {
                    if(list[y][x] == search[0]) {
                        if(E(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "右");
                        }
                        if(W(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "左");
                        }
                        if(S(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "下");
                        }
                        if(N(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "上");
                        }
                        if(NE(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "右上");
                        }
                        if(SE(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "右下");
                        }
                        if(NW(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "左上");
                        }
                        if(SW(x, y) == search) {
                            Console.WriteLine("(" + x + "," + y + ")" + "," + "左下");
                        }
                    }
                }
            }
            Console.ReadLine();
        }
        static string E(int x, int y) {
            try {
                return list[y].Substring(x, search.Length);
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string W(int x, int y) {
            try {
                return Strings.StrReverse(E(x - search.Length + 1, y));
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string S(int x, int y) {
            try {
                StringBuilder strb = new StringBuilder();
                for(int i = 0; i < search.Length; i++) {
                    strb.Append(list[y + i][x]);
                }
                return strb.ToString();
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string N(int x, int y) {
            try {
                return Strings.StrReverse(S(x, y - search.Length + 1));
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string SE(int x, int y) {
            try {
                StringBuilder strb = new StringBuilder();
                for(int i = 0; i < search.Length; i++) {
                    strb.Append(list[y + i][x + i]);
                }
                return strb.ToString();
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string NW(int x, int y) {
            try {
                return Strings.StrReverse(SE(x - search.Length + 1, y - search.Length + 1));
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string NE(int x, int y) {
            try {
                StringBuilder strb = new StringBuilder();
                for(int i = 0; i < search.Length; i++) {
                    strb.Append(list[y - i][x + i]);
                }
                return strb.ToString();
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
        static string SW(int x, int y) {
            try {
                return Strings.StrReverse(NE(x - search.Length + 1, y + search.Length - 1));
            } catch(ArgumentOutOfRangeException) { return ""; } catch(IndexOutOfRangeException) { return ""; }
        }
    }
}