描述

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)

代码

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int N=;
int n,G[N][N],in[N][N],vis[N][N];
int dx[]={,,,-};
int dy[]={,-,,};
struct p
{
int x,y,step,pre,prex,prey;
}t,h;
int bfs(int s)
{
memset(in,,sizeof(in));
memset(vis,,sizeof(vis));
queue<p>q;
t.x=,t.y=,t.step=,t.pre=s,t.prex=,t.prey=;
q.push(t);
while(!q.empty())
{
t=q.front();q.pop();
if(++in[t.x][t.y]>=)vis[t.x][t.y]=;//一个点入队次数>设定值就标记为不可走
if(t.x==n&&t.y==n+)return t.step;
for(int i=;i<;i++)
{
h.x=t.x+dx[i],h.y=t.y+dy[i];
h.step=t.step+;
h.pre=G[t.x][t.y];//前驱
h.prex=t.x,h.prey=t.y;//前驱的x和y
int sum=h.pre+G[t.prex][t.prey];
if(!vis[h.x][h.y]&&h.x>=&&h.x<=n&&h.y>=&&h.y<=n+&&(t.prex!=h.x||t.prey!=h.y)&&G[h.x][h.y]&&sum%G[h.x][h.y]==)
q.push(h);
}
}
return -;
}
int main()
{
int s,e;
while(scanf("%d%d%d",&n,&s,&e)!=EOF)
{
memset(G,,sizeof(G));
G[][]=s;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
scanf("%d",&G[i][j]);
G[i][n+]=;
}
G[n][n+]=e;
int k=bfs(s);
if(k==-)printf("Impossible\n");
else printf("%d\n",k);
}
return ;
}

TZOJ 3709:Number Maze(广搜记录前驱)的更多相关文章

  1. 台州OJ 3709: Number Maze (数组越界不报RE,报WA坑爹)

    http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3709 You are playing on ...

  2. TZOJ 5279 马拉松比赛(广搜)

    描述 有一块矩形的海域,其中有陆地也有海洋,这块海域是CSUFT_ACM集训队的训练基地,这一天,昌神说要集训队的队员不能总是训练,于是昌神提出了中南林ACM集训队第一场环陆马拉松比赛,顾名思义就是围 ...

  3. TZOJ 3533 黑白图像(广搜)

    描述 输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如图所示的图形有3个八连块. 输入 第1行输入一个正 ...

  4. POJ 3026 Borg Maze 广搜(BFS)+最小生成树

    题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...

  5. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  6. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  7. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...

  8. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

  9. TZOJ 2755 国际象棋(广搜+哈希)

    描述 在n*n的国际象棋棋盘中,给定一“马(Knight)”和一“后(Queen)”的位置,问“马”能否在m步之内(包括m步)到达“后”的位置?马的走法是:每步棋先横走或直走一格,然后再斜走一格,即走 ...

随机推荐

  1. python_03 各种运算符

    1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 先计算括号中表达式 计算顺序:and,or,not在一个表达式中从前到后计算, 若and前一个元素为false则立刻返回为False,不计算后面 ...

  2. 企业微信二次开发之-如何获取secret序列号

    第一步:登录JEEWX后台,配置微信企业号账号信息(企业号.企业号应用) [1].配置企业微信信息 参数对应位置参考如下: [2].配置应用信息 必须四字段: 第二步: 登录企业微信后台,配置企业号应 ...

  3. day08-字符串操作

    name = 'hello,world,WORLD! 123,你好' #capitalize()#首字母大写,其他全部变成小写,没有iscapitalize()方法print(name.capital ...

  4. (已解决)java.lang.NoSuchMethodException: com.kevenwu.pojo.User.<init>()

    搭建ssm框架时报了如下错误,原因是: mybatis在初始化bean的时候需要无参构造器, 如果写了有参构造器,将会把无参构造器覆盖掉,加上一个无参构造器就可以了

  5. redis 学习笔记2(集群之哨兵模式的使用)

    redis3.0之前已经有了哨兵模式,3.0之后有了cluster(分片集群),官方不推荐使用!!主要原因是分片后单节点故障后需要实现手动分槽... 集群较为成熟的解决方案codis,公司使用的是哨兵 ...

  6. DMA/Zero copy

    DMA: 直接内存访问,是一种不经过CPU而直接从内存存取数据的数据交换模式.在DMA模式下,CPU只须向DMA控制器下达指令,让DMA控制器来处理数据的传送,数据传送完毕再把信息反馈给CPU,这样就 ...

  7. shell编程:基本语法

    要掌握一门语言,就要先掌握它的语法.如同C语言一般,shell也有自己的语法. 变量 按照惯例,Shell变量通常由字母加下划线开头,由任意长度的字母.数字.下划线组成.有两种类型的Shell变量: ...

  8. 使用sigaction函数

    sigaction函数 修改信号处理动作(通常在Linux用其来注册一个信号的捕捉函数) :失败:-1,设置errno 参数: act:传入参数,新的处理方式.oldact:传出参数,旧的处理方式. ...

  9. How to Pronounce TH after N or Z

    How to Pronounce TH after N or Z Share Tweet Share Tagged With: Linking Consonant to Consonant The T ...

  10. Linux查看CPU、内存、IO占用高的进程

    查看CPU占用高的top15进程 | | 查看内存占用高的top15进程 | | 查看IO占用高的top15进程 ./ind_high_io_process.py 3 4 5.其中3表示间隔3秒获取一 ...