0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

リバーシの操作(縦横)

Posted at

きちんとメモ取っていこうという構え。
こういうところで「まあ正解したからいいや」と理解をおざなりにすると後の問題で詰まるのだ。

まずは自己流。

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int h, w, y, x;
        String[][] map;

        Scanner sc = new Scanner(System.in);

        h = sc.nextInt();
        w = sc.nextInt();
        y = sc.nextInt();
        x = sc.nextInt();

        map = new String[h][w];

        for(int i=0; i<h; i++){
            String temp = sc.next();
            for(int j=0; j<w; j++){
                map[i][j] = String.valueOf(temp.charAt(j));
            }
        }
        //_____ここまで初期化______

        map[y][x] = "*";

        //上
        for(int i=y-1; i>=0; i--){
            if(map[i][x].equals("*")){
                for(int j=y-1; j>i; j--){
                    map[j][x] = "*";
                }
                break;
            }
        }
        //下
        for(int i=y+1; i<h; i++){
            if(map[i][x].equals("*")){
                for(int j=y+1; j<i; j++){
                    map[j][x] = "*";
                }
                break;
            }
        }
        //右
        for(int i=x+1; i<w; i++){
            if(map[y][i].equals("*")){
                for(int j=x+1; j<i; j++){
                    map[y][j] = "*";
                }
                break;
            }
        }
        //左
        for(int i=x-1; i>=0; i--){
            if(map[y][i].equals("*")){
                for(int j=x-1; j>i; j--){
                    map[y][j] = "*";
                }
                break;
            }
        }

        //_____ここから表示_______
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                System.out.print(map[i][j]);
            }
            System.out.println("");
        }
    }
}

基準点を見て、上→下→右→左、と処理を行っている。
基準点の「一つとなり」から一個ずつ「*」のマスを探して、行き当たったらそこまでを埋める。
ここはメソッドにした方がよさそうかな?

もーちょっとスマートにできそうなもんだけど、解説(C++)を読み解く元気がないので一旦保留。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?