paizaラーニングレベルアップ問題集の文字列の配列をやってみました。
問題
C
#include <stdio.h>
int main() {
int H, W, r, c;
scanf("%d %d %d %d", &H, &W, &r, &c);
r -= 1;
c -= 1;
char S[H][W+1];
for (int i = 0; i < H; i++) scanf("%s", S[i]);
puts(S[r][c] == '#' ? "Yes" : "No");
return 0;
}
C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int H, W, r, c;
cin >> H >> W >> r >> c;
r -= 1;
c -= 1;
vector<string> S(H);
for (int i = 0; i < H; i++) cin >> S[i];
cout << (S[r][c] == '#' ? "Yes" : "No") << endl;
return 0;
}
C#
using System;
class Program
{
public static void Main()
{
int[] hwrc = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int h = hwrc[0], r = hwrc[2] - 1, c = hwrc[3] - 1;
string[] S = new string[h];
for (int i = 0; i < h; i++) S[i] = Console.ReadLine();
Console.WriteLine(S[r][c] == '#' ? "Yes" : "No");
}
}
Go
package main
import "fmt"
func main() {
var H, W, r, c int
fmt.Scan(&H, &W, &r, &c)
r -= 1
c -= 1
S := make([]string, H)
for i := 0; i < H; i++ {
fmt.Scan(&S[i])
}
if S[r][c] == '#' {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
}
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 r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
String[] S = new String[H];
for (int i = 0; i < H; i++) S[i] = sc.next();
sc.close();
System.out.println(S[r].charAt(c) == '#' ? "Yes" : "No");
}
}
JavaScript
const S = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const [H, W, r, c] = S.shift().split(' ').map(Number);
console.log(S[r-1][c-1] === '#' ? "Yes" : "No");
Kotlin
fun main() {
val (H, _, R, C) = readln().split(' ').map { it.toInt() }
val r = R - 1
val c = C - 1
val S = Array(H) { readln() }
println(if (S[r][c] == '#') "Yes" else "No")
}
PHP
<?php
[$H, $W, $r, $c] = array_map("intval", explode(' ', fgets(STDIN)));
$r -= 1;
$c -= 1;
$S = [];
for ($i = 0; $i < $H; $i++) $S[] = fgets(STDIN);
echo $S[$r][$c] === '#' ? "Yes" : "No", PHP_EOL;
?>
Perl
my ($H, $W, $r, $c) = map { int($_) } split ' ', <STDIN>;
$r -= 1;
$c -= 1;
my @S;
for (1..$H) {
push @S, [split '', <STDIN>];
}
print $S[$r][$c] eq '#' ? "Yes" : "No", $/;
Python3
H, W, r, c = map(int, input().split())
r -= 1
c -= 1
S = list(map(lambda _ : input(), range(H)))
print("Yes" if S[r][c] == '#' else "No")
Ruby
H, W, r, c = gets.split.map(&:to_i)
r -= 1
c -= 1
S = H.times.map { gets }
puts S[r][c] == '#' ? "Yes" : "No"
Scala
import scala.io.StdIn._
object Main extends App{
val Array(h, _, r, c) = readLine().split(' ').map { _.toInt }
val S = (0 until h).map { _ => readLine() }
println(if (S(r-1)(c-1) == '#') "Yes" else "No")
}
Swift
let hwrc = readLine()!.split(separator: " ").compactMap { Int($0) }
let (h, r, c) = (hwrc[0], hwrc[2] - 1, hwrc[3] - 1)
let S = (0..<h).map { _ in Array(readLine()!) }
print(S[r][c] == "#" ? "Yes" : "No")