poj3009

样例和网上找的测试数据 都过了 还是 wa

 

program poj3009;
type
  sta=record
       x,y:integer;
  end;
var
   dir:array[1..4,1..2]of integer=((1,0),(-1,0),(0,1),(0,-1));
   w,h,ans,ansb:integer;
   map:array[0..21,0..21]of integer;
   start:sta;
   flag:boolean;

procedure dfs(x,y,d,depth:integer);
var
  i,tx,ty,j:integer;
begin

     if depth>10 then exit;
     // chu jie
    if map[x,y]=-1 then exit;

    //get ans
    // if flag then exit;
    if map[x,y]=3 then begin
            flag:=true;
            if ansb<ans then ans:=ansb;

            exit;
     end;

    //hited
    if map[x,y]=1 then
    begin
       map[x,y]:=0;
       //tui yi bu
       tx:=x-dir[d,1];ty:=y-dir[d,2];

       for i:=1 to 4 do
                begin
                  inc(ansb);
                  dfs(tx+dir[i,1],ty+dir[i,2],i,depth+1);
                  dec(ansb);
                end;

       map[x,y]:=1;
    end;
    // zhi zou
    if map[x,y]=0 then dfs(x+dir[d,1],y+dir[d,2],d,depth);

end;

procedure main;
var
  i,j,d:integer;
begin
   assign(input,'pro.in');
   assign(output,'pro.out');
   reset(input);
   rewrite(output);

   readln(w,h);
   while  ((w<>0)and(h<>0))do
   begin
       //chu shi hua
       for i:=0 to 21 do
        for j:=0 to 21 do
         map[i,j]:=-1;
       ans:=15;
       ansb:=1; //the start must
       flag:=false;// answer  not get

       //read map
       for i:=1 to h do
        begin
         for j:=1 to w do
            begin
              read(map[i,j]);
              if map[i,j]=2 then
              begin
                  start.x:=i;
                  start.y:=j;
                  map[i,j]:=0;
              end;
            end;
         readln;
        end;

        //dfs get this ans


        for d:=1 to 4 do
         if  map[start.x+dir[d,1],start.y+dir[d,2]]<>1 then
            dfs(start.x+dir[d,1],start.y+dir[d,2],d,1);

        //output ans;
        if ans<=10 then  writeln(ans) else writeln('-1');

        //the next map
        readln(w,h);
   end;
    close(input);
    close(output);

    end;


begin
    main;
end.

转载于:https://www.cnblogs.com/llwoll/archive/2012/02/16/2354058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值