HDU 5024 Wang Xifeng's Little Plot (DP)
题意:给定一个n*m的矩阵,#表示不能走,.表示能走,让你求出最长的一条路,并且最多拐弯一次且为90度。
析:DP,dp[i][j][k][d] 表示当前在(i, j)位置,第 k 个方向,转了 d 次变的最多次数,然后用记忆化搜索就好。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 100 + 5;
- const int mod = 1e9 + 7;
- const int dr[] = {0, 1, 0, -1, -1, 1, 1, -1};
- const int dc[] = {1, 0, -1, 0, 1, 1, -1, -1};
- const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline int Min(int a, int b){ return a < b ? a : b; }
- inline int Max(int a, int b){ return a > b ? a : b; }
- inline LL Min(LL a, LL b){ return a < b ? a : b; }
- inline LL Max(LL a, LL b){ return a > b ? a : b; }
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- int dp[maxn][maxn][10][2];
- char s[maxn][maxn];
- int dfs(int r, int c, int d, int num){
- int &ans = dp[r][c][d][num];
- if(ans >= 0) return ans;
- ans = 1;
- int x = r + dr[d];
- int y = c + dc[d];
- if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, d, num) + 1);
- if(d < 4 && !num){
- x = r + dr[(d+1)%4];
- y = c + dc[(d+1)%4];
- if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, (d+1)%4, 1) + 1);
- x = r + dr[(d+3)%4];
- y = c + dc[(d+3)%4];
- if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, (d+3)%4, 1) + 1);
- }
- else if(!num){
- int t = (d + 1) % 8;
- if(t < 4) t += 4;
- x = r + dr[t];
- y = c + dc[t];
- if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, t, 1) + 1);
- t = (d + 3) % 8;
- if(t < 4) t += 4;
- x = r + dr[t];
- y = c + dc[t];
- if(is_in(x, y) && s[x][y] == '.') ans = Max(ans, dfs(x, y, t, 1) + 1);
- }
- return ans;
- }
- int main(){
- while(scanf("%d", &n) == 1 && n){
- for(int i = 0; i < n; ++i) scanf("%s", s+i);
- memset(dp, -1, sizeof dp);
- m = n;
- int ans = 0;
- for(int i = 0; i < n; ++i)
- for(int j = 0; j < n; ++j)
- if(s[i][j] == '.') for(int k = 0; k < 8; ++k)
- ans = Max(ans, dfs(i, j, k, 0));
- printf("%d\n", ans);
- }
- return 0;
- }
HDU 5024 Wang Xifeng's Little Plot (DP)的更多相关文章
- HDU 5024 Wang Xifeng's Little Plot(枚举)
题意:求一个图中只有一个90°拐点的路的最大长度. 分析:枚举每一个为'.'的点,求出以该点为拐点的八种路中的最大长度,再比较所有点,得出最大长度即可. 如上样例,这样是个90°的角... 注意:最多 ...
- HDU 5024 Wang Xifeng's Little Plot 搜索
pid=5024">点击打开链接 Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others) Memory ...
- [ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举)
Wang Xifeng's Little Plot Problem Description <Dream of the Red Chamber>(also <The Story of ...
- 2014 网选 5024 Wang Xifeng's Little Plot
题意:从任意一个任意一个可走的点开始找一个最长的路,这条路如果有转弯的话, 那么必须是 90度,或者没有转弯! 思路: 首先用dfs将所有可走点开始的 8 个方向上的线段的最长长度求出来 ! step ...
- 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 ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 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 ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
随机推荐
- 数据结构&算法-单链表
1.引言 工作一年了,感觉越来越懒散,把很多基础性的东西都慢慢遗忘了,最近想趁着还没忘完,回顾一下,整理了点笔记,分享一下. 如有错的地方,欢迎大家怒喷. 2.学习 我们就从最简单的链表开始吧. 链表 ...
- 使用 Fiddler2 进行接口测试的方法
一 前言 部分业务需要进行接口测试,而接口测试的覆盖度稍有不全,可能就会造成包括启动崩溃在内的严重问题.目前本人所在的团队中业务大量使用了本地代码中直接 mock 数据进行测试,此种方法虽然可以测试到 ...
- STL之分配器allocator
简单介绍下STL中的分配器allocators. allocators我们一般不会直接接触到,甚至可能并不清楚它的存在,简单的来说,它就是一个幕后工作者,我的印象中它的作用主要在于为容器分配一定的空间 ...
- C++学习基础九——继承
1.不同于Java中通过extends实现继承,C++是通过:实现的. 2.C++中同样包含public,private,protected三个关键字: public关键字表示在任意其他类中可调用该成 ...
- Maven实战(二)构建简单Maven项目
上一节讲了maven的安装和配置,这一节我们来学习一下创建一个简单的Maven项目 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:gene ...
- http://www.cnblogs.com/20135131zxy/
一.实验内容 1. 使用JDK编译.运行简单的Java程序 2.使用Eclipse 编辑.编译.运行.调试Java程序 二.实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门( ...
- AX2012R2使用SQL Server2014安装报表扩展报错
尝试在SQL Server2014上安装AX2012 R2的Reporting Services扩展失败,出现如下错误: "Could not load file or assembly ' ...
- 论velocity在不同后台语言下的不同
第一家公司使用asp.net开发的,本人从事前端工作.当时用velocity写模板程序记得也没配置啥,我就记得写了rewrite,html页面里头直接写的velocity. 现在公司用的java开发的 ...
- HDU 3065 病毒侵袭持续中(AC自动机)
这题数据太水,一开始没有加上Get的方法也能AC..话说AC自动机中一定要注意加上Get的方法!(不然,同一个后缀的其他单词就没被算上了.) 代码如下: #include <stdio.h> ...
- Xcode 字体 设置-- Xcode family没有显示的字体
前往文件夹 -> /Users/user/Library/Developer/Xcode/UserData/FontAndColorThemes/ (user改为自己的用户名) -----如果 ...