zoukankan      html  css  js  c++  java
  • Curling 2.0(dfs)

    http://poj.org/problem?id=3009

    题意:一个小球要从2走到3,1代表障碍物,0代表可走。要求如果小球没遇到障碍物1,则小球按原来的方向一直走,直到遇到障碍物,障碍物变成0,小球任选一个方向继续走,此时步数加1,问从2走到3最少需要几步。

     1 #include <stdio.h>
     2 #include <string.h>
     3 int map[32][32];
     4 int s_x,s_y,e_x,e_y,min;
     5 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
     6 void dfs(int x,int y,int step,int d)
     7 {
     8     if (step > 10||map[x][y] == -1)
     9         return ;
    10     if (x==e_x&&y==e_y)
    11     {
    12         if (min==-1||step < min)
    13         {
    14             min = step;
    15             return ;
    16         }
    17     }
    18     int dx = x+dir[d][0];
    19     int dy = y+dir[d][1];
    20     if (map[dx][dy]!=1)
    21     {
    22         dfs(dx,dy,step,d);
    23         return ;
    24     }
    25     else if (map[dx][dy]==1)
    26     {
    27         map[dx][dy]=0;
    28         for (int i = 0; i < 4; i ++)
    29         {
    30             int xx = x + dir[i][0];
    31             int yy = y + dir[i][1];
    32             if (map[xx][yy]!=1)
    33                 dfs(xx,yy,step+1,i);
    34         }
    35         map[dx][dy] = 1;
    36     }
    37 }
    38 int main()
    39 {
    40     int row,col;
    41     while(~scanf("%d%d",&col,&row))
    42     {
    43         if (col==0 && row==0)
    44             break;
    45         memset(map,-1,sizeof(map));
    46         min = -1;
    47         for (int i = 1; i <= row; i ++)
    48         {
    49             for (int j = 1; j <= col; j ++)
    50             {
    51                 scanf("%d",&map[i][j]);
    52                 if (map[i][j]==2)
    53                 {
    54                     s_x = i;
    55                     s_y = j;
    56                 }
    57                 if (map[i][j]==3)
    58                 {
    59                     e_x = i;
    60                     e_y = j;
    61                 }
    62             }
    63         }
    64        
    65         for (int i = 0; i < 4; i ++)
    66         {
    67             int dx = s_x+dir[i][0];
    68             int dy = s_y+dir[i][1];
    69             if (map[dx][dy]!=1)
    70                 dfs(dx,dy,1,i);
    71         }
    72         printf("%d
    ",min);
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    JavaScript常用正則表達式
    详尽解析window.event对象
    DWR的类却无法在js里用
    javascript控制小数点精度
    49. Group Anagrams
    48. Rotate Image
    64. Minimum Path Sum
    63. Unique Paths II
    62. Unique Paths
    53. Maximum Subarray
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3290747.html
Copyright © 2011-2022 走看看