paizaの新作プログラミングゲーム【電脳少女プログラミング2088 ─壊レタ君を再構築─】一番通りの繁華街(paizaランク:B相当)をやってみました。
問題
解答例
N=int(input())
R=[input()for _ in range(N)]
print(sum(R[i][j]==R[i][j+k]==R[i+k][j]==R[i+k][j+k]=='.'for i in range(N)for j in range(N)for k in range(1,min(N-i,N-j))))
方針
- $i=1,2,...,N-1$について
- $j=1,2,...,N-1$について
- $k=1,2,...,\min(N-i,N-j)$について
- $r_{i,j},r_{i,j+k},r_{i+k,j},r_{i+k,j+k}$がすべて
.
ならば- 正方形としてカウントします
- $r_{i,j},r_{i,j+k},r_{i+k,j},r_{i+k,j+k}$がすべて
- $k=1,2,...,\min(N-i,N-j)$について
- $j=1,2,...,N-1$について
- 但し、$r_{i,j}\neq$
.
の時点で$k$のループを回さず、次の$j$の処理へ移ることにすると処理が速くなります
C
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
char R[N][N + 1];
for (int i = 0; i < N; i++)
scanf("%s", R[i]);
int count_square = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (R[i][j] != '.') continue;
int m = i > j ? N - i : N - j;
for (int k = 1; k < m; k++) {
if (R[i][j + k] == '.' && R[i + k][j] == '.'
&& R[i + k][j + k] == '.')
count_square += 1;
}
}
}
printf("%d\n", count_square);
return 0;
}
C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<string> R(N);
for (int i = 0; i < N; i++)
cin >> R[i];
int count_square = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (R[i][j] != '.') continue;
int m = min(N - i, N - j);
for (int k = 1; k < m; k++) {
if (R[i][j + k] == '.' && R[i + k][j] == '.'
&& R[i + k][j + k] == '.')
count_square += 1;
}
}
}
cout << count_square << endl;
return 0;
}
C#
using System;
class Program
{
public static void Main()
{
int N = int.Parse(Console.ReadLine());
String[] R = new String[N];
for (int i = 0; i < N; i++) R[i] = Console.ReadLine();
int count_square = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (R[i][j] != '.') continue;
int m = Math.Min(N - i, N - j);
for (int k = 1; k < m; k++) {
if (R[i][j + k] == '.' && R[i + k][j] == '.'
&& R[i + k][j + k] == '.')
count_square += 1;
}
}
}
Console.WriteLine(count_square);
}
}
Go
package main
import "fmt"
func main() {
var N int
fmt.Scan(&N)
R := make([]string, N)
for i := 0; i < N; i++ {
fmt.Scan(&R[i])
}
count_square := 0;
for i := 0; i < N; i++ {
for j := 0; j < N; j++ {
if R[i][j] != '.' {
continue
}
var m int
if i > j { m = N - i } else { m = N - j }
for k := 1; k < m; k++ {
if R[i][j + k] == '.' && R[i + k][j] == '.' &&
R[i + k][j + k] == '.' {
count_square += 1;
}
}
}
}
fmt.Println(count_square)
}
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[] R = new String[N];
for (int i = 0; i < N; i++)
R[i] = sc.next();
sc.close();
int count_square = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (R[i].charAt(j) != '.') continue;
int m = Math.min(N - i, N - j);
for (int k = 1; k < m; k++) {
if (R[i].charAt(j + k) == '.' && R[i + k].charAt(j) == '.'
&& R[i + k].charAt(j + k) == '.')
count_square += 1;
}
}
}
System.out.println(count_square);
}
}
JavaScript
const [N, ...R] = require("fs").readFileSync("/dev/stdin", "utf8").split('\n');
var count_square = 0;
for (var i = 0; i < N; i++) {
for (var j = 0; j < N; j++) {
if (R[i][j] !== '.') continue;
let m = Math.min(N - i, N - j);
for (var k = 1; k < m; k++) {
if (R[i][j + k] === '.' && R[i + k][j] === '.'
&& R[i + k][j + k] === '.')
count_square += 1;
}
}
}
console.log(count_square);
Kotlin
import java.util.*
fun main() {
val sc = Scanner(System.`in`)
val N = sc.nextInt()
val R = Array(N){ "" }
for (i in 0 until N) R[i] = sc.next()
sc.close()
var count_square = 0
for (i in 0 until N) {
for (j in 0 until N) {
val m = Math.min(N - i, N - j)
if (R[i][j] != '.') continue
for (k in 1 until m) {
if (R[i][j + k] == '.' && R[i + k][j] == '.'
&& R[i + k][j + k] == '.')
count_square += 1
}
}
}
println(count_square)
}
PHP
<?php
$N = intval(fgets(STDIN));
$R = [];
for ($i = 0; $i < $N; $i++) $R[] = trim(fgets(STDIN));
$count_square = 0;
for ($i = 0; $i < $N; $i++) {
for ($j = 0; $j < $N; $j++) {
if ($R[$i][$j] !== '.') continue;
$m = min($N - $i, $N - $j);
for ($k = 1; $k < $m; $k++) {
if ($R[$i][$j + $k] === '.' && $R[$i + $k][$j] === '.'
&& $R[$i + $k][$j + $k] === '.')
$count_square += 1;
}
}
}
echo $count_square, PHP_EOL;
?>
Perl
my $N = int(<STDIN>);
my @R;
foreach (<STDIN>) {
push @R, [split "", $_];
}
$count_square = 0;
for (my $i = 0; $i < $N; $i++) {
for (my $j = 0; $j < $N; $j++) {
if ($R[$i][$j] ne '.') {
continue;
}
my $m = $i > $j ? $N - $i : $N - $j;
for (my $k = 1; $k < $m; $k++) {
if ($R[$i][$j + $k] eq '.' && $R[$i + $k][$j] eq '.'
&& $R[$i + $k][$j + $k] eq '.') {
$count_square += 1;
}
}
}
}
print "$count_square$/";
Python3
N = int(input())
R = [input() for _ in range(N)]
count_square = 0
for i in range(N):
for j in range(N):
if R[i][j] != '.':
continue
m = min(N - i, N - j)
for k in range(1, m):
if R[i][j + k] == R[i + k][j] == R[i + k][j + k] == '.':
count_square += 1
print(count_square)
Ruby
N = gets.to_i
R = []
N.times do
R << gets.chomp
end
count_square = 0
N.times do |i|
N.times do |j|
if R[i][j] != '.'
continue
end
m = [N - i, N - j].min
(1...m).each do |k|
if R[i][j + k] == '.' && R[i + k][j] == '.' &&
R[i + k][j + k] == '.'
count_square += 1
end
end
end
end
p count_square
Scala
import scala.io.StdIn._
object Main extends App{
val N = readInt()
val R = for (_ <- 0 until N) yield readLine()
var count_square = 0
for (i <- 0 until N) {
for (j <- 0 until N) {
if (R(i)(j) == '.') {
val m = Math.min(N - i, N - j)
for (k <- 1 until m) {
if (R(i)(j + k) == '.' && R(i + k)(j) == '.'
&& R(i + k)(j + k) == '.') {
count_square += 1
}
}
}
}
}
println(count_square)
}
Swift
let N = Int(readLine()!)!
var R = [[Character]]()
for _ in 0..<N {
R.append(Array(readLine()!))
}
var count_square = 0
for i in 0..<N {
for j in 0..<N {
if R[i][j] != "." {
continue
}
let m = min(N - i, N - j)
for k in 1..<m {
if R[i][j + k] == "." && R[i + k][j] == "."
&& R[i + k][j + k] == "." {
count_square += 1
}
}
}
}
print(count_square)