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();
    }
}
时间: 2024-11-08 03:53:16

cdoj 15 Kastenlauf dfs的相关文章

BZOJ_1016_[JSOI2008]_最小生成树计数_(dfs+乘法原理)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1016 给出一张图,其中具有相同权值的边的数目不超过10,求最小生成树的个数. 分析 生成树的计数有一个什么什么算法... 我真的企图研究了...但是智商捉急的我实在看不懂论文... 所以最后还是写了暴力... 当然暴力也要靠正确的姿势的. 首先来看一个结论: 同一张图的所有最小生成树中,边权值相同的边的数目是一定的. 也就是说,假如某一张图的某一棵最小生成树由边权值为1,1,2,2,2,3的

HDU1010 DFS+剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 110290    Accepted Submission(s): 29967 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

【noi 2.5_1789】算24(dfs)

最开始我想的是全排列+枚举符号和括号的方法,但是我自己倒腾了很久还是打不对,只好向他人请教.正解很机智——直接随意将几个数“捆绑”在一起,值存在其中一个数上,其他数标记不可再选,直到只剩下一个数,再判断这个数是否为24. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 using name

hdu 1760 DFS+博弈

0代表可放 1带表不能放 每次放一个2*2的方块 不能放者败如果先手必胜则输出Yes 必胜态:从当前状态所能到达的状态中存在一个必败态必败态:从当前状态所能达到的状态全部是必胜态 Sample Input4 400000000000000004 40000001001000000 Sample OutputYesNo 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include

nyoj27-水池数目 (求连通块数目)【dfs】

http://acm.nyist.net/JudgeOnline/problem.php?pid=27 水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池. 输入 第一行输入一个整数N,表示共有N组测试数据每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0

hdu 1515 dfs

一道不错的搜索题 题意:告诉你两个字符串a和b,要求对a进行栈的操作而产生b串,输出操作的顺序,如果有多组输出就按字典序输出. Sample Input madam adamm bahama bahama long short eric rice Sample Output [ i i i i o o o i o o i i i i o o o o i o i i o i o i o i o o i i o i o i o o i o ] [ i o i i i o o i i o o o i

DFS/四色定理/poj 1129 Channel Allocation

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n; 5 int a[30][30]; 6 int c[30]; 7 8 bool pd(int x,int color) 9 { 10 for (int i=1;i<x;i++) 11 if (a[x][i]==1 && c[i]==color) return false; 12 return true; 13 } 14 1

图算法(一)——基本图算法(BFS,DFS及其应用)(2)

2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和完成时间 DFS后原图的前驱子图构成一个深度优先森林 1 #include<iostream> 2 using namespace std; 3 #define NIL -1 4 int g[1000][1000]; 5 int n; 6 struct Node 7 { 8 int color;

POJ-3134-Power Calculus(迭代加深DFS)

Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, -, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications.