HDU4324——DFS——Triangle LOVE

http://acm.hdu.edu.cn/showproblem.php?pid=4324

/*
搜索一个三角形,用vector+vis来减少复杂度
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-25 10:42:01
* File Name     :E.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 2000 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

char  mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int n;
vector<int> G[MAXN];
bool dfs(int x, int y, int step, int ex, int ey)
{
    if(step == 2 && y == ex) return true;
    if(vis[x][y]) return false;
    vis[x][y] = 1;
    if(step >= 2) return false;
    for(int i = 0; i < G[y].size(); i++){
        int v = G[y][i];
       // printf("%d %d\n", y, v);
           if(!dfs(y, v, step + 1, ex, ey));
           else return true;
    }
    return false;
}

bool solve()
{
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < G[i].size(); j++){
            int v = G[i][j];
            if(!vis[i][v])
                if(dfs(i, v, 0, i, v)){
                    return true;
                }
            }
    return false;
}

int main(){
    int T;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas++){
        memset(vis, 0, sizeof(vis));
        scanf("%d", &n);
        for(int i = 0; i <= n; i++)
            G[i].clear();
        for(int i = 1; i <= n; i++)
            scanf("%s", mp[i] + 1);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(mp[i][j] == ‘1‘){
                    G[i].push_back(j);
                }
            }
        }
        printf("Case #%d: ", cas);
        if(solve()) puts("Yes");
        else puts("No");
    }
    return 0;
}

  

时间: 2024-10-09 17:38:03

HDU4324——DFS——Triangle LOVE的相关文章

hdu4324

http://acm.hdu.edu.cn/showproblem.php?pid=4324 Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility th

LeetCode: Triangle 解题报告

Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle[     [2],    [3,4],   [6,5,7],  [4,1,8,3]]The minimum path sum from top to

HDU4324 Triangle LOVE【拓扑排序】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2683    Accepted Submission(s): 1084 Problem Description Recently, scientists find that there is love between any of two people. Fo

Leetcode dfs&amp;dp Triangle

Triangle Total Accepted: 17536 Total Submissions: 65508My Submissions Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4],

HDU4324 Triangle LOVE 【拓扑排序】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2455    Accepted Submission(s): 997 Problem Description Recently, scientists find that there is love between any of two people. For

HDU4324 - Triangle LOVE 拓补排序

HDU4324 - Triangle LOVE : http://acm.showproblemhdu.edu.cn/.php?pid=4324 标准的拓补排序,上代码 : #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 2010; int In[MAXN]; char Map[MAXN][MAXN]; int T,N; int

拓扑排序/DFS HDOJ 4324 Triangle LOVE

题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记,否则超时.看是否存在两点路程只差等于2,如果存在,则说明有上述的三角环.其他做法. 收获:DFS搜索一定要用vis数组啊,否则很容易超时的 代码(拓扑排序): /************************************************ * Author :Running_T

【HDOJ】1362 The Bermuda Triangle

1. 题目描述给定几个三角形拼成一个百慕大三角形. 2. 基本思路基本思路肯定是搜索,关键点是剪枝.(1) 若存在长度为$l$的边,则一定可以拼成长度为$k \cdot l$的三角形,则可拼成长度为$k \cdot l$的百慕大三角形:(2) 长度超过百慕大三角形的边长的三角形没有任何价值:(3) 百慕大三角形中每个正三角形可以作为正多边形的顶点,倒三角形可以作为倒正三角形的顶点.因此,可以将每个三角形映射到二维坐标,高度为$y$,倒三角形的$x$坐标为偶数,正三角形的$x$坐标为奇数.对于每个

Triangle &lt;leetcode&gt;

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i