TZOJ 3709:Number Maze(广搜记录前驱)

描述

You are playing one game called "Number Maze". The map of an example is shown in the following figure.

In the map, there are N*N+2 cells. When the game starts, you stay the top-left cell and you target is to reach the bottom-right cell with fewest number of moves. At the first step, you must move to the right of the start cell. After that, you can move to any cell (left, right, up or down, but can not move diagonally) if the target cell can be divided by the sum of the previous two numbers. However, you should never move backwards. For example, at first, you stay at the "2" cell, and you must move to the "6" cell, then have two selections "8" or "4" because (2+6)/8=1 and (2+6)/4=2, you can not move back to the "2" cell at this step although (2+6)/2=4. One possilbe solution is 2->6->8->7->5->3->4->7->11->2->13, and the total number of moves is 10.
Another solution is also legal but has longer moves:2->6->8->7->5->3->4->7->5->3->4->7->11->2->13

输入

Thare are at most 20 cases. The first line of each case has three integers N<=10, S and T, which N indicates the dimension of the map, S and T indicate the number in the start and target cell. Then follows N lines and each line has N positive integers which indicate each number in the N*N cells.
There has one blank line after each case and you can assume that the total number of all cells is no larger than 1000000.

The inputs are ended with End of File. If you have some questions, please visit the help page.

输出

Each case outputs the fewest number of moves or "Impossible" if you can not reach the target cell per line.

样例输入

3 2 13
6 4 3
8 7 5
2 11 2

样例输出

10

题意

由图可得,从图中S点出发到E点,第一步一定走到右边一格,第二步满足前两步和可以被当前值整除,不可以往回走

题解

首先我们得考虑怎么记录前驱,可以在结构体里新加pre,prex,prey,每次走的时候更新

然后我们还得考虑死循环的问题

例如

2 2 2

2 2

2 2

我们可以把每个点设一个设定值,如果一个点走的次数超过设定值,标记为不可走

写完一交,wa了??原因不明,后来发现没有考虑往回走的问题(t.prex!=h.x||t.prey!=h.y)

代码

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 const int N=15;
 6 int n,G[N][N],in[N][N],vis[N][N];
 7 int dx[]={0,0,1,-1};
 8 int dy[]={1,-1,0,0};
 9 struct p
10 {
11     int x,y,step,pre,prex,prey;
12 }t,h;
13 int bfs(int s)
14 {
15     memset(in,0,sizeof(in));
16     memset(vis,0,sizeof(vis));
17     queue<p>q;
18     t.x=1,t.y=1,t.step=1,t.pre=s,t.prex=1,t.prey=0;
19     q.push(t);
20     while(!q.empty())
21     {
22         t=q.front();q.pop();
23         if(++in[t.x][t.y]>=100)vis[t.x][t.y]=1;//一个点入队次数>设定值就标记为不可走
24         if(t.x==n&&t.y==n+1)return t.step;
25         for(int i=0;i<4;i++)
26         {
27             h.x=t.x+dx[i],h.y=t.y+dy[i];
28             h.step=t.step+1;
29             h.pre=G[t.x][t.y];//前驱
30             h.prex=t.x,h.prey=t.y;//前驱的x和y
31             int sum=h.pre+G[t.prex][t.prey];
32             if(!vis[h.x][h.y]&&h.x>=1&&h.x<=n&&h.y>=1&&h.y<=n+1&&(t.prex!=h.x||t.prey!=h.y)&&G[h.x][h.y]&&sum%G[h.x][h.y]==0)
33                 q.push(h);
34         }
35     }
36     return -1;
37 }
38 int main()
39 {
40     int s,e;
41     while(scanf("%d%d%d",&n,&s,&e)!=EOF)
42     {
43         memset(G,0,sizeof(G));
44         G[1][0]=s;
45         for(int i=1;i<=n;i++)
46         {
47             for(int j=1;j<=n;j++)
48                 scanf("%d",&G[i][j]);
49             G[i][n+1]=0;
50         }
51         G[n][n+1]=e;
52         int k=bfs(s);
53         if(k==-1)printf("Impossible\n");
54         else printf("%d\n",k);
55     }
56     return 0;
57 }

转载于:https://www.cnblogs.com/taozi1115402474/p/9019121.html

DQN(深度 Q 网络)是一种深度强化学习算法,可用于训练智能体在迷宫等复杂环境中采取最优策略。机器人自动走迷宫可以通过 DQN 算法实现。 在机器人自动走迷宫的应用中,输入是由一个迷宫类实例化的对象。迷宫类可以包含迷宫的尺寸、墙壁的位置、起始位置和目标位置等信息。 DQN 算法的实现步骤如下: 1. 定义神经网络:创建一个深度神经网络模型,用于近似 Q 值函数。可以使用卷积神经网络或者全连接神经网络等。 2. 初始化 Q 表:创建一个空的 Q 表,用于记录每个状态和动作的 Q 值。 3. 初始化迷宫状态:将机器人放置在迷宫的起始位置。 4. 选择动作:根据当前状态,使用 ε-greedy 策略选择动作。ε 表示探索的概率。可以在开始时设置较高的ε,逐渐降低以增加利用经验的概率。 5. 执行动作:将机器人执行选择的动作,并根据环境的反馈更新状态。 6. 更新 Q 值:利用当前状态和环境反馈的奖励更新 Q 表,使用下述公式计算新的 Q 值: Q(s,a) = Q(s,a) + α * (r + γ * maxQ(s',a') - Q(s,a)) 其中,α 是学习率,γ 是折扣因子,s' 是新的状态,a' 是根据ε-greedy策略选择的新动作,r 是环境反馈的奖励。 7. 跳转到步骤 4,直到机器人到达目标位置。 通过反复迭代,机器人学习到最优的策略,并在迷宫中找到最短路径到达目标位置。 这就是使用 DQN 算法进行机器人自动走迷宫的应用。该算法能够智能地学习并获得最优策略,而无需手动设计规则。它在其他复杂的问题中也具有潜力,并且在实际应用中取得了良好的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值