zoukankan      html  css  js  c++  java
  • [codevs1026]逃跑的拉尔夫

    常规的宽搜,每个方向上都一路走到’X‘为止

    如果不加以判重,理论上是对的,但是会做大量无用的计算,要么超时要么数组越界,只能得30分

    不过,这里的判重并不是指每个点只能经过一次,而是指在一层搜索中,每个点只能经过一次,搜索下一层时,判重数组要清空

    程序如下:

    program ex1026;
    type node=record
           x,y,d:longint;
         end;
    var r,c,n,i,j,cen:longint;
        t:string;
        hash:array[0..51,0..51] of boolean;//判重数组
        map:array[0..51,0..51] of char;
        dir:array[1..3000] of longint;
        a:array[1..1000000] of node;//宽搜队列
    
    procedure bfs;
    var l,r,i:longint;
    begin
      l:=0;
      r:=1;
      while l<r do
      begin
        inc(l);
        if a[l].d<>cen then//如果进入下一层搜索判重数组就清空
        begin
          fillchar(hash,sizeof(hash),true);
          cen:=a[l].d;//层数加一
        end;
        case dir[a[l].d] of
          1://begin
            i:=1;
            while map[a[l].x-i,a[l].y]<>'X' do
            begin
              if a[l].d=n then//如果是最后一个方向,下一个点就变成’*begin
                map[a[l].x-i,a[l].y]:='*';
                inc(i);
              end
              else
                if hash[a[l].x-i,a[l].y] then
                begin
                  hash[a[l].x-i,a[l].y]:=false;
                  inc(r);
                  a[r].x:=a[l].x-i;
                  a[r].y:=a[l].y;
                  a[r].d:=a[l].d+1;
                  inc(i);
                end
                else
                  inc(i);
            end;
          end;
          2://begin
            i:=1;
            while map[a[l].x+i,a[l].y]<>'X' do
            begin
              if a[l].d=n then
              begin
                map[a[l].x+i,a[l].y]:='*';
                inc(i);
              end
              else
                if hash[a[l].x+i,a[l].y] then
                begin
                  hash[a[l].x+i,a[l].y]:=false;
                  inc(r);
                  a[r].x:=a[l].x+i;
                  a[r].y:=a[l].y;
                  a[r].d:=a[l].d+1;
                  inc(i);
                end
                else
                  inc(i);
            end;
          end;
          3://西
          begin
            i:=1;
            while map[a[l].x,a[l].y-i]<>'X' do
            begin
              if a[l].d=n then
              begin
                map[a[l].x,a[l].y-i]:='*';
                inc(i);
              end
              else
                if hash[a[l].x,a[l].y-i] then
                begin
                  hash[a[l].x,a[l].y-i]:=false;
                  inc(r);
                  a[r].x:=a[l].x;
                  a[r].y:=a[l].y-i;
                  a[r].d:=a[l].d+1;
                  inc(i);
                end
                else
                  inc(i);
            end;
          end;
          4://begin
            i:=1;
            while map[a[l].x,a[l].y+i]<>'X' do
            begin
              if a[l].d=n then
              begin
                map[a[l].x,a[l].y+i]:='*';
                inc(i);
              end
              else
                if hash[a[l].x,a[l].y+i] then
                begin
                  hash[a[l].x,a[l].y+i]:=false;
                  inc(r);
                  a[r].x:=a[l].x;
                  a[r].y:=a[l].y+i;
                  a[r].d:=a[l].d+1;
                  inc(i);
                end
                else
                  inc(i);
            end;
          end;
        end;
      end;
    end;
    
    begin
      cen:=0;
      readln(r,c);
      for i:=0 to r+1 do
        for j:=0 to c+1 do
          map[i,j]:='X';
      for i:=1 to r do
      begin
        for j:=1 to c do
        begin
          read(map[i,j]);
          if map[i,j]='*' then
          begin
            a[1].x:=i;
            a[1].y:=j;
            map[i,j]:='.';//起点变为’.’
            a[1].d:=1;//第1方向
          end;
        end;
        readln;
      end;
      readln(n);
      for i:=1 to n do
      begin
        readln(t);
        if t='NORTH' then dir[i]:=1;
        if t='SOUTH' then dir[i]:=2;
        if t='WEST' then dir[i]:=3;
        if t='EAST' then dir[i]:=4;
      end;
      bfs;
      for i:=1 to r do
      begin
        for j:=1 to c do
          write(map[i,j]);
        writeln;
      end;
    end.
  • 相关阅读:
    [kuangbin带你飞]专题十二 基础DP1
    bits/stdc++.h
    第七届 山东省ACM Execution of Paladin(水题)
    poj 1523 SPF【点双连通求去掉割点后bcc个数】
    hdoj 5112 A Curious Matt
    【转】我,一个写代码的
    poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
    数据结构第二次上机实验【链表实现多项式的加法和乘法】
    hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
    hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&&输出桥&&字符串处理】
  • 原文地址:https://www.cnblogs.com/victorslave/p/4798569.html
Copyright © 2011-2022 走看看