Sicily 1389. Linear Pachinko

1389. Linear Pachinko

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

This problem is inspired by Pachinko, a popular game in Japan. A traditional Pachinko machine is a cross between
a vertical pinball machine and a slot machine. The player launches small steel balls to the top of the machine using a plunger as in pinball. A ball drops through a maze of pins that deflect the ball, and eventually the ball either exits at a hole in the bottom
and is lost, or lands in one of many gates scattered throughout the machine which reward the player with more balls in varying amounts. Players who collect enough balls can trade them in for prizes.

For the purposes of this problem, a linear Pachinko machine is a sequence of one or more of the following: holes ("."),
floor tiles ("_"), walls ("|"),
and mountains ("/\"). A wall or mountain will never be adjacent to another wall or mountain. To play the
game, a ball is dropped at random over some character within a machine. A ball dropped into a hole falls through. A ball dropped onto a floor tile stops immediately. A ball dropped onto the left side of a mountain rolls to the left across any number of consecutive
floor tiles until it falls into a hole, falls off the left end of the machine, or stops by hitting a wall or mountain. A ball dropped onto the right side of a mountain behaves similarly. A ball dropped onto a wall behaves as if it were dropped onto the left
or right side of a mountain, with a 50% chance for each. If a ball is dropped at random over the machine, with all starting positions being equally likely, what is the probability that the ball will fall either through a hole or off an end?

For example, consider the following machine, where the numbers just indicate character positions and are not part of the machine itself:

123456789

/\.|__/\.

The probabilities that a ball will fall through a hole or off the end of the machine are as follows, by position: 1=100%, 2=100%, 3=100%, 4=50%, 5=0%, 6=0%, 7=0%, 8=100%, 9=100%. The combined probability for the whole machine is just the average, which is approximately
61.111%.

Input

The input consists of one or more linear Pachinko machines, each 1–79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input.

Output

For each machine, compute as accurately as possible the probability that a ball will fall through a hole or off the end when dropped at random, then output a single line containing that percentage truncatedto
an integer by dropping any fractional part.

Sample Input

/\.|__/\.
_._/\_|.__/\./\_
...
___
./\.
_/\_
_|.|_|.|_|.|_
____|_____
#

Sample Output

61
53
100
0
100
50
53
10

比较水的模拟题,注意下特殊情况就好,0s:

#include <stdio.h>
#include <string.h>

char a[85];

int calculate(char dir, int pos) {
    if (pos == 0 && dir == 'l')
        return 1;//注意直接从边缘滚下也算成功的,下同
    else if (pos == (int)strlen(a) - 1 && dir == 'r')
        return 1;
    else if (dir == 'l') {
        for (int i = pos - 1; i >= 0; i--) {
            if (a[i] == '.')
                return 1;
            else if (a[i] == '\\' || a[i] == '|')
                return 0;
        }
        return 1;//当循环结束的时候,说明都是'_',那么最后是从边缘滚下去了,下同
    } else if (dir == 'r') {
        for (int i = pos + 1; i < (int)strlen(a); i++) {
            if (a[i] == '.')
                return 1;
            else if (a[i] == '|' || a[i] == '/')
                return 0;
        }
        return 1;//
    }
}

int main() {
    while (gets(a) && a[0] != '#') {
        float sum = 0;
        for (int i = 0; i < (int)strlen(a); i++) {
            if (a[i] == '.')
                sum += 1;
            else if (a[i] == '_')
                sum += 0;
            else if (a[i] == '/')
                sum += calculate('l', i);
            else if (a[i] == '\\')
                sum += calculate('r', i);
            else if (a[i] == '|')
                sum += 0.5 * calculate('l', i) + 0.5 * calculate('r', i);
        }
        int int_sum = sum * 100 / (float)strlen(a);//去尾
        printf("%d\n", int_sum);
        memset(a, '\0', sizeof(a));
    }
    return 0;
}
时间: 2024-11-14 12:59:07

Sicily 1389. Linear Pachinko的相关文章

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

Spark MLlib Linear Regression线性回归算法

1.Spark MLlib Linear Regression线性回归算法 1.1 线性回归算法 1.1.1 基础理论 在统计学中,线性回归(Linear Regression)是利用称为线性回归方程的最小平方函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析.这种函数是一个或多个称为回归系数的模型参数的线性组合. 回归分析中,只包括一个自变量和一个因变量,且二者的关系可用一条直线近似表示,这种回归分析称为一元线性回归分析.如果回归分析中包括两个或两个以上的自变量,且因变量和自变量之间

ReLu(Rectified Linear Units)激活函数

ReLu(Rectified Linear Units)激活函数 论文参考:Deep Sparse Rectifier Neural Networks (很有趣的一篇paper) 起源:传统激活函数.脑神经元激活频率研究.稀疏激活性 传统Sigmoid系激活函数 传统神经网络中最常用的两个激活函数,Sigmoid系(Logistic-Sigmoid.Tanh-Sigmoid)被视为神经网络的核心所在. 从数学上来看,非线性的Sigmoid函数对中央区的信号增益较大,对两侧区的信号增益小,在信号的

codevs 1389 乘积平均数

1389 乘积平均数 题目描述 Description 定义 n 个数的乘积平均数为这 n 个数的乘积开 n 次方.给定 n 个正整数,求它们的乘积平均数. 给定n 个正整数,求它们的乘积平均数. 输入描述 Input Description 第一行,一个数 n接下来一行 n 个数,表示给定的 n 个数 输出描述 Output Description 一个实数,表示给定数的乘积平均数,保留2 位小数输出 样例输入 Sample Input 22 8 样例输出 Sample Output 4.00

线性判别分析(Linear Discriminant Analysis, LDA)算法初识

LDA算法入门 一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discriminant ,FLD),是模式识别的经典算法,它是在1996年由Belhumeur引入模式识别和人工智能领域的.性鉴别分析的基本思想是将高维的模式样本投影到最佳鉴别矢量空间,以达到抽取分类信息和压缩特征空间维数的效果,投影后保证模式样本在新的子空间有最大的类间距离和最小的类内距离,即模式在该空间中有最佳

Algorithm - Linear Structures

Linear Data Structures: Stacks Queues Deques Lists The names given to the ends are not significant. What distinguishes one linear structure from another is the way in which items are added and removed, in particular the location where these additions

PRML-Chapter3 Linear Models for Regression

Example: Polynomial Curve Fitting The goal of regression is to predict the value of one or more continuous target variables t given the value of a D-dimensional vector x of input variables. 什么是线性回归?线性回归的目标就是要根据特征空间是D维的输入x,预测一个或多个连续的目标值变量,大多数情况下我们研究的目

A Linear Algebra Problem(唯一性的判定)

A Linear Algebra Problem Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status God Kufeng is the God of Math. However, Kufeng is not so skilled with linear algebra, especially when dealing with matrixes. On

Generalized Linear Model

最近一直在回顾linear regression model和logistic regression model,但对其中的一些问题都很疑惑不解,知道我看到广义线性模型即Generalized Linear Model后才恍然大悟原来这些模型是这样推导的,在这里与诸位分享一下,具体更多细节可以参考Andrew Ng的课程. 一.指数分布 广义线性模型都是由指数分布出发来推导的,所以在介绍GLM之前先讲讲什么是指数分布.指数分布的形式如下: η是参数,T(y)是y的充分统计量,即T(y)可以完全表