cdoj 15 Kastenlauf dfs

Kastenlauf

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/3

Description

Once every year, Jo and his friends want to visit the local fair in Erlangen, called Bergkirchweih. This year, they want to make a Kastenlauf (box run). They start at Jo’s home, and have one box (Kasten) of beer (with twenty bottles). As they are very thirsty, they drink one bottle of beer every 50 metres.

As the way from Jo’s home to the Bergkirchweih is pretty long, they need more beer than they have initially. Fortunately, there are stores selling beer on the way. When they visit a store, they can drop their empty bottles and buy new bottles, but their total number of full bottles will not be more than twenty (because they are too lazy to carry more than one full box).

You are given the coordinates of the stores, of Jo’s home and of the location of the Bergkirchweih. Write a program to determine whether Jo and his friends can happily reach the Bergkirchweih, or whether they will run out of beer on the way.

 

Input

Input starts with one line containing the number of test cases t (t≤50).

Each test case starts with one line, containing the number n of stores selling beer (with 0≤n≤100). The next n+2 lines cointain (in this order) the location of Jo’s home, of the stores, and of the Bergkirchweih. The location is given with two integer coordinates x and y, (both in meters, −32768≤x,y≤32767).

As Erlangen is a rectangularly laid out city, the distance between two locations is the difference of the first coordinate plus the difference of the second coordinate (also called Manhattan-Metric).

Output

For each test case print one line, containing either happy (if Jo and his friends can happily reach the Bergkirchweih), or sad (if they will run out of beer on the way).

Sample Input

2
2
0 0
1000 0
1000 1000
2000 1000
2
0 0
1000 0
2000 1000
2000 2000

Sample Output

happy
sad

HINT

题意

给你很多个点,要求每个点之间的距离小于等于1000就可以走过去,然后问你能否从起点走到终点

题解:

数据范围很小,直接爆搜

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 200000
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
struct node
{
    int x,y;
};
node a[200];
int n;
int flag[200];
void dfs(int x)
{
    if(flag[x])
        return;
    flag[x]=1;
    for(int i=1;i<=n;i++)
    {
        if(i==x)
            continue;
        if(abs(a[i].x-a[x].x)+abs(a[x].y-a[i].y)<=1000)
            dfs(i);
    }
}
void solve()
{
    memset(flag,0,sizeof(flag));
    memset(a,0,sizeof(a));
    scanf("%d",&n);
    n+=2;
    for(int i=1;i<=n;i++)
        a[i].x=read(),a[i].y=read();
    dfs(1);
    if(flag[n])
        cout<<"happy"<<endl;
    else
        cout<<"sad"<<endl;
}
int main()
{
    //test;
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        solve();
    }
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4544843.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞