| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1658 人关注过本帖
标题:一道oj的题
取消只看楼主 加入收藏
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
结帖率:95.37%
收藏
已结贴  问题点数:20 回复次数:5 
一道oj的题
吃土豆
时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.



Now, how much qualities can you eat and then get ?
输入
There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn't beyond 1000, and 1<=M,N<=500.
输出
For each case, you just output the MAX qualities you can eat and then get.
样例输入
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
样例输出
242

    #include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int a[505][505];
int Max,m,n;
void DFS(int x,int y,int temp)
{
    if(x<0||x>m-1||y<0||y>n-1)
        return;
    if(Max<temp)
        Max=temp;
    DFS(x+1,y+1,temp+a[x+1][y+1]);//调试时是这一步出错,,求指教、
    DFS(x-1,y+1,temp+a[x-1][y+1]);
    DFS(x-1,y-1,temp+a[x-1][y-1]);
    DFS(x+1,y-1,temp+a[x+1][y-1]);
}

int main()
{
    while(cin>>m>>n)
    {
        freopen("1.txt","r",stdin);
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                cin>>a[i][j];
        Max=0;
        DFS(0,0,0);
        cout<<Max<<endl;
    }
    return 0;
}

搜索更多相关主题的帖子: int the and MAX temp 
2018-04-08 22:17
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 2楼 wmf2014
好的 谢谢。
2018-04-09 11:58
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 2楼 wmf2014
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int a[505][505];
bool flag[505][505];
int Max,m,n;
void DFS(int x,int y,int temp)
{
    if(x<1||x>m||y<1||y>n)
        return;
    if(Max<temp)
        Max=temp;
    if(!flag[x][y])
    {
        flag[x][y]=true;
        DFS(x+1,y+1,temp+a[x][y]);
        flag[x][y]=false;
        
        flag[x][y]=true;
        DFS(x-1,y+1,temp+a[x][y]);
        flag[x][y]=false;
        
        flag[x][y]=true;
        DFS(x-1,y-1,temp+a[x][y]);
        flag[x][y]=false;
        
        flag[x][y]=true;
        DFS(x+1,y-1,temp+a[x][y]);
        flag[x][y]=false;
    }
}

int main()
{
    while(cin>>m>>n)
    {
        freopen("1.txt","r",stdin);
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                cin>>a[i][j];
        Max=0;
        memset(flag,false,sizeof(flag));
        DFS(0,0,0);
        cout<<Max<<endl;
    }
    return 0;
}

改过了,怎么还是不对?哪里出错了,,请各位路过的大佬指点下。
2018-04-09 19:00
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
先谢谢各位了。
2018-04-09 19:01
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 6楼 wmf2014
好的 谢谢
2018-04-10 22:41
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 9楼 九转星河
好的谢谢
2018-04-10 22:56
快速回复:一道oj的题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016674 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved