题意:给定一个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)的更多相关文章

  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. Visual Studio 中可执行文件中嵌入的清单文件

    概要 本分步指南介绍如何在 Microsoft Visual Studio 2005年中的可执行文件 (.exe) 文件中嵌入的清单文件.如果您要开发"认证 Windows Vista&qu ...

  2. openldap sshkey & 用户自定义属性

    http://qiita.com/T_Tsan/items/eeb0a9ae9b4cdeb80934 https://www.ossramblings.com/using-ldap-to-store- ...

  3. 使用DotNetOpenAuth搭建OAuth2.0授权框架

    标题还是一如既往的难取. 我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1.0. ...

  4. TCP协议总结--停止等待协议,连续ARQ协议,滑动窗口协议

    前言:在学习tcp三次握手的过程之中,由于一直无法解释tcpdump命令抓的包中seq和ack的含义,就将tcp协议往深入的了解了一下,了解到了几个协议,做一个小结. 先来看看我的问题: 这是用tcp ...

  5. dll 导出函数名的那些事

    dll 导出函数名的那些事 关键字: VC++  DLL  导出函数 经常使用VC6的Dependency或者是Depends工具查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导 ...

  6. Cacti客户端SNMP的安装和配置

    安装 yum -y install net-snmp 配置 编辑/etc/snmp/snmpd.conf文件 找到下面这句: access  notConfigGroup ""   ...

  7. <COM原理和应用>第七章的ITextObject代码是什么?

      第7章中有如下的描述:-----------------------------------为了在程序中使用"Text.Object"文本对象,我们利用ClassWizard引 ...

  8. 三天没有写题了,罪过!--Hash Table Start

    (1)Island Perimeter 解题思路: 在矩阵上循环并记录岛(1)的个数;如果当前节点是岛,则检查其是否具有任何右邻居或下邻居,有的话邻居计数加1 ;岛的周长结果为islands * 4 ...

  9. 如何在A用户下建立视图,这个视图是A的表与B的表进行关联的?

    这个前提条件是,同一个数据库,不同用户!!!如果是不同数据库,就要用dblink了 一开始,我直接创建视图,但是提示“权限不足”: 于是我是用A登陆,直接用select * from B.sa_tas ...

  10. C语课设心得分享(三)

    调试. 以前咱们写课后习题,一般也不需要使用调试,如果程序编译error,根据错误信息就可以改好:如果是结果错误,那么在稿纸上过几遍基本也可以得出结果. 但咱们这个课设比较大,就需要很多调试的过程,尤 ...