0
0

More than 3 years have passed since last update.

裏返せる可能性(斜め) (paizaランク C 相当)

Last updated at Posted at 2021-05-18

 最初は力業でこんなことをやった。

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++){
            for(int j=0; j<w; j++){
                if(i == y && j == x){
                    map[i][j] = "!";
                } else {
                    map[i][j] = ".";
                }
            }
        }
        //_____ここまで初期化______

        //右上
        int tx = x+1;
        for(int i=y-1; i>=0; i--){
            if(tx < w){
                map[i][tx] = "*";
                tx++;
            } else {
                break;
            }
        }

        //右下
        tx = x+1;
        for(int i=y+1; i<h; i++){
            if(tx < w){
                map[i][tx] = "*";
                tx++;
            } else {
                break;
            }
        }

        //左下
        tx = x-1;
        for(int i=y+1; i<h; i++){
            if(tx >= 0){
                map[i][tx] = "*";
                tx--;
            } else {
                break;
            }
        }

        //左上
        tx = x-1;
        for(int i=y-1; i>=0; i--){
            if(tx >= 0){
                map[i][tx] = "*";
                tx--;
            }
        }

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

「うんまあそうなんだけど、もっとこう、すっきりとだね」
 自分でもんやりしながら解説を見て、「あーそっか」と納得して書き直した。

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++){
            for(int j=0; j<w; j++){
                if(i == y && j == x){
                    map[i][j] = "!";
                } else if(y-i == x-j || y-i == -x+j) {   //これで済む話だった
                    map[i][j] = "*";
                } else {
                    map[i][j] = ".";
                }
            }
        }
        //_____ここまで初期化______

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

「指定した座標の斜め一直線」ということは、

y = x \\
y = -x

 この二式でいいわけだ。そこから入力されたY座標とX座標分ズレさせればいいんだから

y-i = x-j \\
y-i = -(x-j)

 これでよい、と。

 中学生レベルのことでも大事なところで思い出せるかどうかは別問題……と。
 ひとまずこの方針でこの問題は詰められそうだ。

0
0
0

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