1656: [Usaco2006 Jan] The Grove 树木

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 246  Solved: 158
[Submit][Status][Discuss]

Description

The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here's a typical example where '.' is pasture (which Bessie may traverse), 'X' is the grove of trees, '*' represents Bessie's start and end position, and '+' marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): ...+... ..+X+.. .+XXX+. ..+XXX+ ..+X..+ ...+++* The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.

牧场里有一片树林,林子里没有坑.
    贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回到起点.她能上下左右走,也能走对角线格子.牧场被分成R行C列(1≤R≤50,1≤C≤50).下面是一张样例的地图,其中“.”表示贝茜可以走的空地,  “X”表示树林,  “*”表示起点.而贝茜走的最近的路已经特别地用“+”表示出来. 
 
题目保证,最短的路径一定可以找到.

Input

* Line 1: Two space-separated integers: R and C

* Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them).

    第1行输入R和C,接下来R行C列表示一张地图.地图中的符号如题干所述.

Output

* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

    输出最少的步数.

Sample Input

6 7
.......
...X...
..XXX..
...XXX.
...X...
......*

Sample Output

13

题目保证所有的树木一定在一个联通块中,并且起始点一定不会被树包围

(也就是说至少有两个方向上没有树木)

那么步骤:

①输入时的第一个'X',如果它上面没有'*',那么将这个'X'上面所有的'.'全部置为'T',否则就找下一个'X'

②从'*'处开始BFS,将'T'和'X'全部视为墙

③暴力所有的'T',答案就是每个'T'两测点最短路和的最小值+2(注意斜着的也算)

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{int x;int y;
}Point;
Point now, temp;
queue<Point> q;
char str[55][55];
int bet[55][55], dir[8][2] = {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,-1,-1,1};
int main(void)
{int n, m, i, j, x, y, k, fx, fy, l, r, ans;scanf("%d%d", &n, &m);memset(str, 'X', sizeof(str));x = y = fx = fy = -1;for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf(" %c", &str[i][j]);if(str[i][j]=='X' && fx==-1 && (y!=j || x==-1 || x>=i)){for(k=1;k<=i-1;k++)str[k][j] = 'T';fx = i, fy = j;}if(str[i][j]=='*')x = i, y = j;}}now.x = x, now.y = y;memset(bet, 62, sizeof(bet));q.push(now);bet[now.x][now.y] = 0;while(q.empty()==0){now = q.front();q.pop();for(k=0;k<=7;k++){temp.x = now.x+dir[k][0];temp.y = now.y+dir[k][1];if(str[temp.x][temp.y]!='X' && str[temp.x][temp.y]!='T' && bet[now.x][now.y]+1<bet[temp.x][temp.y]){bet[temp.x][temp.y] = bet[now.x][now.y]+1;q.push(temp);}}}ans = 100000;for(i=1;i<=fx-1;i++){j = fy;l = min(bet[i][j-1], min(bet[i-1][j-1], bet[i+1][j-1]));r = min(bet[i][j+1], min(bet[i-1][j+1], bet[i+1][j+1]));ans = min(ans, l+r+2);}printf("%d\n", ans);return 0;
}

bzoj 1656: [Usaco2006 Jan] The Grove 树木(BFS)相关推荐

  1. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  2. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 380  Solved: 2 ...

  3. bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(高精度完全背包)

    1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 599  Solved: 3 ...

  4. bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    题意 给你一个无向图 问你最少添加多少条边可以使得他变成边双图 题解 直接双连通缩点 得到一颗树 然后答案是叶子节点/2向上取整 取法是每一次找两个LCA深度最小的叶子,两个连边就可以了 然后不知道为 ...

  5. BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数(数位DP+恶心细节)

    BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 Time Limit: 5 Sec  Memory Limit: 64 MB Description 正如你所知 ...

  6. bzoj1719 [Usaco2006 Jan] Roping the Field 麦田巨画

    1719: [Usaco2006 Jan] Roping the Field 麦田巨画 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 80  Solve ...

  7. bzoj 1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛(BFS)

    1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛 Time Limit: 1 Sec  Memory Limit: 64 MBSec  Special Judge S ...

  8. [BZOJ] 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 827  S ...

  9. BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛【Floyd】

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MB Description FJ的N(1 <= ...

最新文章

  1. 运维7年,对Linux的经验总结
  2. SPARK:作业基本运行原理
  3. 安川g7变频器说明书_安川机器人故障维修合集
  4. caffe cifar10试跑问题总结
  5. KMP算法(待优化)--2015年7月25日14:04:25V1.0版
  6. open函数返回-1_记录学习python的第3天-递归函数/文件操作
  7. [Angularjs]国际化
  8. 测试 极客时间_针对数据极客和记者测试DocHive
  9. (22)FPGA软核、固核、硬核介绍
  10. WCF分布式安全开发实践(1):传输安全模式之匿名客户端:Transport_None_WSHttpBinding
  11. 2018.08.22 NOIP模拟 string(模拟)
  12. Everything的使用-初级篇
  13. 我国会计界计算机软件界大规模研究,初级会计电算化第一章练习.doc
  14. SQL的order by函数语法及其用法实例——排序方式
  15. [办公自动化]目录修改以及插入分页符后行间距自动变宽
  16. 智能运动手环设计构思
  17. 囍囍囍~~~ 以后可能用的着
  18. arcgis栅格缺值填补
  19. 效率神器Apifox_API 文档、API 调试、API Mock、API 自动化测试工具推荐
  20. 42道最新java开发常见面试题:JavaSE基础知识

热门文章

  1. python是什么-什么是Python?最全的python百科
  2. 为什么黑客都用python-终于发现为什么黑客都用python
  3. p语言是python吗-Python 这语言真是混乱和原始
  4. 输入法黑科技:语音识别准确率98% 用户超过6亿
  5. 汉语语音情绪识别,Emotion Recognition by Speech Signal in Mandarin,音标,读音,翻译,英文例句,英语词典...
  6. 车机“智能互联”深度评测:第三弹 吉利博越PRO与GKUI 19
  7. cas5.3:CAS Server搭建
  8. 【王道操作系统笔记】系统调用
  9. python getopterror_python getopt抛出getopterror选项——mode不能有参数
  10. 计算机知识wendang,计算机基础知识Microsoft Word 文档