Maze For Robot - UVaLive 4040 dp

The Fun-To-Play company is designing a maze to train a robot. The maze has several check points, each with 3 doors labeled as a , b , and c . The robot has to choose a door to continue his journey. Some of the check points have a hidden door which will only show up when the battery of the robot runs out. There is no hint for which door leads to which check point and some of the doors may lead back to the same check point. The robot will keep moving until the battery runs out. If the robot stops at a check point with the hidden door when the battery runs out, the robot gets a credit. Assume that the time needed for a robot to choose a door and move on to a check point is always the same, and, for simplicity sake, can be considered as one time unit. Also assume that the battery can run for L time units. Your mission is to find the number of different ways for the robot to get credits with the given maze and battery life.

The maze may be modeled by a finite state machine M . The input alphabets are a , b , and c . Each check points is a state of M . The entry is the starting state and the check point with hidden doors are the final states. State i with input a map to state j if the door a in check point i leads to check point j . Then the solution is the same as finding the different number of strings over alphabets {abc} with length L that are accepted by machine M .

Figure 1 shows an example of a maze represented by a finite state machine M . State 1 is the starting state and the final states are indicated by double circles. Given L = 4 , there are only 4 possible strings aaac ,abac , acbc and acac that can be accepted by M .

\epsfbox{p4040.eps}


Technical Specification

  1. There are N (1$ \le$N$ \le$100) states.
  2. The state 1 is always the starting state.
  3. Each state has exactly 3 transitions with input a , b , and c respectively.
  4. There is at least 1 final state.

Input 

The first line of the input file contains an integer indicating the number of test cases to follow. The first line of each test case contains a positive integer N (less than 100) indicating the number of states in the test case. Each of the N following lines will contain the description of one state. Each state is described by a sequence of five integers, the first as the name of the state, followed by three indicating the transitions to the next state of each input alphabet a , b , and c respectively. The last integer is `1' if the state is a final state and `0' otherwise. The last line of the test case is an integer (not larger then 20) denoting the length of the input strings.

Output 

For each test case, output a line with a single integer, indicating the number of different strings that are accepted by the finite state machine (the number of different ways that the robot may take to win).

Sample Input 

3 
4 
1 2 4 4 0
2 2 1 3 0
3 1 2 2 1
4 4 4 4 0
4 
8 
1 2 3 4 0
2 1 5 6 0
3 1 5 7 0
4 1 6 7 0
5 2 3 8 0
6 2 4 8 0
7 3 4 8 0
8 5 6 7 1
9
5
1 2 4 5 1
2 1 3 5 0
3 4 2 5 1
4 3 1 5 0 
5 5 5 5 0  
19

Sample Output 

4 
4920 
0

题意:这道题看着挺夸张,但是很简单,就是说你最开始在1点,每个点可能到其他三个点(即使相同也算不同的情况),问你L步之后停在的那个点是特殊点的方案数。(特殊点就是给定数据的第五个数是1)

思路:dp[i][j]表示i步后停在j点的方案数。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll dp[21][110],ans;
int num[110][4];
int main()
{
    int t,n,m,i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&k);
            scanf("%d%d%d%d",&num[k][0],&num[k][1],&num[k][2],&num[k][3]);
        }
        scanf("%d",&m);
        memset(dp,0,sizeof(dp));
        dp[0][1]=1;
        for(i=0;i<m;i++)
           for(j=1;j<=n;j++)
           {
               dp[i+1][num[j][0]]+=dp[i][j];
               dp[i+1][num[j][1]]+=dp[i][j];
               dp[i+1][num[j][2]]+=dp[i][j];
           }
        ans=0;
        for(i=1;i<=n;i++)
           if(num[i][3]==1)
             ans+=dp[m][i];
        printf("%lld\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值