人材獲得作戦・4を解く

ひまだったので、
人材獲得作戦・4 試験問題ほか: 人生を書き換える者すらいた。

この問題を解いてみた。

26*13の大きさ固定になってしまった 面倒くさいから直す気は今のところない。
ていうか解けているのだろうかこれ。


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class a {

  public static int[] SearchStart(char[][] mazeX, int[][] mazeCount) {
    int[] ansXY;
    for (int i = 0; i < 13; i++) {
      for (int j = 0; j < 26; j++) {
        if (mazeX[i][j] == 'S') {
          mazeCount[i][j] = 0;
          return ansXY = new int[] { i, j };
        }
      }
    }
    return ansXY = new int[] { -1, -1 };
  }

  public static int[] SearchGoal(char[][] mazeX) {
    int[] ansXY;
    for (int i = 0; i < 13; i++) {
      for (int j = 0; j < 26; j++) {
        if (mazeX[i][j] == 'G') {
          return ansXY = new int[] { i, j };
        }
      }
    }
    return ansXY = new int[] { -1, -1 };
  }

  public static void check(char[][] mazeX, int[][] mazeCount, int X, int Y, int Z) {
    if (mazeX[X][Y] != '*') {
      if (mazeCount[X][Y] > Z) {
        mazeCount[X][Y] = Z;
        if (mazeX[X][Y] != 'G') {
          check(mazeX, mazeCount, X + 1, Y, Z + 1);
          check(mazeX, mazeCount, X - 1, Y, Z + 1);
          check(mazeX, mazeCount, X, Y + 1, Z + 1);
          check(mazeX, mazeCount, X, Y - 1, Z + 1);
        }
      }
    }
  }

  public static void searchReturn(char[][] mazeX, int[][] mazeCount, int X, int Y) {

    if (mazeX[X][Y] == 'G' || mazeX[X][Y] == ' ') {
      if (mazeX[X][Y] == ' ') {
        mazeX[X][Y] = '$';
      }

      int[] Count = new int[] { mazeCount[X + 1][Y], mazeCount[X - 1][Y], mazeCount[X][Y + 1],
          mazeCount[X][Y - 1] };
      int min = Count[0];

      int a = 0;

      for (int i = 1; i < 4; i++) {
        if (min > Count[i]) {
          min = Count[i];
          a = i;
        }
      }

      switch (a) {
      case 0:
        searchReturn(mazeX, mazeCount, X + 1, Y);
        break;
      case 1:
        searchReturn(mazeX, mazeCount, X - 1, Y);
        break;
      case 2:
        searchReturn(mazeX, mazeCount, X, Y + 1);
        break;
      case 3:
        searchReturn(mazeX, mazeCount, X, Y - 1);
        break;
      }
    }
  }

  public static void main(String[] args) throws IOException {
    Scanner stdIn = new Scanner(System.in);
    File fileQ = new File("問題ファイル");
    FileReader filereader = new FileReader(fileQ);
    BufferedReader br = new BufferedReader(filereader);

    String[] maze = new String[13];

    for (int i = 0; i < maze.length; i++) {
      maze[i] = br.readLine();
    }

    char[][] mazeX = new char[13][26];
    int[][] mazeCount = new int[13][26];

    for (int i = 0; i < 13; i++) {
      for (int j = 0; j < 26; j++) {
        mazeX[i][j] = maze[i].charAt(j);
        mazeCount[i][j] = 99;
      }
    }

    int[] StartXY = SearchStart(mazeX, mazeCount);

    check(mazeX, mazeCount, StartXY[0] + 1, StartXY[1], 0);
    check(mazeX, mazeCount, StartXY[0] - 1, StartXY[1], 0);
    check(mazeX, mazeCount, StartXY[0], StartXY[1] + 1, 0);
    check(mazeX, mazeCount, StartXY[0], StartXY[1] - 1, 0);

    int[] GoalXY = SearchGoal(mazeX);

    searchReturn(mazeX, mazeCount, GoalXY[0], GoalXY[1]);

    for (int i = 0; i < 13; i++) {
      for (int j = 0; j < 26; j++) {
        System.out.print(mazeX[i][j]);

      }
      System.out.println();
    }

    File fileA = new File("回答ファイル");
    FileWriter filewriter = new FileWriter(fileA);
    for (int i = 0; i < 13; i++) {
      filewriter.write(mazeX[i]);
      filewriter.write("\r\n");
    }
    filewriter.close();

  }

}