zoukankan      html  css  js  c++  java
  • [GRYZ2014]迷宫问题

       设有一个N*N方格的迷宫,入口和出口分别在左上角和右上角,迷宫格子中分别放有0和1,0表示可走,1表示不能走,迷宫走的规则如图。当迷宫给出之后,找出一条从入口到出口的通路。

    输入:N

          N*N的迷宫

    输出:具体路径

    输入样例:

    8

    0

    0

    0

    1

    1

    0

    1

    0

    1

    0

    1

    1

    0

    1

    1

    0

    0

    1

    0

    0

    1

    0

    0

    1

    0

    0

    1

    1

    0

    1

    0

    1

    0

    1

    0

    0

    0

    1

    1

    0

    0

    1

    1

    1

    1

    1

    0

    1

    0

    0

    1

    1

    1

    0

    1

    1

    1

    1

    0

    0

    0

    0

    0

    0

     

    输出样例:

    (1,1)-(2,1)-(3,1)-(2,2)-(1,3)-(2,4)-(3,3)-(4,3)-(5,2)-(6,3)-(7,3)-(8,2)-(8,1)

    const
      fx:array[1..8] of longint=(0,0,-1,1,-1,-1,1,1);
      fy:array[1..8] of longint=(-1,1,0,0,-1,1,-1,1);
    type lujing=record
          h:longint;
          l:longint;
    end;
    var
      ditu:array[1..10,1..10] of 0..1;
      visited:array[1..10,1..10] of boolean;
      b:array[1..100] of lujing;
      n:longint;
      i,j:longint;
      sum:longint=0;
    procedure find(x,y,k:longint);
    var fi:longint;
    begin
        b[k].h:=x;
        b[k].l:=y;
        if (x=1) and (y=n) then inc(sum);
        for fi:=1 to 8 do
         if ((x+fx[fi]>0)
            and(x+fx[fi]<=n)
            and(fy[fi]+y>0)
            and(fy[fi]+y<=n)
            and(ditu[fx[fi]+x,fy[fi]+y]<>1)
            and(visited[fx[fi]+x,fy[fi]+y]=true))
          then
          begin
           visited[x,y]:=false;
           x:=fx[fi]+x;
           y:=fy[fi]+y;
           find(x,y,k+1);
           x:=x-fx[fi];
           y:=y-fy[fi];
           if (x=1) and (y=n) then inc(sum);
           visited[x,y]:=true;
          end;
    end;
    begin
       read(n);
       fillchar(visited,sizeof(visited),true);
       for i:=1 to n do
        for j:=1 to n do
         read(ditu[i,j]);
       find(1,1,1);
       writeln(sum);
    end.
  • 相关阅读:
    日期比较
    Hashtable哈希表和SortedList排序列表类[转贴]
    XML读取数据
    列表效果,附上字数过多,将列表布局损坏
    event.keycode值大全[转帖]
    常用正则表达式
    写在d2沙龙之前的话
    物理引擎demo (4) —— 力、关节和马达
    基于向量的运动
    物理引擎demo (2) —— 拖拽
  • 原文地址:https://www.cnblogs.com/yangqingli/p/4709279.html
Copyright © 2011-2022 走看看