LoginSignup
0
0

More than 1 year has passed since last update.

陣取りのターン数 Java編(解決)【paiza Aランクレベルアップメニュー】

Posted at

 解決しました。

Main.java
import java.util.*;
import java.util.Queue;
import java.util.ArrayDeque;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int h = sc.nextInt();
        int w = sc.nextInt();
        int n = sc.nextInt();
        String map[][] = new String[h][w];  //陣

        Queue<XY> q = new ArrayDeque<>();   //キュー

        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));
                if(map[i][j].equals("*")){
                    q.add(new XY(i, j, 1)); //スタート地点(初期値のy, x, 1が入っている)
                }
            }
        }

        int l[] = new int[n]; 
        int lcount = 0; //引数

        for(int i=0; i<n; i++){
            l[i] = sc.nextInt();
        }
        Arrays.sort(l);  //?にする手番を昇順に格納

        while(q.size() > 0){
            XY xy = q.poll();
            int y = xy.y;
            int x = xy.x;
            int d = xy.d;

            String in = "*";    //書き込むテキスト。初期値は*。

            if(l[0] == 0 && d == 1 && lcount < n){  //0が入っている。初期地点を?にする場合
                map[y][x] = "?";
                lcount++;
            }

            //dがl[lcount]と同じなら?を入れる
            //lcountを1増やす
            //この時nを越えた時点でIndexOutになる。lcountがn-1に来た時点で増加処理を通させない
            //→一度上限まで来てしまったら判定の必要がないと言える
            //増加条件を「一致したとき」にしてしまうと、最初の一回でlcountが動いてしまう
            //l[lcount]の値がdより小さいとき

            if(l[lcount] < d && lcount < n-1){   //ここが!!!
                lcount++;
            }
            if(l[lcount] == d){    //逆!!!
                in = "?";
            }

            if(y > 0 && map[y-1][x].equals(".")){   //上
                map[y-1][x] = in;
                q.add(new XY(y-1, x, d+1));
            }
            if(y < h-1 && map[y+1][x].equals(".")){   //下
                map[y+1][x] = in;
                q.add(new XY(y+1, x, d+1));
            }
            if(x < w-1 && map[y][x+1].equals(".")){   //右
                map[y][x+1] = in;
                q.add(new XY(y, x+1, d+1));
            }
            if(x > 0 && map[y][x-1].equals(".")){   //左
                map[y][x-1] = in;
                q.add(new XY(y, x-1, d+1));
            }
        }

        for(int i=0; i<h; i++){ //出力
            for(int j=0; j<w; j++){
                System.out.print(map[i][j]);
            }
            System.out.println("");
        }
    }
}

class XY{
    int y;
    int x;
    int d;

    XY(int y, int x, int d){
        this.y = y;
        this.x = x;
        this.d = d;
    }
}
            if(l[lcount] < d && lcount < n-1){   //ここが!!!
                lcount++;
            }
            if(l[lcount] == d){    //逆!!!
                in = "?";
            }

 こういうオチでした。
 引数を増やすタイミングが遅かったんですね。
「持ってこられたd(探索深度)が、今参照したいl[lcount](?にしたい探索深度)より大きければ、引数を増やす」
 んだから、
「先に今の引数とあってるかどうか確認して、それから増やす」じゃ上手くいかないの当然です。

 よし、ようやくハマりを抜けた。
 Aランク相当問題にチャレンジ出来るぞう。

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