USACO Silver 2019 January - Icy Perimeter

Authors: Tanish Tyagi, Brad Ma, Juheon Rhee


Official Analysis (C++)

Implementation

Time Complexity: O(N2)\mathcal{O}(N^2)

Java

import java.io.*;
import java.util.*;
public class IcyPerimeter {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("perimeter");
n = io.nextInt();
// we do n + 2 because there is an extra outer ring for flood fill
numberedMatrix = new int[n + 2][n + 2];

C++

// created by Tanish Tyagi
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1005;
char grid[MAXN][MAXN]; // the grid itself
int n;

Python

# This solution does TLE on the USACO website on one test case because python is slow.
from collections import deque
with open("perimeter.in") as r:
t = r.readline
n = int(t())
ice = []
visited = [[False] * n for _ in range(n)]
for _ in range(n):

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!