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自动机上跑,并保证不出现 ...
随机推荐
- C++设计模式-Singleton
Singleton单例模式 Singleton 是对全局变量的取代策略作用:保证一个类只能有一个实例,并提供一个全局唯一的访问点. 仅有一个实例:通过类的静态成员变量来体现.提供访问它的全局访问点:访 ...
- 基于综合服务平台浅谈Sass应用
一. 前言 CSS不是一种编程语言,只是单纯的一行行的描述,没有逻辑没有变量,因此写CSS对于习惯于运用逻辑思维编码的程序员来说是一件很头疼的事.于是勤奋的程序员就开始运转他们敏捷的大脑, ...
- 5,SFDC 管理员篇 - 数据模型 - 数据关联
1,PickList 1,填写基本信息 2, 选择能角色的权限 3,在哪一个层上显示(object 上有多个 Record Type 对应多个层,需要选择在哪一个层显示) 4,Save 2,两个P ...
- $.when().then()
当两个Ajax请求是成功的则执行函数myFunc,如果任一个有错误则执行myFailure. $.when($.ajax("/page1.php"), $.ajax("/ ...
- Python自动化 【第九篇】:Python基础-线程、进程及python GIL全局解释器锁
本节内容: 进程与线程区别 线程 a) 语法 b) join c) 线程锁之Lock\Rlock\信号量 d) 将线程变为守护进程 e) Event事件 f) queue队列 g) 生 ...
- modelsim(1) - 安装和使用 心得
最近一段时间使用modelsim, 一,安装 使用的时候,出现license验证不对. 由于经常换虚拟机,要注意首先MAC地址是否换了,如果换了,license要重新做! 其次/etc/hosts的I ...
- vbs获取命令行里的参数
var args1=WScript.Arguments.Item(0) var args2=WScript.Arguments.Item(1)
- Python 集合方法总结
1.添加一个元素: add(...) Addan element to a set. 1 2 3 4 >>> a = {'shaw',11,22} >>>a. ...
- WCF第二天
消息 : 消息是一个独立的数据单元,它可能由几个部分组成,包括消息正文和消息头. 服务 : 服务是一个构造,它公开一个或多个终结点,其中每个终结点都公开一个或多个服务操作. 终结点 ...
- scala学习心得3
在scala中可以定义函数字面量参数,定义方式如下: