2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【電脳少女プログラミング2088 ─壊レタ君を再構築─】「ギャングのアジト」をやってみた。

Last updated at Posted at 2025-01-27

paizaの新作プログラミングゲーム【電脳少女プログラミング2088 ─壊レタ君を再構築─】ギャングのアジト(paizaランク:B相当)をやってみました。


問題


解答例
const [N, ...B] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
console.log(B.map((s) => s.split(' ')).every((b) => b.join() === b.reverse().join()) ? "Yes" : "No");
print("Yes" if all(b == b[::-1] for b in [input().split() for _ in range(int(input()))]) else "No")
puts Array.new(gets.to_i) { gets.split }.all? { |b| b == b.reverse } ? "Yes" : "No"

方針

全ての$1\le i\le N$, $1\le j\le N$に対して$b_{i,j}=b_{i,N+1-j}$が成り立てば左右対称です。

  • $j$については、$j<N+1-j$、即ち$j<\frac{N+1}2$の範囲で調べれば十分です
  • 実装時は0-based indexingとしています
  • 各$b_{i,j}$は、文字列として受け取っても数値として受け取ってもどちらでも構いません
    • スペース区切りで受け取れる言語では整数として
    • 一行ずつ受け取る言語は空白で区切ったあと文字列のまま
      扱っています
  • 入力しながら途中で非対称であることが分かった場合は打ち切る、ということもできますが、今回は入力を全部受け取ってから判定していきたいと思います
  • 二重ループから抜ける時は、二重ループ部分を関数出しして、returnで抜けるようにしています

C
#include <stdio.h>
#include <stdbool.h>

bool is_symmetric(int n, const int b[n][n]) {
	int m = n / 2;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (b[i][j] != b[i][n - j - 1])
				return false;
	return true;
}

int main() {
	int N;
	scanf("%d", &N);
	int B[N][N];
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			scanf("%d", &B[i][j]);
	puts(is_symmetric(N, B) ? "Yes" : "No");
	return 0;
}

C++
#include <iostream>
#include <vector>
using namespace std;

bool is_symmetric(int n, const vector<vector<int>>& b) {
	int m = n / 2;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (b[i][j] != b[i][n - j - 1])
				return false;
	return true;
}

int main() {
	int N;
	cin >> N;
	vector<vector<int>> B(N, vector<int>(N));
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> B[i][j];
	cout << (is_symmetric(N, B) ? "Yes" : "No") << endl;
	return 0;
}

C#
using System;

class Program
{
	private static bool IsSymmetric(int n, String[][] b) {
		int m = n / 2;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				if (!b[i][j].Equals(b[i][n - j - 1]))
					return false;
		return true;
	}
	
	public static void Main()
	{
		int N = int.Parse(Console.ReadLine());
		string[][] B = new string[N][];
		for (int i = 0; i < N; i++)
			B[i] = Console.ReadLine().Split();
		Console.WriteLine(IsSymmetric(N, B) ? "Yes" : "No");
	}
}

Go
package main
import "fmt"

func is_symmetric(n int, b [][]int) bool {
	m := n / 2;
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if b[i][j] != b[i][n - j - 1] {
				return false;
			}
		}
	}
	return true;
}

func main() {
	var N int
	fmt.Scan(&N)
	B := make([][]int, N)
	for i := 0; i < N; i++ {
		B[i] = make([]int, N)
		for j := 0; j < N; j++ {
			fmt.Scan(&B[i][j])
		}
	}
	if is_symmetric(N, B) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

Java
import java.util.*;

public class Main {
	private static boolean isSymmetric(int n, int[][] b) {
		int m = n / 2;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				if (b[i][j] != b[i][n - j - 1])
					return false;
		return true;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] B = new int[N][N];
		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++)
				B[i][j] = sc.nextInt();
		sc.close();
		System.out.println(isSymmetric(N, B) ? "Yes" : "No");
	}
}

JavaScript
function isSymmetric(n, b) {
	const m = n / 2;
	for (var i = 0; i < n; i++)
		for (var j = 0; j < m; j++)
			if (b[i][j] != b[i][n - j - 1])
				return false;
	return true;
}

const lines = require("fs").readFileSync("/dev/stdin", "utf8").split('\n');
const N = Number(lines[0]);
let B = []
for (var i = 1; i <= N; i++) B.push(lines[i].split(' '))
console.log(isSymmetric(N, B) ? "Yes" : "No");

Kotlin
import java.util.*

fun isSymmetric(n: Int, b: Array<Array<Int>>): Boolean {
	val m = n / 2
	for (i in 0 until n)
		for (j in 0 until m)
			if (b[i][j] != b[i][n - j - 1])
				return false
	return true
}

fun main() {
	val sc = Scanner(System.`in`)
	val N = sc.nextInt()
	val B = Array(N) { Array(N){ 0 } }
	for (i in 0 until N)
		for (j in 0 until N)
			B[i][j] = sc.nextInt()
	sc.close()
	println(if (isSymmetric(N, B)) "Yes" else "No")
}

PHP
<?php
	function is_symmetric($n, $b) {
		$m = $n / 2;
		for ($i = 0; $i < $n; $i++)
			for ($j = 0; $j < $m; $j++)
				if ($b[$i][$j] != $b[$i][$n - $j - 1])
					return false;
		return true;
	}

	$N = intval(fgets(STDIN));
	$B = [];
	for ($i = 0; $i < $N; $i++)
		$B[] = explode(' ', fgets(STDIN));
	echo is_symmetric($N, $B) ? "Yes" : "No", PHP_EOL;
?>

Perl
sub is_symmetric {
	my ($n, $b) = @_;
	my $m = int($n / 2);
	for (my $i = 0; $i < $n; $i++) {
		for (my $j = 0; $j < $m; $j++) {
			if ($b->[$i][$j] ne $b->[$i][$n - $j - 1]) {
				return 0;
			}
		}
	}
	return 1;
}

my $N = int(<STDIN>);
my @B;
for $s (<STDIN>) {
	push @B, [split ' ', $s];
}
print is_symmetric($N, \@B) ? "Yes" : "No", $/;

Python3
def is_symmetric(n, b):
	for i in range(n):
		for j in range(n // 2):
			if b[i][j] != b[i][n - j - 1]:
				return False
	return True


N = int(input())
B = []
for _ in range(N):
	B.append(input().split())
print("Yes" if is_symmetric(N, B) else "No")

Ruby
def is_symmetric(n, b)
	m = n / 2
	n.times do |i|
		m.times do |j|
			if b[i][j] != b[i][n - j - 1]
				return false
			end
		end
	end
	return true
end

N = gets.to_i
B = []
N.times do
	B << gets.split
end
puts is_symmetric(N, B) ? "Yes" : "No"

Scala
import scala.io.StdIn._

object Main extends App{
	def isSymmetric(n: Int, b: Array[Array[String]]): Boolean = {
		val m = n / 2
		for (i <- 0 until n)
			for (j <- 0 until m)
				if (b(i)(j) != b(i)(n - j - 1))
					return false
		return true
	}

	val N = readInt()
	val B = Array.fill(N)(Array.empty[String])
	for (i <- 0 until N) 
		B(i) = readLine().split(" ")
	println(if (isSymmetric(N, B)) "Yes" else "No")
}

Swift
func is_symmetric(n: Int, b: Array<Array<String>>) -> Bool {
	let m = n / 2
	for i in 0..<n {
		for j in 0..<m {
			if b[i][j] != b[i][n - j - 1] {
				return false
			}
		}
	}
	return true
}

let N = Int(readLine()!)!
var B: [[String]] = []
for _ in 0..<N {
	B.append(readLine()!.split(separator: " ").map{ String($0) })
}
print(is_symmetric(n: N, b: B) ? "Yes" : "No")
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?