SHUOJ 1552 滑雪(小数据)(BFS)

来源:互联网 发布:linux虚拟主机 编辑:程序博客网 时间:2024/06/17 00:40

题目:SHUOJ-1552

题目链接:http://202.121.199.212/JudgeOnline/problem.php?id=1552

题目:

1552: 滑雪(小数据)

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 38  Solved: 29
[Submit][Status][Web Board]

Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子 

 1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9


一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 7)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

Output

输出最长区域的长度。

Sample Input

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

Sample Output

25

这道题目仍然是经典BFS,但是这个题目需要在每一个点都走一下DFS,这样才能找出来最长的一条(没有优化的情况下),数据量比较小就直接写了,看代码:

#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<queue>using namespace std;int n,m;int map[10][10];                                             //地图int timemap[10][10];                                         //存储长度,习惯性的使用了timemapint dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};struct T{    int x,y;};int ans=0;T sa,sb;                                                    //依旧是这么好听的名字,hhh void bfs(int x,int y){                                      //BFS    queue<T> Q;    sa.x=x;    sa.y=y;    Q.push(sa);                                            //首结点入队    while(!Q.empty()){        sa=Q.front();                                      //队首出队        Q.pop();        for(int i=0;i<4;i++){            int xx=sa.x+dir[i][0];            int yy=sa.y+dir[i][1];            if(xx>=0 && xx <n && yy>=0 && yy<m){                if(map[xx][yy]>map[sa.x][sa.y]){                sb.x=xx;                sb.y=yy;                if(timemap[xx][yy]<timemap[sa.x][sa.y]+1){   //满足步数最长的入队                    timemap[xx][yy]=timemap[sa.x][sa.y]+1;                    Q.push(sb);                }            }            }        }    }}int main(){    while(cin>>n>>m){        for(int i=0;i<n;i++)            for(int j=0;j<m;j++){                cin>>map[i][j];            }        ans=0;        for(int i=0;i<n;i++)            for(int j=0;j<m;j++){                for(int l=0;l<n;l++)                    for(int k=0;k<m;k++)                        timemap[l][k]=1;              //初始化                bfs(i,j);                int max=0;                for(int l=0;l<n;l++)                    for(int k=0;k<m;k++)                        if(timemap[l][k]>max)                            max=timemap[l][k];       //求单个最长                if(ans<=max){                        //求整体最长                    ans=max;                }                             }        cout<<ans<<endl;    }    return 0;}



0 0