Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search

Introduction

This theme

AtCoder Grand Contest 033 A - Darker and Darker Difficulty: 1245

This theme, breadth-first search Ruby This is an advanced version of * AtCoder Typical Contest 002 A-Breadth-first search * that was solved before. In particular, it has a strict condition of execution time limit: 1 sec.

ruby.rb


h, w = gets.split.map(&:to_i)
cm = Array.new(h + 2){Array.new(w + 2, 0)}
que = []
1.upto(h) do |i|
  s = gets.chomp
  1.upto(w) do |j|
    if s[j - 1] == '.'
      cm[i][j] = -1
    else
      que.push(i)
      que.push(j)
    end
  end
end
ans = 0
while que.size > 0
  y = que.shift
  x = que.shift
  ans = cm[y][x] + 1
  if cm[y + 1][x] == -1
    cm[y + 1][x] = ans
    que.push(y + 1)
    que.push(x)
  end
  if cm[y - 1][x] == -1
    cm[y - 1][x] = ans
    que.push(y - 1)
    que.push(x)
  end
  if cm[y][x + 1] == -1
    cm[y][x + 1] = ans
    que.push(y)
    que.push(x + 1)
  end
  if cm[y][x - 1] == -1
    cm[y][x - 1] = ans
    que.push(y)
    que.push(x - 1)
  end
end
puts ans - 1

array.rb


cm = Array.new(h + 2){Array.new(w + 2, 0)}

When checking up, down, left and right, the array is enlarged so that you do not get an error with nil.

push.rb


h, w = gets.split.map(&:to_i)
cm = Array.new(h + 2){Array.new(w + 2, 0)}
que = []
1.upto(h) do |i|
  s = gets.chomp
  1.upto(w) do |j|
    if s[j - 1] == '.'
      cm[i][j] = -1
    else
      que << i << j
      cm[i][j] = 0
    end
  end
end
ans = 0
while que.size > 0
  y = que.shift
  x = que.shift
  ans = cm[y][x] + 1
  if cm[y + 1][x] == -1
    cm[y + 1][x] = ans
    que << y + 1 << x
  end
  if cm[y - 1][x] == -1
    cm[y - 1][x] = ans
    que << y - 1 << x
  end
  if cm[y][x + 1] == -1
    cm[y][x + 1] = ans
    que << y << x + 1
  end
  if cm[y][x - 1] == -1
    cm[y][x - 1] = ans
    que << y << x - 1
  end
end
puts ans - 1

Code that uses << instead of the push you received in the comments. Here, the execution time is shortened. Python

  • Python * was TLE this time. Looking at the answers of other athletes, there are many examples of using numpy.

cmd.exe


C:\Users\user>pacman -S mingw-w64-x86_64-python-numpy

I installed it immediately.

** Addition ** I passed by PyPy.

PyPy.py


from collections import deque

h, w = map(int, input().split())
cm = [[0 for j in range(w + 2)] for i in range(h + 2)]
que = deque([])
for i in range(1, h + 1):
    s = input()
    for j in range(1, w + 1):
        if s[j - 1] == ".":
            cm[i][j] = -1
        else:
            que.append(i)
            que.append(j)
ans = 0
while len(que) > 0:
    y = que.popleft()
    x = que.popleft()
    ans = cm[y][x] + 1
    if cm[y + 1][x] == -1:
        cm[y + 1][x] = ans
        que.append(y + 1)
        que.append(x)
    if cm[y - 1][x] == -1:
        cm[y - 1][x] = ans
        que.append(y - 1)
        que.append(x)
    if cm[y][x + 1] == -1:
        cm[y][x + 1] = ans
        que.append(y)
        que.append(x + 1)
    if cm[y][x - 1] == -1:
        cm[y][x - 1] = ans
        que.append(y)
        que.append(x - 1)
print(ans - 1)

Perl

  • Perl * was TLE this time. Looking at the answers of other athletes. .. .. There is no correct answer.

Java This time it is a double stand. The first is a copy of * previous code * with some modifications.

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        int cm[][] = new int[h + 2][w + 2];
        Deque<Integer> que = new ArrayDeque<>();
        for (int i = 1; i <= h; i++) {
            String s = sc.next();
            for (int j = 1; j <= w; j++) {
                if (".".equals(s.substring(j - 1, j))) {
                    cm[i][j] = -1;
                } else {
                    que.add(i);
                    que.add(j);
                }
            }
        }
        sc.close();
        int ans = 0;
        while (que.size() > 0) {
            int y = que.poll();
            int x = que.poll();
            ans = cm[y][x] + 1;
            if (cm[y + 1][x] == -1) {
                cm[y + 1][x] = ans;
                que.add(y + 1);
                que.add(x);
            }
            if (cm[y - 1][x] == -1) {
                cm[y - 1][x] = ans;
                que.add(y - 1);
                que.add(x);
            }
            if (cm[y][x + 1] == -1) {
                cm[y][x + 1] = ans;
                que.add(y);
                que.add(x + 1);
            }
            if (cm[y][x - 1] == -1) {
                cm[y][x - 1] = ans;
                que.add(y);
                que.add(x - 1);
            }
        }
        System.out.println(ans - 1);
    }
}

array.java


        int cm[][] = new int[h + 2][w + 2];

When checking up, down, left and right, the array is enlarged so as not to get ʻArrayIndexOutOfBoundsException`.

class.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        int s[][] = new int[h + 2][w + 2];
        Queue<Pair> que = new ArrayDeque<>();
        for (int i = 1; i <= h; i++) {
            char c[] = sc.next().toCharArray();
            for (int j = 1; j <= w; j++) {
                if (c[j - 1] == '.') {
                    s[i][j] = -1;
                } else {
                    que.add(new Pair(i, j));
                }

            }
        }
        sc.close();

        Pair p;
        int ans = 0;
        while (!que.isEmpty()) {
            p = que.poll();
            ans = s[p.x][p.y] + 1;
            if (s[p.x + 1][p.y] == -1) {
                s[p.x + 1][p.y] = ans;
                que.add(new Pair(p.x + 1, p.y));
            }
            if (s[p.x - 1][p.y] == -1) {
                s[p.x - 1][p.y] = ans;
                que.add(new Pair(p.x - 1, p.y));
            }
            if (s[p.x][p.y + 1] == -1) {
                s[p.x][p.y + 1] = ans;
                que.add(new Pair(p.x, p.y + 1));
            }
            if (s[p.x][p.y - 1] == -1) {
                s[p.x][p.y - 1] = ans;
                que.add(new Pair(p.x, p.y - 1));
            }
        }
        System.out.println(ans - 1);
    }
}

class Pair {
    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

ArrayDeque seems to be slow, so in order to reduce inflows and outflows, classes are generated and passed for each class.

  • Java * was a child who could do it.
Ruby Ruby(<<) Python PyPy Perl Java Java(class)
Code length(Byte) 753 676 878 878 777 1511 1580
Execution time(ms) 843 801 TLE 599 TLE 779 345
memory(KB) 27132 27132 52484 37692 174720 114932 70740

Summary

  • Solved AGC 033 A
  • Become familiar with Ruby
  • Installed numpy
  • Java was a capable child

Referenced site

Recommended Posts

Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
[Python] Depth-first search and breadth-first search
AtCoder ABC168 A case expression solved in Ruby and Python
Scraping with Node, Ruby and Python
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
A memo with Python2.7 and Python3 on CentOS
Search the maze with the python A * algorithm
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
Solve with Python [100 past questions that beginners and intermediates should solve] (028 --033 breadth-first search)
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
MessagePack-Try to link Java and Python with RPC
Causal reasoning and causal search with Python (for beginners)
Building a python environment with virtualenv and direnv
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
I wrote a class in Python3 and Java
Why I'm a Java shop and start Python
Launch a web server with Python and Flask
Solving the Lorenz 96 model with Julia and Python
Solving with Ruby AtCoder ABC110 C String Manipulation
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
AtCoder ARC080 D simulation solved in Ruby and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Benchmark for C, Java and Python with prime factorization
Let's make a simple game with Python 3 and iPhone
Crawling with Python and Twitter API 1-Simple search function
I made a LINE BOT with Python and Heroku
Build a python virtual environment with virtualenv and virtualenvwrapper
Investigate Java and python data exchange with Apache Arrow
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python