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
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
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.
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 |
Referenced site
Recommended Posts