题意:给定一个n*m的矩阵,#表示不能走,.表示能走,让你求出最长的一条路,并且最多拐弯一次且为90度。

析:DP,dp[i][j][k][d] 表示当前在(i, j)位置,第 k 个方向,转了 d 次变的最多次数,然后用记忆化搜索就好。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define freopenr freopen("in.txt", "r", stdin)
  17. #define freopenw freopen("out.txt", "w", stdout)
  18. using namespace std;
  19.  
  20. typedef long long LL;
  21. typedef pair<int, int> P;
  22. const int INF = 0x3f3f3f3f;
  23. const double inf = 0x3f3f3f3f3f3f;
  24. const double PI = acos(-1.0);
  25. const double eps = 1e-8;
  26. const int maxn = 100 + 5;
  27. const int mod = 1e9 + 7;
  28. const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
  29. const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
  30. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  31. int n, m;
  32. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  34. inline int Min(int a, int b){ return a < b ? a : b; }
  35. inline int Max(int a, int b){ return a > b ? a : b; }
  36. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  37. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  38. inline bool is_in(int r, int c){
  39. return r >= 0 && r < n && c >= 0 && c < m;
  40. }
  41. int dp[maxn][maxn][10][2];
  42. char s[maxn][maxn];
  43.  
  44. int dfs(int r, int c, int d, int num){
  45. int &ans = dp[r][c][d][num];
  46. if(ans >= 0) return ans;
  47. ans = 1;
  48. int x = r + dr[d];
  49. int y = c + dc[d];
  50. if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, d, num) + 1);
  51. if(d < 4 && !num){
  52. x = r + dr[(d+1)%4];
  53. y = c + dc[(d+1)%4];
  54. if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, (d+1)%4, 1) + 1);
  55. x = r + dr[(d+3)%4];
  56. y = c + dc[(d+3)%4];
  57. if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, (d+3)%4, 1) + 1);
  58. }
  59. else if(!num){
  60. int t = (d + 1) % 8;
  61. if(t < 4) t += 4;
  62. x = r + dr[t];
  63. y = c + dc[t];
  64. if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, t, 1) + 1);
  65. t = (d + 3) % 8;
  66. if(t < 4) t += 4;
  67. x = r + dr[t];
  68. y = c + dc[t];
  69. if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, t, 1) + 1);
  70. }
  71. return ans;
  72. }
  73.  
  74. int main(){
  75. while(scanf("%d", &n) == 1 && n){
  76. for(int i = 0; i < n; ++i) scanf("%s", s+i);
  77. memset(dp, -1, sizeof dp);
  78. m = n;
  79. int ans = 0;
  80. for(int i = 0; i < n; ++i)
  81. for(int j = 0; j < n; ++j)
  82. if(s[i][j] == '.') for(int k = 0; k < 8; ++k)
  83. ans = Max(ans, dfs(i, j, k, 0));
  84. printf("%d\n", ans);
  85. }
  86. return 0;
  87. }

HDU 5024 Wang Xifeng's Little Plot (DP)的更多相关文章

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

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

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

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

  3. [ACM] HDU 5024 Wang Xifeng&#39;s Little Plot (构造,枚举)

    Wang Xifeng's Little Plot Problem Description <Dream of the Red Chamber>(also <The Story of ...

  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 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. HDU 3341 Lost's revenge AC自动机+dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  9. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

随机推荐

  1. 数据结构&算法-单链表

    1.引言 工作一年了,感觉越来越懒散,把很多基础性的东西都慢慢遗忘了,最近想趁着还没忘完,回顾一下,整理了点笔记,分享一下. 如有错的地方,欢迎大家怒喷. 2.学习 我们就从最简单的链表开始吧. 链表 ...

  2. 使用 Fiddler2 进行接口测试的方法

    一 前言 部分业务需要进行接口测试,而接口测试的覆盖度稍有不全,可能就会造成包括启动崩溃在内的严重问题.目前本人所在的团队中业务大量使用了本地代码中直接 mock 数据进行测试,此种方法虽然可以测试到 ...

  3. STL之分配器allocator

    简单介绍下STL中的分配器allocators. allocators我们一般不会直接接触到,甚至可能并不清楚它的存在,简单的来说,它就是一个幕后工作者,我的印象中它的作用主要在于为容器分配一定的空间 ...

  4. C++学习基础九——继承

    1.不同于Java中通过extends实现继承,C++是通过:实现的. 2.C++中同样包含public,private,protected三个关键字: public关键字表示在任意其他类中可调用该成 ...

  5. Maven实战(二)构建简单Maven项目

    上一节讲了maven的安装和配置,这一节我们来学习一下创建一个简单的Maven项目 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:gene ...

  6. http://www.cnblogs.com/20135131zxy/

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 二.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门( ...

  7. AX2012R2使用SQL Server2014安装报表扩展报错

    尝试在SQL Server2014上安装AX2012 R2的Reporting Services扩展失败,出现如下错误: "Could not load file or assembly ' ...

  8. 论velocity在不同后台语言下的不同

    第一家公司使用asp.net开发的,本人从事前端工作.当时用velocity写模板程序记得也没配置啥,我就记得写了rewrite,html页面里头直接写的velocity. 现在公司用的java开发的 ...

  9. HDU 3065 病毒侵袭持续中(AC自动机)

    这题数据太水,一开始没有加上Get的方法也能AC..话说AC自动机中一定要注意加上Get的方法!(不然,同一个后缀的其他单词就没被算上了.) 代码如下: #include <stdio.h> ...

  10. Xcode 字体 设置-- Xcode family没有显示的字体

    前往文件夹 -> /Users/user/Library/Developer/Xcode/UserData/FontAndColorThemes/  (user改为自己的用户名) -----如果 ...