zoukankan      html  css  js  c++  java
  • poj 3009 Curling 2.0

    题目大意:

      一冰壶在board上运动,会沿着原来的方向一直运动,直到遇到障碍物。此时冰壶停下,障碍物消失,你重新给冰壶指定一个方向,冰壶继续运动,直到达到终点。求你给方向的次数。

    要点:

         1.运动时碰到障碍物才会停下,如果它周围都是障碍物,就没法运动。

       2.题目要求次数必须小于等于10,超过则认为不能到达

    代码如下:

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #define max_ans 15
     5 int dir[][2] = {{0,1}, {0,-1},{-1,0},{1,0}};
     6 int w, h;
     7 int sx, sy;
     8 
     9 char board[22][22];
    10 int ans;
    11 
    12 
    13 void dfs(int x, int y, int step) {
    14     if(step > 10) {
    15         return;
    16     }
    17     if(board[x][y] == 3) {
    18         if(step < ans) {
    19             ans = step;
    20         }
    21         return;
    22     }
    23     for(int i = 0; i < 4; i++) {
    24         int tpx = x;
    25         int tpy = y;
    26         while(tpx >= 0 && tpy >= 0 && tpx < h && tpy < w  && (board[tpx][tpy] == 0 || board[tpx][tpy] == 2)) {
    27             tpx = tpx + dir[i][0];
    28             tpy = tpy + dir[i][1];
    29         }
    30         
    31         if(tpx >= 0 && tpy >= 0 && tpx < h && tpy < w) {
    32             if(board[tpx][tpy] == 3) {
    33                 if(step < ans) {
    34                     ans = step;
    35                 }
    36                 continue;
    37             }
    38             else {
    39                 int tx = tpx - dir[i][0];
    40                 int ty = tpy - dir[i][1];
    41                 
    42                 if(tx != x || ty != y) {
    43                     if(board[tpx][tpy] == 1) {
    44                         board[tpx][tpy] = 0;
    45                         dfs(tx, ty, step+1);
    46                         board[tpx][tpy] = 1;
    47                     }    
    48                 }
    49             }
    50         }
    51         
    52         
    53     }
    54 }
    55 int main(int argc, char const *argv[])
    56 {
    57     //freopen("input.txt","r",stdin);
    58     while(scanf("%d %d",&w, &h) != EOF && (w != 0 && h != 0)) {
    59         
    60         for(int i = 0; i < h; i++) {
    61             for(int j = 0; j < w; j++) {
    62                 scanf("%d",&board[i][j]);
    63                 if(board[i][j] == 2) {
    64                     sx = i;
    65                     sy = j;
    66                 }
    67             }
    68         }
    69         ans = max_ans;
    70         dfs(sx, sy, 1);
    71         if(ans == max_ans) {
    72             puts("-1");
    73         }
    74         else {
    75             printf("%d
    ",ans);
    76         }
    77     }
    78     return 0;
    79 }

    这道题我提交了很多次,尤其是31 行到46行,必须把逻辑搞清楚,不然很容易错。

  • 相关阅读:
    restframework 自定义返回响应格式
    restframework 分页器
    Python设计模式
    Pytest系列
    Pytest系列
    Pytest系列 -pytest-dependency 用例依赖
    restframework jwt登录验证
    restframework 自定义json返回格式
    Axure RP8 注册码
    LVM 移除PV步骤
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5801961.html
Copyright © 2011-2022 走看看