码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ1004

时间:2015-10-26 00:16:48      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

#include<bits/stdc++.h>
using namespace std;
int Map[106][106];
int Vis[106][106];
int Num[106][106];
int T;
const int step[2][2] = {1,0,0,1};

void Init()
{
    memset(Map,0,sizeof(Map));
    memset(Vis,0,sizeof(Vis));
    memset(Num,0,sizeof(Num));
}

struct Node {
    int x, y;
    Node(int _x = 0, int _y = 0) :    x(_x), y(_y) {}
};

int BFS(int n)
{
    Num[0][0] = Map[0][0];
    Node Begin(0,0);
    queue<Node> Q;
    Q.push(Begin);
    while(!Q.empty())
    {
        Node c = Q.front();
        Q.pop();
        int s = Num[c.x][c.y];
        for(int i = 0; i < 2; ++i)
        {
            int xx = c.x + step[i][0];
            int yy = c.y + step[i][1];
            //cout << Num[xx] << "  " << Num[yy] <<endl;
            if(xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
            if(s + Map[xx][yy] > Num[xx][yy])
            {
                //cout << 1 <<endl;
                Num[xx][yy] = s+Map[xx][yy];
                Q.push(Node(xx,yy));
            }
        }
    }
    return Num[n-1][n-1];
}

void RedMap(int n)
{
    int bx = 0, by = 0;
    for(int i = 1; i <= n; ++i)
    {
        int xx = bx, yy = by;
        for(int j = 1; j <= i; ++j)
        {
            cin >> Map[xx][yy];
            //cout << xx<< "  " << yy << endl;
            xx--, yy++;
        }
        bx++;
    }
    bx --, by ++;
    for(int i = 1; i <= n-1; ++i)
    {
        int xx = bx, yy = by;
        for(int j = 1; j <= n-i; ++j)
        {
            cin >> Map[xx][yy];
            //cout << xx<<"  " << yy << endl;
            xx--, yy++;
        }
        by++;
    }
}
int main()
{
    int t, n;
    cin >> t;
    for(int kase = 1; kase <= t; ++kase)
    {
        cin >> n;
        Init();
        RedMap(n);
        printf("Case %d: %d\n",kase, BFS(n));
        /*for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < n; ++j)
                cout << Num[i][j] << " ";
            cout << endl;
        }*/
    }
}

 

裸地BFS, 就是输出从头到尾一条和最大的路径

 

LightOJ1004

标签:

原文地址:http://www.cnblogs.com/aoxuets/p/4909800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!