zoukankan      html  css  js  c++  java
  • 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
    ");
    54         else printf("%d
    ",k);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    c/c++混编
    inotify监听文件
    二维数组
    CentOS7 修改系统时间
    书签书签
    c语言并行程序设计之路(四)(barrier的实现和条件变量)
    MPI分布式内存编程(一):预备知识
    有些狮子不喝咖啡:条件式与合取式的翻译
    【部分博客已搬家至博客园】对CSDN、博客园和简书的一点比较
    c语言并行程序设计之路(三)(信号量与同步)
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9019121.html
Copyright © 2011-2022 走看看