AtCoder Grand Contest 033 A - Darker and Darker Difficulty: 1245
Dieses Thema, Breite Priorität Suche Ruby Dies ist eine erweiterte Version des zuvor gelösten * AtCoder Typical Contest 002 A - Breite Prioritätssuche *. Insbesondere gilt die strikte Bedingung "Ausführungszeitlimit: 1 Sek.".
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)}
Beim Überprüfen von oben, unten, links und rechts wird die Anordnung vergrößert, damit Sie keinen Fehler mit "Null" erhalten.
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
Dieser Code verwendet "<<" anstelle des "Push", den Sie in den Kommentaren erhalten haben. Hier wird die Ausführungszeit verkürzt. Python
TLE.
Wenn man sich die Antworten anderer Wettbewerber ansieht, gibt es viele Beispiele für die Verwendung von "numpy".cmd.exe
C:\Users\user>pacman -S mingw-w64-x86_64-python-numpy
Ich habe es sofort installiert.
** Nachtrag ** Ich kam an PyPy vorbei.
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.
Betrachten Sie die Antworten anderer Wettbewerber. .. .. Es gibt keine richtige Antwort.Java Diesmal ist es ein Doppelstand. Die erste ist eine Kopie von * vorheriger Code * mit einigen Änderungen.
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];
Beim Überprüfen von oben, unten, links und rechts wird das Array vergrößert, sodass Sie keine "ArrayIndexOutOfBoundsException" erhalten.
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 scheint langsam zu sein. Um Zu- und Abflüsse zu reduzieren, werden Klassen für jede Klasse generiert und übergeben.
| Ruby | Ruby(<<) | Python | PyPy | Perl | Java | Java(class) | |
|---|---|---|---|---|---|---|---|
| Codelänge(Byte) | 753 | 676 | 878 | 878 | 777 | 1511 | 1580 |
| Ausführungszeit(ms) | 843 | 801 | TLE | 599 | TLE | 779 | 345 |
| Erinnerung(KB) | 27132 | 27132 | 52484 | 37692 | 174720 | 114932 | 70740 |
Referenzierte Site
Recommended Posts