cdoj 15 Kastenlauf dfs

来源:互联网 发布:mac 风暴英雄 鼠标 编辑:程序博客网 时间:2024/05/17 00:07

题目链接:

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

题意:

n+2个点,要求每个点之间的距离小于等于1000就可以走过去,然后问你能否从1走到n+2

题解:

dfs

代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst 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;}//////////////////////////////////////////////////////////////////////////const int maxn = 1e5+10;struct node{    int x,y;}a[105];int n,f,vis[105];void dfs(int u){    if(vis[u]) return ;    vis[u] = 1;    for(int i=1; i<=n; i++){        if(i==u)            continue;        if(abs(a[i].x-a[u].x) + abs(a[i].y-a[u].y) <= 1000)            dfs(i);    }}int main(){    int T=read();    while(T--){        MS(vis);        f = 1;        n = read();         n += 2;        for(int i=1; i<=n; i++)            a[i].x=read(),a[i].y=read();        dfs(1);        if(vis[n]) puts("happy");        else puts("sad");    }    return 0;}
0 0