您的位置:首页 > 其它

POJ 1088 滑雪 DFS 记忆化搜索

2013-11-14 08:52 190 查看
http://poj.org/problem?id=1088

校运会放假继续来水一发^ ^

不过又要各种复习,功课拉下了许多 QAQ。

还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^

好了,说正事~

题目大意:

中文题吖,就不用我说了哈哈~~~~~~做中文题真舒服,不用开词典^ ^

思路:

搜索的时候显然会有重复的所以采用记忆化搜索。

顺带用了下宏定义,让代码简洁点。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define  F(i,R) for(int i=0;i<R;i++)
int a[101][101];
int ans[101][101];
int R,C;

int dfs(int r,int c)
{
if(ans[r][c]>0)
return ans[r][c];

int temp=0,res=1;
if(r<R-1 && a[r][c] > a[r+1][c])
res=dfs(r+1,c)+1;

if(c<C-1 && a[r][c] > a[r][c+1])
{
temp=dfs(r,c+1)+1;
res = res> temp ? res: temp;
}
if(r>=1 && a[r][c] > a[r-1][c])
{
temp=dfs(r-1,c)+1;
res = res> temp ? res: temp;
}
if(c>=1 && a[r][c] > a[r][c-1])
{
temp=dfs(r,c-1)+1;
res = res> temp ? res: temp;
}

return ans[r][c]=res;
}

int main()
{
memset(ans,-1,sizeof(ans));
scanf("%d%d",&R,&C);
F(i,R)
F(j,C)
scanf("%d",&a[i][j]);

int x=1;
F(i,R)
F(j,C)
{
int temp=dfs(i,j);
x= x > temp? x: temp;
}

printf("%d\n",x);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: