A - A Dangerous Maze

Description

You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.

If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can't remember anything. So, every time you come to the beginning position, you have no past experience.

Now you want to find the expected time to get out of the maze.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of doors. The next line contains n space separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.

Output

For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print 'inf'. Print the result in p/q format. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.

Sample Input

3

 

1

1

 

2

-10 -3

 

3

3 -6 -9

Sample Output

Case 1: 1/1

Case 2: inf

Case 3: 18/1

#include<stdio.h>
#include<math.h>
int a[1010];
int main()
{
	int n,r,count,sum;
	scanf("%d",&r);
	int ca=0;
	while(r--)
	{
		ca++;
	    count=sum=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
  	  {
    		scanf("%d",&a[i]);
    		sum+=fabs(a[i]);
    		if(a[i]<0)
    		 count++;
    	}
    	printf("Case %d: ",ca);
	    if(count==n)  printf("inf\n");
    	else
		{
		    int	x= sum,m; 
            int y=n-count,y1=n-count;
            while (y != 0) 
           { 
               m = y; 
               y = x%y; 
               x = m; 
           }
           sum/=x;
           y1/=x;
	       printf("%d/%d\n",sum,y1);
		}
		
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A*算法是一种启发式搜索算法,可以用于解决迷宫问题。它通过估计从起点到终点的距离来指导搜索方向,从而提高搜索效率。下面是使用A*算法解决迷宫问题的步骤: 1. 定义状态表示:将迷宫看作一个二维网格图,每个格子表示一个状态,状态包括格子的坐标、是否可达、起点到该格子的距离和该格子到终点的估计距离(即启发函数值)。 2. 定义启发函数:启发函数用于估计从当前状态到达终点的距离,常用的启发函数有曼哈顿距离和欧几里得距离。 3. 定义开放列表和关闭列表:开放列表用于存储待扩展的状态,关闭列表用于存储已扩展的状态。 4. 初始化:将起点加入开放列表,并将起点到起点的距离设为0,起点到终点的估计距离设为启发函数值。 5. 迭代扩展状态:重复以下步骤直到找到终点或开放列表为空: 1)从开放列表中选取启发函数值最小的状态作为当前状态。 2)将当前状态从开放列表中移除,并加入关闭列表。 3)如果当前状态为终点,则搜索结束。 4)否则,将当前状态的相邻可达状态加入开放列表,并更新它们的起点到起点的距离和该状态到终点的估计距离。 6. 回溯路径:从终点开始,沿着每个状态的父状态指针回溯到起点,即可得到一条最短路径。 下面是一个使用A*算法解决迷宫问题的Python代码示例: ```python import heapq def manhattan_distance(point1, point2): return abs(point1[0] - point2[0]) + abs(point1[1] - point2[1]) def astar(maze, start, end): rows, cols = len(maze), len(maze[0]) open_list = [(0, start)] closed_list = set() parent = {} g_score = {start: 0} f_score = {start: manhattan_distance(start, end)} while open_list: _, current = heapq.heappop(open_list) if current == end: path = [] while current in parent: path.append(current) current = parent[current] path.append(start) return path[::-1] closed_list.add(current) for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]: neighbor = current[0] + i, current[1] + j if 0 <= neighbor[0] < rows and 0 <= neighbor[1] < cols and maze[neighbor[0]][neighbor[1]] == 0: tentative_g_score = g_score[current] + 1 if neighbor in closed_list and tentative_g_score >= g_score.get(neighbor, float('inf')): continue if tentative_g_score < g_score.get(neighbor, float('inf')): parent[neighbor] = current g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + manhattan_distance(neighbor, end) heapq.heappush(open_list, (f_score[neighbor], neighbor)) return None maze = [[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]] start = (0, 0) end = (4, 4) path = astar(maze, start, end) print(path) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值