IsingModel
SRM 260 · 2005-08-27 · by gepa
Problem Statement
The Ising model is a simple widespread model in statistical physics for simulating magnetic properties of matter. In this model, we consider a rectangular grid, and we associate each grid point with a value '+' or '-' (which physically represents the spin of the element positioned at that grid point).
When we have such a spin configuration, the total energy of this configuration is computed by adding the values contributed by each pair of neighboring cells (only horizontal and vertical direct neighbors are considered). If the pair consists of two cells with the same spin (both '+' or both '-'), this pair contributes a value of -1 to the total energy. Otherwise (one cell is '+' and its neighbor is '-'), the pair contributes a value of +1 to the total energy.
See example 0 for a detailed computation of the energy value.
You are given a
Constraints
- spins will have between 1 and 50 elements, inclusive.
- Each element of spins will have between 1 and 50 characters, inclusive.
- All elements of spins will have the same number of characters.
- Each character of each element of spins will be either '+' or '-'.
{"-++",
"+-+"}
Returns: 3
There are a total of 7 neighboring pairs here: 4 horizontal (-+), (++), (+-) and (-+) and 3 vertical (-+), (+-) and (++). Pairs of type (+-) and (-+) contribute a value of +1 to the total energy, and pairs of type (--) and (++) contribute a value of -1. The total energy of this configuration is thus: E = 1 - 1 + 1 + 1 + 1 + 1 - 1 = 3.
{"+"}
Returns: 0
With just one element, there is no neighboring pair, so the total energy is 0.
{"++-+",
"-++-",
"+-+-",
"++++"}
Returns: 4
{"-"}
Returns: 0
{"--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------", "--------------------------------------------------"}
Returns: -4900
Submissions are judged against all 60 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class IsingModel with a public method int energy(vector<string> spins) · 60 test cases · 2 s / 256 MB per case