Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Hoj 2064 Journey to Tibet

$
0
0

今天HIT冬训第一次阶段赛。这一道题我一直A不掉。明明很简单,思路也对。就是WA后来发现时是 max = i;/出了错。/如果写做:max = i+1,然后在最后输出输出max是错误的。因为可能max = 0。伤不起。。。就这样断送了我的AC之路。

注意:关键在于读懂题意,是两村庄之间的距离不大于30才可以到达。

题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2064

先写一发非递归版DFS:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>

using namespace std;

struct Village
{
    int x;
    int y;
    int num;
};
Village v[1005];
int visited[1005];
int ans[1005];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n;
    int x,y,num;
    while(scanf("%d",&n)!=EOF && n!=0)
    {
        stack<Village> st;
        int max = 0;
        int sum = 0;
        for(int i=0; i<n;i++)
        {
            scanf(" %d %d %d",&v[i].x,&v[i].y,&v[i].num);
        }
        for(int i=0;i<n;i++)
        {
            memset(visited,0,sizeof(visited));
            memset(ans,0,sizeof(ans));
            visited[i] = 1;
            st.push(v[i]);
            ans[i] += v[i].num;

            while(!st.empty())
            {
                Village temp = st.top();
                int flag = 0;
                for(int j=0;j<n;j++)
                {
                    if(visited[j] == 0 && (temp.x - v[j].x)*(temp.x - v[j].x) + (temp.y - v[j].y)*(temp.y - v[j].y)<=900)
                    {
                        visited[j] = 1;
                        ans[i] += v[j].num;
                        st.push(v[j]);
                        flag = 1;
                    }
                }
                if(flag == 0)
                {
                    st.pop();
                }
            }
            if(ans[i]>sum)
            {
                sum = ans[i];
                max = i;//如果写做:max = i+1,然后在最后输出输出max是错误的。
            }
        }
        printf("%d %d\n",max+1,sum);

    }

    return 0;
}

最后写一发本题的DFS的递归版的,就当练习逻辑思维了(递归版细节要格外注意啊!!)

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>

using namespace std;

struct Village
{
    int x;
    int y;
    int num;
};
Village v[1005];
int visited[1005];
int ans[1005];
int n;
int max = 0;
int sum = 0;

int dfs(int k,int sum)
{
    int max = sum;
    int tempSum = sum;
    for(int i=0; i<n; i++)
    {
        tempSum = sum;
        if(visited[i] == 0 && (v[k].x-v[i].x)*(v[k].x-v[i].x)+(v[k].y-v[i].y)*(v[k].y-v[i].y)<=900)
        {
            visited[i] = 1;
            tempSum = dfs(i,tempSum + v[i].num);
            if(tempSum>max)
            {
                max = tempSum;
            }
            visited[i] = 0;
        }
    }
    return max;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif

    int sum;
    int maxi;
    int maxSum;
    while(scanf("%d",&n)!=EOF && n!=0)
    {
        sum = 0;
        maxi = maxSum = 0;
        memset(visited,0,sizeof(visited));
        for(int i=0; i<n; i++)
        {
            scanf(" %d %d %d",&v[i].x,&v[i].y,&v[i].num);
        }
        for(int i=0; i<n; i++)
        {
            if(visited[i] == 0)
            {
                visited[i] = 1;
                sum = dfs(i,v[i].num);
                if(sum>maxSum)
                {
                    maxSum = sum;
                    maxi = i;
                }
                visited[i] = 0;
            }
        }
        printf("%d %d\n",maxi+1,maxSum);
    }

    return 0;
}


作者:niuox 发表于2013-1-27 22:22:02 原文链接
阅读:29 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>