http://cstest.scu.edu.cn/soj/contest/problem.action?cid=332&alias=C
garlic and zombies
Description
This problem is recommended for ACM newcomers. huangshenno1 is playing the famous game Plants VS Zombies and he is so boring that he puts garlic onto every grid of the garden. At this time, there happen to be a zombie coming from the middle path. Initially, the zombie is in the nth column but there is a car in the 0th column of every path. A car will beat the zombie as soon as the zombie meets it. Assume that when a zombie meet a garlic, it will either go up-left or go down-left and both possibility is 50%(See the picture, but it will never go out of the garden). huangshenno1 wants to know the possibility of the cars from path 1 to path 5 to beat this zombie if there are n columns.
Input
There are multiple test cases.
For each test case there is only one line with a single integer n indicating the number of columns. (0<=n<=10000)
Output
For each test case output one line five numbers indicating possibilities of the cars from path 1 to path 5 to beat this zombie, each number with 2 digits after the decimal point.
Sample Input
1
9
Sample Output
0.00 0.50 0.00 0.50 0.00
0.00 0.50 0.00 0.50 0.00
思路:这道题有很多人AC,可是我一直都在画图,试图找到一种规律解决该情况
可是还是卡在那了。
比赛后问了同学才知道,直接用模拟法就可以了,通过以前即现在的这道题我总结模拟法如下:
1.题目特征:一般没有什么算法而言,但又找不到合适的规律时,然后题目中在做某一项任务,而且分步进行,前后关联的那种,这时候就可以考虑模拟法了
2.解题步骤:
初始化初值,根据题意模拟步骤行走
#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
const int maxn=10000+10;
double p[maxn][7];
int main()
{int n,i;
memset(p,0,sizeof(p));
p[0][2]=1.00;//设置初始情况,当没有格子的时候直接就在在第三行撞上了
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
p[i][0]=p[i-1][1]*0.5;
p[i][1]=p[i-1][2]*0.5+p[i-1][0];
p[i][2]=p[i-1][3]*0.5+p[i-1][1]*0.5;
p[i][3]=p[i-1][2]*0.5+p[i-1][4];
p[i][4]=p[i-1][3]*0.5;
}
for(i=0;i<4;i++)
printf("%.2f ",p[n][i]);
printf("%.2f\n",p[n][4]);
}
return 0;
}