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
3
#.#
##.
..#
3
...
##.
..#
3
...
###
..#
3
...
##.
...
0
 
Sample Output
3
4
3
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个方向分别走的最远距离,然后依照上面的配对,找出一条最长的合法路径。

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

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std; char mp[102][102];
int n;
int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int ds[8];//一个点每一个方向最长能够走多远
int all[8];//每一个点为直角的拐点两边最长走多远 int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
scanf("%c",&mp[i][j]);
getchar();
}
int ans=-1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(mp[i][j]=='.')
{
memset(all,0,sizeof(all));
memset(ds,0,sizeof(ds));
for(int k=0;k<8;k++)
{
int x=i,y=j;
while(x>=0&&x<n&&y>=0&&y<n&&mp[x][y]=='.')
{
ds[k]++;//每一个方向上最远走多少
x+=dir[k][0];
y+=dir[k][1];
}
}
for(int i=0;i<8;i++)
{
all[i]=ds[i]+ds[(i+2)%8];//构成直角的两个方向
ans=max(ans,all[i]);
//cout<<"ans"<<ans<<endl;
}
}
}
printf("%d\n",ans-1);
}
return 0;
}

[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. NSURLSession使用模板和AFNetworking使用模板(REST风格)

    1.NSURLSession使用模板 NSURLSession是苹果ios7后提供的api,用来替换 NSURLConnection会话指的是程序和服务器的通信对象//一.简单会话不可以配合会话(ge ...

  2. POJ 2836:Rectangular Covering(状态压缩DP)

    题目大意:在一个平面内有若干个点,要求用一些矩形覆盖它们,一个矩形至少覆盖两个点,可以相互重叠,求矩形最小总面积. 分析: 数据很小,很容易想到状压DP,我们把点是否被覆盖用0,1表示然后放在一起得到 ...

  3. 【bzoj3555】[Ctsc2014]企鹅QQ 字符串hash

    题目描述 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体 ...

  4. 【bzoj1520】[POI2006]Szk-Schools 费用流

    题目描述 输入 输出 如果有可行解, 输出最小代价,否则输出NIE. 样例输入 5 1 1 2 3 1 1 5 1 3 2 5 5 4 1 5 10 3 3 3 1 样例输出 9 题解 费用流 设xi ...

  5. 短文对话的神经反应机 -- Neural Responding Machine for Short-Text Conversation学习笔记

    最近学习了一篇ACL会议上的文章,讲的是做一个短文对话的神经反映机, 原文: 会议:ACL(2015) 文章条目:    Lifeng Shang, Zhengdong Lu, Hang Li: Ne ...

  6. ext2 与 ext3

    http://linux.vbird.org/linux_basic/1010appendix_B.php https://baike.baidu.com/item/Ext2/822106?fr=al ...

  7. Maven构建多模块项目

    使用Maven构建多模块项目 转自:http://www.cnblogs.com/xdp-gacl/p/4242221.html 在平时的Javaweb项目 开发中为了便于后期的维护,我们一般会进行分 ...

  8. Vue.js笔记 — vue-router路由懒加载

    用vue.js写单页面应用时,会出现打包后的JavaScript包非常大,影响页面加载,我们可以利用路由的懒加载去优化这个问题,当我们用到某个路由后,才去加载对应的组件,这样就会更加高效,实现代码如下 ...

  9. pat 团体天梯赛 L3-009. 长城

    L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...

  10. 【源码】List<T>泛型绑定repeater,以及repeater的交替绑定

    原文发布时间为:2009-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 后台: using System;using System.Collections.Generic; public ...