One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let’s consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.
In order not to get lost, Vasya decided to act as follows.
Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
Let’s assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.
大致题意:
迷宫中有(n + 1)个房间,编号从1到(n + 1).)最在第一个房间,走出迷宫,他需要去(n + 1)号。
迷宫的每个房间都有两个单向入口。可以使用第一个入口从它移动到房间号(i + 1),也可以使用第二个入口从它移动到房间号pi,这里是1 ≤ pi ≤ i.
进入某个房间,他都会在天花板上画一个十字架。最初,瓦希亚在1号房间的天花板上画了一个十字架。如果天花板现在包含奇数个十字,使用第二个入口(它通向房间pi),否则用第一个入口。求他最终需要使用门户才能到达房间(n + 1)的次数与1e0+7取模。
题解:设动态数组dp[i]代表人在第几个房间。怎么写动态方程显然有两种选择。因为每次有到房间会有因为标记选择出口。
动态方程:1.dp[i]=(dp[i]+dp[i-1])%mod;
2.dp[i]=(dp[i]+1)%mod;
#include<stdio.h>
#include
#include<math.h>
#include
#include
using namespace std;
const int mod=1e10+7;
int n,a[1010],now,tp;
long long dp[1010];
int main()
{
while(cin>>n)
{
for (int i=1;i<=n;i++)
cin>>a[i];
dp[0]=0;
dp[1]=0;
for (int i=2;i<=n+1;i++)
{
dp[i]=(dp[i]+dp[i-1])%mod;
now=i-1;
while (1)
{
tp=a[now];
dp[i]=(dp[i]-dp[tp]+mod)%mod;
dp[i]=(dp[i]+1)%mod;
now=tp;
if(tp==now) break;
}
dp[i]=(dp[i]+dp[i-1])%mod;
dp[i]=(dp[i]+1)%mod;
}
cout<<dp[n+1]%mod<<endl;
}
return 0;
}
走迷宫(房间问题一维)
最新推荐文章于 2023-11-16 18:35:02 发布