Wang Xifeng's Little Plot



Problem Description
《Dream of the Red Chamber》(also 《The Story of the Stone》) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin. But the last 40 chapters of the original
version is missing, and that part of current version was written by Gao E. There is a heart breaking story saying that after Cao Xueqin died, Cao's wife burned the last 40 chapter manuscript for heating because she was desperately poor. This story was proved
a rumor a couple of days ago because someone found several pages of the original last 40 chapters written by Cao. 



In the novel, Wang Xifeng was in charge of Da Guan Yuan, where people of Jia family lived. It was mentioned in the newly recovered pages that Wang Xifeng used to arrange rooms for Jia Baoyu, Lin Daiyu, Xue Baochai and other teenagers. Because Jia Baoyu was
the most important inheritor of Jia family, and Xue Baochai was beautiful and very capable , Wang Xifeng didn't want Jia Baoyu to marry Xue Baochai, in case that Xue Baochai might take her place. So, Wang Xifeng wanted Baoyu's room and Baochai's room to be
located at two ends of a road, and this road should be as long as possible. But Baoyu was very bad at directions, and he demanded that there could be at most one turn along the road from his room to Baochai's room, and if there was a turn, that turn must be
ninety degree. There is a map of Da Guan Yuan in the novel, and redists (In China English, one whose job is studying 《Dream of the Red Chamber》is call a "redist") are always arguing about the location of Baoyu's room and Baochai's room. Now you can solve this
big problem and then become a great redist.
 
Input
The map of Da Guan Yuan is represented by a matrix of characters '.' and '#'. A '.' stands for a part of road, and a '#' stands for other things which one cannot step onto. When standing on a '.', one can go to adjacent '.'s through 8 directions: north, north-west,
west, south-west, south, south-east,east and north-east.



There are several test cases.



For each case, the first line is an integer N(0<N<=100) ,meaning the map is a N × N matrix.



Then the N × N matrix follows.



The input ends with N = 0.
 
Output
For each test case, print the maximum length of the road which Wang Xifeng could find to locate Baoyu and Baochai's rooms. A road's length is the number of '.'s it includes. It's guaranteed that for any test case, the maximum length is at least 2.
 
Sample Input
  1. 3
  2. #.#
  3. ##.
  4. ..#
  5. 3
  6. ...
  7. ##.
  8. ..#
  9. 3
  10. ...
  11. ###
  12. ..#
  13. 3
  14. ...
  15. ##.
  16. ...
  17. 0
 
Sample Output
  1. 3
  2. 4
  3. 3
  4. 5
 
Source

解题思路:

N * N的矩阵。 走的方向为8个方向,当中' . '表示可走,以下用点表示,' #' 表示不可走,找出一条最长的路径包括几个点。输出点的个数。当中路径的要求是 最多包括一个直角(拐一个弯)。

int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}}   定义方向。 依照 上。左上,右,右下,下,左下,左,左上 的顺序定义,编号为0,1,2,3,4,5,6,7

那么构成直角的两个方向有7对,各自是  0 2,  1 3 。 2  4, 3 5 ,4 6, 5 7 。6 0 。7 1

那么枚举每一个可走的点。求出该点向8个方向分别走的最远距离,然后依照上面的配对,找出一条最长的合法路径。

枚举全然部可走的点,也就得出答案了。

代码:

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. char mp[102][102];
  8. int n;
  9. int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
  10. int ds[8];//一个点每一个方向最长能够走多远
  11. int all[8];//每一个点为直角的拐点两边最长走多远
  12.  
  13. int main()
  14. {
  15. while(scanf("%d",&n)!=EOF)
  16. {
  17. if(n==0)
  18. break;
  19. getchar();
  20. for(int i=0;i<n;i++)
  21. {
  22. for(int j=0;j<n;j++)
  23. scanf("%c",&mp[i][j]);
  24. getchar();
  25. }
  26. int ans=-1;
  27. for(int i=0;i<n;i++)
  28. for(int j=0;j<n;j++)
  29. {
  30. if(mp[i][j]=='.')
  31. {
  32. memset(all,0,sizeof(all));
  33. memset(ds,0,sizeof(ds));
  34. for(int k=0;k<8;k++)
  35. {
  36. int x=i,y=j;
  37. while(x>=0&&x<n&&y>=0&&y<n&&mp[x][y]=='.')
  38. {
  39. ds[k]++;//每一个方向上最远走多少
  40. x+=dir[k][0];
  41. y+=dir[k][1];
  42. }
  43. }
  44. for(int i=0;i<8;i++)
  45. {
  46. all[i]=ds[i]+ds[(i+2)%8];//构成直角的两个方向
  47. ans=max(ans,all[i]);
  48. //cout<<"ans"<<ans<<endl;
  49. }
  50. }
  51. }
  52. printf("%d\n",ans-1);
  53. }
  54. return 0;
  55. }

[ACM] HDU 5024 Wang Xifeng&#39;s Little Plot (构造,枚举)的更多相关文章

  1. HDU 5024 Wang Xifeng&#39;s Little Plot 搜索

    pid=5024">点击打开链接 Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. HDU 5024 Wang Xifeng's Little Plot (DP)

    题意:给定一个n*m的矩阵,#表示不能走,.表示能走,让你求出最长的一条路,并且最多拐弯一次且为90度. 析:DP,dp[i][j][k][d] 表示当前在(i, j)位置,第 k 个方向,转了 d ...

  3. HDU 5024 Wang Xifeng's Little Plot(枚举)

    题意:求一个图中只有一个90°拐点的路的最大长度. 分析:枚举每一个为'.'的点,求出以该点为拐点的八种路中的最大长度,再比较所有点,得出最大长度即可. 如上样例,这样是个90°的角... 注意:最多 ...

  4. 2014 网选 5024 Wang Xifeng's Little Plot

    题意:从任意一个任意一个可走的点开始找一个最长的路,这条路如果有转弯的话, 那么必须是 90度,或者没有转弯! 思路: 首先用dfs将所有可走点开始的 8 个方向上的线段的最长长度求出来 ! step ...

  5. hdu5024 Wang Xifeng's Little Plot (水

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 网络赛 Wang Xifeng's Little Plot Time Limit: 2000/1000 M ...

  6. hdu 5024 最长的L型

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 找到一个最长的L型,L可以是斜着的 简单的模拟 #include <cstdio> #incl ...

  7. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  8. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  9. HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...

随机推荐

  1. DOM中的节点属性

    摘抄自:http://www.imooc.com/code/1589 nodeName 属性: 节点的名称,是只读的. 1. 元素节点的 nodeName 与标签名相同 2. 属性节点的 nodeNa ...

  2. 【ubuntu】配置zsh

    1. 修改默认shell为zsh chsh -s /bin/zsh echo $SHELL$ 2. 下载oh-my-zsh wget https://raw.github.com/robbyrusse ...

  3. svg动画 之 我的自制太阳系

    SVG意为可缩放矢量图形,svg的图片与普通的jpg,png等图片相比,其优势在于不失真.一般普通的图片放大后,会呈现出锯齿的形状,但是svg图片则不会这样,它可以被高质量地打印. 现在就用dream ...

  4. artTemplate模板引擎的不同使用方式

    arttemplate提供了两种不同的使用方式 一种是将模板写在页面内 <script id="test" type="text/html"> &l ...

  5. 关于在redux当中 reducer是如何知道传入的state是初始化state下面的哪一条数据

    首先初始化redux的数据 reducer 那么问题来了,todos这个reducer是如何知道传入的是初始化state下面的todos这条数据呢? 合并reducer 合并之后是这样的 他们之间的关 ...

  6. Static 静态内部类

    Java中的类可以是static吗?答案是可以.在java中我们可以有静态实例变量.静态方法.静态块.类也可以是静态的. java允许我们在一个类里面定义静态类.比如内部类(nested class) ...

  7. 转:mysql 索引

    转:mysql 索引 文章归属:http://feiyan.info/16.html,我想自己去写了,但是发现此君总结的非常详细.直接搬过来了 关于MySQL索引的好处,如果正确合理设计并且使用索引的 ...

  8. Bzoj 3145 - [Feyat cup 1.5]Str

    bzoj 3145 - [Feyat cup 1.5]Str Description 给你两个长度\(10^5\)级别的串\(S, T\) 求\(S,T\)的最长模糊匹配公共子串 模糊匹配 : 至多一 ...

  9. URAL1960 Palindromes and Super Abilities

    After solving seven problems on Timus Online Judge with a word “palindrome” in the problem name, Mis ...

  10. java中的序列化与反序列化,还包括将多个对象序列化到一个文件中

    package Serialize; /** * Created by hu on 2015/11/7. */ //实现序列化必须实现的接口,这就是一个空接口,起到标识的作用 import java. ...