ICPC 2014 - H. Pachinko
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2014/H-pachinko. Edit
competitive_programming/icpc/2014/H-pachinko/solution.tex to update the written solution and
competitive_programming/icpc/2014/H-pachinko/solution.cpp to update the implementation.
The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.
Problem Statement
Copied statement text kept beside the solution archive for direct reference.
Problem H
Pachinko
Time Limit: 6 seconds
You have been hired by Addictive Coin Machines to help design the next hit in their line of eye-catching,
coin-guzzling, just-one-more-try Pachinko machines for casinos around the world.
Playing a Pachinko machine involves launching balls into a rectangular grid filled with pegs, obstacles,
and targets. The ball bounces around the grid until it eventually hits one of the targets. The player earns
a certain number of points depending on which target is hit.
The grid pattern for the next Pachinko machine has already been designed, but point values for the targets
have not been assigned. These must be set so that like all casino machines, the machine is profitable but
not too profitable. Thus it is important to figure out the probability of a ball hitting any particular target.
That’s your job!
For simplicity, the grid is modeled as a tall rectangle filled with mostly-open spaces (each represented
by ‘.’), impassable obstacles (each represented by ‘X’), and targets (each represented by ‘T’).
A ball is launched randomly with uniform probability into one of the mostly-open spaces on the top row
of the grid. From that point on, collisions with pegs cause the ball to randomly bounce up, down, left, or
right, with various given probabilities. For simplicity, assume these probabilities are the same for every
space in the grid. If the ball bounces into an obstacle or attempts to move off the grid, it won’t actually
move from its current space. When the ball moves into a target it is removed from play.
You can safely assume that the average number of spaces visited by a ball before hitting a target will not
exceed 109 . It would not make for a very enjoyable game if the ball just bounces forever!
For each target, calculate the probability that it is the one hit by a launched ball.
Input
The input consists of a single test case. The first line contains integers w and h, which are the width and
height of the Pachinko grid (1 ≤ w ≤ 20 and 2 ≤ h ≤ 10 000). The next line contains four non-negative
integers u, d, l, and r, which sum to 100 and are the percentage probabilities of the ball bouncing up,
down, left, or right from any open space.
Each of the next h lines contains w characters, each of which is ‘.’, ‘X’, or ‘T’. These lines describe the
Pachinko grid. The first line, which describes the top row of the grid, contains at least one ‘.’ and no
‘T’s.
Output
Display one line for each ‘T’ in the grid, in order from top to bottom, breaking ties left to right. For each
target, display the probability that a launched ball will hit it. Give the answer with an absolute error of
at most 10−6 .
Sample Input 1 Sample Output 1
3 2 0.333333333
20 20 20 40 0.666666667
X.X
T.T
Sample Input 2 Sample Output 2
4 5 0.435853889
12 33 28 27 0.403753221
.... 0.081202502
.XX. 0.079190387
....
T..T
XTTX
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2014\\H. Pachinko Time Limit: 6 seconds}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}