| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:还是超时,我已经记忆化搜索了,代码附上,求建议啊In Reply To:还是超时,我已经记忆化搜索了,代码附上 Posted by:kanxue2002 at 2013-03-30 16:37:04 > #include <cstdlib>
> #include <iostream>
> #include <stdio.h>
> using namespace std;
> int map[105][105];
> int len[105][105];//最大高度
> int dir[4][2]={
> 1,0,
> 0,-1,
> -1,0,
> 0,1
> } ;//四方向数组
> int m,n;//行,列
> int i,j;//深搜坐标点
> void find(int x,int y,int h){//x,y为当前坐标,h为当前点的高度
> ++h;
> if(h>len[i][j])
> len[i][j]=h;
> int tx,ty;
> for(int k=0;k<4;++k){
> tx=x+dir[k][0];
> ty=y+dir[k][1];
> if(tx>=0&&tx<m)//行越界判断
> if(ty>=0&&ty<n)//列越界判断
> if(map[x][y]>map[tx][ty]){
> // cout<<h<<'\t'<<ti<<'\t'<<tj<<'\t'<<map[ti][tj]<<endl;
> if(tx<i&&ty<j){//这个点已经搜过了
> len[i][j]=h+len[tx][ty]>len[i][j]?h+len[tx][ty]:len[i][j];//如果当前点高度加之前高度大于最大高度 则赋值
> continue;
> }//dp
> find(tx,ty,h);// 将下一点作为当前点递归调用
> }//当前点高度是否比周围点高
> }
> --h;
> }
> int main(int argc, char *argv[])
> {
> int max;//全地图最大高度
> scanf("%d %d",&m,&n);
> while(m!=0&&n!=0){
> max=0;
> for(i=0;i<m;++i)
> for(j=0;j<n;++j){
> scanf("%d",&map[i][j]);//输入地图
> }
> for(i=0;i<m;++i)
> for(j=0;j<n;++j){
> find(i,j,0);//深搜
> if(max<len[i][j])
> max=len[i][j];//求全地图最大高度
> }
> printf("%d\n",max);
> scanf("%d %d",&m,&n);
> }
> return 0;
> }
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator