题目描述

Farmer John's cows have grown lazy and he would like to get them back in shape! He has decided that the best way to do this is by setting up haybales in the field and having the cows jump from the northwest corner of the field to the southeast corner. The field is an n×n square grid. Note that on a standard map, North is up, South is down, East is right and West is left. 
A  cow  can  only  jump  straight east or  south, never  west  or  north,  or even  southeast. They also have a limit k on how many cells they can jump. They can jump over haybales, empty spaces, or any combination, even if there are no haybales in between, but they  cannot land on a haybale. Bessie wants to still be lazy and is interested in the minimum number of jumps to reach the southeast corner of the map from the northwest corner. 

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with a line containing two integers n and  k  (1  ≤  n,k  ≤  2,000),  where  n  is  the  size  of  one  side  of  the  square  grid,  and k is Bessie’s  limit on the number of cells she can jump. Each of the next n lines will contain a string with exactly n characters. Only the characters ‘#’ (a haybale) and ‘.’ (an empty space) will appear. The northwest and southeast corners of the field are guaranteed to 
be empty. 

输出

Output a single integer, which is the minimum number of jumps Bessie needs to reach the southeast corner from the northwest corner, or -1 if Bessie cannot make it.  

样例输入

4 2 
.### 
#... 
.#..

#.#.

样例输出

4

题意:一只羊要从左上跳到右下,最少要跳几步,#是草垛,不能降落,第一个数是方阵的大小,第二个数是羊每一步跳的最长距离。

思路,按照顺序,从第一排开始改,每次记录现在这一点所在的行改到了第几位,所在的列改到了第几位。

另:因为按照顺序改动,所以当改动某一个点时,这个点同行的点已经无法再由其上部分的点改动,就是说现在的值再按行改动之后就是他最好的值,所以如果当前的值就比所用更新的点的值+1要小,那么其后的点都应该由他来改,就是说在这一行里,现在所用的更新点已经没有用了。

代码:

#include<stdio.h>
#include<string.h>
char tu[2005][2005];
int hang[2005];
int hda[2005];
int lda[2005];
int lie[2005];
int bu[2005][2005];
int main()
{int n,k;while(~scanf("%d%d",&n,&k)){for(int i=0; i<n; i++)scanf("%s",tu[i]);if(tu[0][0]=='#'||tu[n-1][n-1]=='#'){printf("-1\n");continue;}memset(hang,0,sizeof(hang));memset(lie,0,sizeof(lie));memset(bu,0,sizeof(bu));memset(lda,0x3f3f3f3f,sizeof(lda));for(int i=0; i<n; i++){for(int j=0; j<n; j++){if(tu[i][j]=='.'){if(bu[i][j]==0&&(i!=0||j!=0))continue;if(lie[i]<=j)lie[i]=j+1;if(hang[j]<=i||lda[j]>bu[i][j]+1){hang[j]=i+1;}if(hang[j]<=i+k)lda[j]=bu[i][j]+1;for(; hang[j]<=i+k && hang[j]<n; hang[j]++){if(bu[hang[j]][j]==0||bu[hang[j]][j]>bu[i][j]+1)bu[hang[j]][j]=bu[i][j]+1;}for(; lie[i]<=j+k && lie[i]<n; lie[i]++){if(bu[i][lie[i]]==0||bu[i][lie[i]]>bu[i][j]+1)bu[i][lie[i]]=bu[i][j]+1;else if(bu[i][lie[i]]!=0&&bu[i][lie[i]]<bu[i][j]+1&&tu[i][lie[i]]=='.'){lie[i]--;break;}}}}}if(bu[n-1][n-1]==0&&n!=1){printf("-1\n");continue;}printf("%d\n",bu[n-1][n-1]);}return 0;
}

【中石油OJ-5151】 Jumping Haybales相关推荐

  1. 中石油oj 2654: 序列合并

    参照别人的思路自己写的代码.(代码量很大也不够优化.) #include <iostream> #include <cstring> using namespace std; ...

  2. 中石油OJ: 美人松高度2

    [问题描述] 又到过年了,狗熊岭的动物们都忙碌起来,张灯结彩准备过年.李老板却要光头强砍掉一些百年美人松回去.美人松都是很高的,但是也不会超过长整型(long long).现在光头强看到丛林里有N颗美 ...

  3. 中石油oj 奥运会跳水比赛 C++ sort函数对结构体的排序

    奥运会跳水比赛 题目解析: 结构体排序,手写cmp的sort函数用法. 题目代码: #include<iostream> #include<algorithm>using na ...

  4. 中石油2018年秋季C语言 试卷答案,[石油华东]《数据采集系统》2018年秋季学期在线作业...

    <数据采集系统>2018年秋季学期在线作业(一) 中石油华东作业资料www.op5n.com整理提供 一.单选题: 1.[单选题]n路的多路模拟开关,每路断开的开关的漏电流均为IR,开关的 ...

  5. 中石油在意大利加油站0.5分/升,会不会赔死啊?

    中石油在意大利新开加油站无论柴油还是汽油只需要0.5欧分/每升,看到这个消息,广大网友都炸天了,为啥油价这么低呢?这是不是双重标准呢?要是加满10次,免费送一辆兰博基尼那不是更带劲? 我们先来看一下事 ...

  6. 与中石油分享SOA成功实践

    中国石油天然气集团公司的"生产运行管理系统"系统是一个地域上覆盖了中国石油十大油田,业务领域涵盖了物理勘探.钻井工程.测井.录井.试油和井下作业等专业应用的信息管理系统.在这个项目 ...

  7. 中石油布局天然气商储 天然气国家储备有望

    http://www.sina.com.cn 2007年09月29日 09:12 华夏时报 本报记者 王冰凝 北京报道 9月26日,总投资90.22亿元,中石油大连液化天然气(以下简称LNG)项目在大 ...

  8. 俄油与中石油达成为期10年的协议;苏伊士完成股东联合体收购,苏冰岚履职首席执行官 | 能动...

    石油和化工 燕山石化制氢项目顺利通过依据T/CAB 0078-2020<低碳氢.清洁氢及可再生氢标准及评级>的审核,并获得由TUV南德意志集团颁发的全国首张清洁氢证书.作为2022年北京冬 ...

  9. 拆分中石油:多种方案流传 但启动阻力重重

    公司报道时代周报张蕊2013-10-10 00:25 我要分享 腾讯微博 QQ空间 QQ好友 新浪微博 2512 转播到腾讯微博 本报记者 张蕊 发自北京 余勇最近的生活已经完全被打乱了,他已经半个月 ...

最新文章

  1. R语言readLines函数、n.readLines函数、readline函数读取文本文件实战
  2. 华为自研搜索引擎曝光:「花瓣」虽小,但要替代谷歌
  3. Spring boot登录错误提示
  4. 10年前腾讯微信后台第一天提交的代码曝光!
  5. 已经创建了AWS EC2实例,Linux系统默认没有root用户,那么如何创建root用户并更改为root用户登录呢?
  6. 3个可以写进简历的京东AI NLP项目实战
  7. 结构体,文件操作,指针,简单练习
  8. 如何让pm2支持es6
  9. php_l3arning_notes_2
  10. 单独编译一个内核模块的方法
  11. 基于jedis.setnx(key, value)实现分布式锁
  12. C语言制作小游戏——贪吃蛇
  13. python脚本编写
  14. matlab工具箱下载
  15. Java 基础入门训练
  16. JAVA基础语法——关键字
  17. Linux驱动学习--ALSA框架(三)PCM设备的创建--以SCO虚拟声卡为例
  18. margin 百分比是按参照物来计算滴 不知道吧?
  19. 22 mysql有那些”饮鸩止渴”提高性能的方法?
  20. 评:日本的“泡沫”代价

热门文章

  1. 西门子机械手臂基于S7-1200 PLC和Wincc组态的定点控制系统设计
  2. 【mmdetection】模型初始化
  3. Docker 镜像编排并部署SpringBoot应用
  4. 和数新获得由国家知识产权局颁发的9项实用新型专利证书
  5. echarts地图罗平县的json_Echarts 加载省地图json
  6. 如\u751f\u4ea7\u8005\u5411\u79fb\u52a8\u63a8\u9001属性文件properties转换中文,使用python
  7. linux下使用UDP发送接收数据
  8. 转载:统计学读物推荐
  9. JMM:Java内存模型
  10. SAP ABAP 业务对象 BUS1112 CostCenterGroup 成本中心组 BAPI 清单和相关 TCODE