暴力--Aizu1379:Parallel Lines

2024-03-05 09:18
文章标签 暴力 parallel lines aizu1379

本文主要是介绍暴力--Aizu1379:Parallel Lines,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

平行直线

题意:给出一些点,这些点两两相连成一条直线,问最多能连成多少条直线。

思路:暴力出奇迹!!记得当时比赛做这道题的时候一直依赖于板子,结果却限制了自己的思路,这得改。dfs直接暴力,但是需要将已经走过的点标记一下,用一个循环跳过已经标记的点减少dfs次数,不然得不出正确的结果,因为会出现如下的连线结果(左图),而正确的连线方式应该如右图。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
#define MIN(a,b) a<b ? a : b
#define MAX(a,b) a>b ? a : b//确实要比C++自带的max函数要快
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
const int maxn = 20;
const int MOD = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> P;
struct Point {int x,y;
} p[maxn];
struct Line {int x,y;
} l[maxn];
int m,ans,cnt,vis[maxn];int upDate() {//遍历任意两条边之间是否平行,更新最大的结果int res = 0;for(int i = 0; i<cnt; i++)for(int j = i+1; j<cnt; j++) {if(l[i].x*l[j].y == l[i].y*l[j].x){//注意有斜率不存在情况,所以要对等式做一下变形res++;}}return res;
}void dfs(int now) {while(vis[now])//减少dfs次数,同样也是避免出现之前说的情况now++;if(now == m) {ans = MAX(upDate(),ans);return;}vis[now] = 1;for(int i = 0; i<m; i++) {if(!vis[i] && cnt != i) {l[cnt].x = p[now].x - p[i].x;l[cnt].y = p[now].y - p[i].y;vis[i] = 1;cnt++;dfs(now+1);//枚举的是每种连接方式下的点的个数cnt--;vis[i] = 0;}}vis[now] = 0;return;
}int main() {memset(vis,0,sizeof(vis));scanf("%d",&m);for(int i = 0; i<m; i++) {scanf("%d%d",&p[i].x,&p[i].y);}ans = 0;cnt = 0;dfs(0);printf("%d\n",ans);return 0;
}

 

这篇关于暴力--Aizu1379:Parallel Lines的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/776006

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

10125-Sumsets【暴力】

利用n^2的时间枚举所有a[i] + a[j] 利用n^2的时间枚举所有a[i] - a[j] 之后利用n^2时间一个一个找a[i] - a[j]的值是否存在于a[i] + a[j]中 找的时候需要二分查找 另外一点就是注意long long的范围以及四个数是集合内不同的四个元素 15222638 10125 Sumsets Accepted C++ 0.449 2015-03-

10730-Antiarithmetic?【暴力枚举】

水题 求一个序列是否存在3个数按顺序构成等差数列 直接枚举等差数列的差值 时间复杂度降到 n * n / 3 开pos数组记录每个值得为之 楷vis数组记录目前i是否出现过 强行AC 15221397 10730 Antiarithmetic? Accepted C++ 0.035 2015-03-26 12:09:56 #include<cstdio>#include

HLJUOJ1125(暴力三点一线)

两点确定一条直线 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 19   Solved: 11 [ Submit][ Status][ Web Board] Description  给一个15000 * 15000 的区域, 坐标都是整数. 其中有N个点,N <= 770.问总共有多少个3点共线的组合.并按升序(点的ID)输出

HLJUOJ1117(暴力模拟)

八数码 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 109   Solved: 19 [ Submit][ Status][ Web Board] Description 给定一个8数码的初始状态,然后给出一系列对8数码的操作,求其最终状态. Input 第一行t,代表样例个数。 每组数据前三行各三个数,代表八数

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/probl

UVA10010(八方向暴力枚举)

Where's Waldorf? Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18656 Description Where's Waldo

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There

CF#278 (Div. 2) A.(暴力枚举)

A. Giga Tower time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/A Giga To

CF#284 (Div. 2) B.(暴力)

题目链接:http://codeforces.com/contest/499/problem/B 解题思路: 开一个is变量记录每组单词最后应该输出哪个。最后每次把原来的数组暴力扫一遍即可。 完整代码: #include <functional>#include <algorithm>#include <iostream>#include <fstream>#inc