STEP: 1 盤面の情報取得
問題
解答
step1.java
import java.util.*;
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();
// 改行
sc.nextLine();
String[][] board = createBoard(h, w, sc);
displayCoordinatesInText(board, n, sc);
sc.close();
}
/**
* boardの作成
*
* @param h 高さ
* @param w 横幅
* @param sc 標準入力
*
* @param board
*/
public static String[][] createBoard(int h, int w, Scanner sc){
String[][] board = new String[h][w];
for(int y = 0; y < h; y++ ){
String line = sc.nextLine();
board[y] = line.split("");
}
return board;
}
/**
* 座標の値の表示
*
* @param board board
* @param n 確認したい座標数
* @param sc 標準入力
*/
public static void displayCoordinatesInText(String[][] board, int n, Scanner sc){
for(int i = 0; i < n; i++){
int y = sc.nextInt();
int x = sc.nextInt();
System.out.println(board[y][x]);
}
}
}
結果
STEP: 2 盤面の情報変更
問題
解答
step2.java
import java.util.*;
import java.util.HashMap;
import java.util.Map;
/*
・ 1 行目には盤面の行数を表す整数 H , 盤面の列数を表す整数 W , 与えられる座標の数を表す整数 N が与えられます。
・ 続く H 行のうち i 行目 (0 ≦ i < H) には、盤面の i 行目の文字をまとめた文字列 S_i が与えられ、 S_i の j 文字目は、盤面の i 行目の j 列目に書かれている文字を表します。(0 ≦ j < W)
・ 続く N 行 には、 文字を書き換えるマスの y , x 座標が与えられます。(1 ≦ i ≦ N)
*/
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[][] board = createBase(h, w, sc);
// 座標取得
Integer[] coordinate = getCoordinates(n, sc);
// ボードの書き換え
board = rewriteBoard(n, coordinate, board);
// boardの表示
displayBoard(h, w, board);
}
/**
* 盤面を作成する
* h 高さ
* w 幅
* sc 盤面情報
*/
private static String[][] createBase(int h, int w, Scanner sc){
String[][] board = new String[h][w];
sc.nextLine();
for(int y = 0; y < h; y++){
String line = sc.nextLine();
String[] letters = line.split("");
for(int x = 0; x < w; x++){
board[y][x] = letters[x];
}
}
return board;
}
/**
* 座標を取得する
* n 座標数
* sc 座標情報
*/
private static Integer[] getCoordinates(int n, Scanner sc){
Integer[] coordinate = new Integer[2*n];
for(int i = 0; i < 2*n; i++){
coordinate[i] = sc.nextInt();
}
return coordinate;
}
/**
* 盤面を書き換える
* n 座標数
* coordinate 座標情報
* board 盤面情報
*/
private static String[][] rewriteBoard(int n, Integer[] coordinate, String[][] board){
for(int i = 0; i < 2*n; i++){
int x = coordinate[i];
int y = coordinate[i+1];
board[x][y] = "#";
i++;
}
return board;
}
/**
* 盤面を表示する
* h 高さ
* w 幅
* board 盤面情報
*/
private static void displayBoard(int h, int w, String[][] board){
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
System.out.print(board[y][x]);
}
System.out.println();
}
}
}
結果
STEP: 3 マップの判定・横
問題
解答
step3.java
import java.util.*;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int w = sc.nextInt();
// ボード作成
String[][] board = createBase(h, w, sc);
displayBoard(h, w, board);
}
/**
* 盤面を作成する
* h 高さ
* w 幅
* sc 盤面情報
*/
private static String[][] createBase(int h, int w, Scanner sc){
String[][] board = new String[h][w];
sc.nextLine();
for(int y = 0; y < h; y++){
String line = sc.nextLine();
String[] letters = line.split("");
for(int x = 0; x < w; x++){
board[y][x] = letters[x];
}
}
return board;
}
/**
* 盤面を表示する
* h 高さ
* w 幅
* board 盤面情報
*/
private static void displayBoard(int h, int w, String[][] board){
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
if(locateCoordinates(y, x, w, board)){
System.out.println(y + " " + x);
}
}
}
}
/**
* 座標の位置確認
* 左端のマスの場合は「右のマスが "#" 」であれば、
* 右端のマスの場合は「左のマスが "#" 」であれば条件を満たす
*
* y 高さの座標
* x 幅の座標
* w 幅
* board 盤面情報
*/
private static boolean locateCoordinates(int y, int x, int w, String[][] board){
boolean judgement = false;
if(x == 0){
// 左端判定
if(checkMark(y, x+1, board)){
return true;
}
} else if(x == w-1){
// 右端判定
if(checkMark(y, x-1, board)){
return true;
}
} else {
// 真ん中確認
// 左端
if(checkMark(y, x-1, board)){
judgement = true;
} else {
return false;
}
// 右端
if(checkMark(y, x+1, board)){
judgement = true;
} else {
return false;
}
}
return judgement;
}
/**
* 座標のマーク確認
* #を確認する
*
* y 高さの座標
* x 幅の座標
* board 盤面情報
*/
private static boolean checkMark(int y, int x, String[][] board){
if(Objects.equals(board[y][x],"#")){
return true;
}
return false;
}
}
結果
問題
解答
step4.java