当前位置:网站首页>ACWING2013. 三条线
ACWING2013. 三条线
2022-06-25 06:32:00 【AlwaysDayOne】

输入样例:
6
1 7
0 0
1 2
2 0
1 4
3 4
输出样例:
1
样例解释
y=0,x=1,y=4 可满足将所有奶牛位置覆盖。
题解(DFS)
首先选择一个点,他有两种选择,用x或者y方向的线,
选择方向后,将此方向的所有点给删除
然后将未删除的点中随机选一个,再区分x或y方向,重复第2步
然后重复第三步
前四步完成后,删除的点就分别有三条线穿过了,此时判断是否还有剩下的即可得出结果
若还有剩下点说明此方案不成立,则回溯旋转不同方向
如此一共有8种选择线的方向组合,如下图

代码思路:
用dfs递归三层来实现
用一个Struct来装坐标和记录点是否删除的状态
用数组来记录所有点
为了防止每次都遍历数组判断点是否删除,每次一层遍历的起点是新的线决定点,前面所有的点都被删除了(也就是已经有线穿过),起点之后的点在数组中包含了删除与未删除的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 50010;
struct Node{
int x,y,status;
}node[N];
int n;
int dfs(int index, int level){
if(level == 0){
if(index != n) return 0;
else return 1;
}
int next = n;
node[index].status = 1;
// try x line
for(int i = index+1;i<n;i++){
if(node[i].x == node[index].x){
node[i].status = 1;
}else if(node[i].status){
continue;
}else{
next = i;
break;
}
}
for(int i = next+1;i<n;i++){
if(node[i].x == node[index].x){
node[i].status = 1;
}else if(node[i].status){
continue;
}
}
int ret = dfs(next,level-1);
for(int i = index+1;i<n;i++){
// 回溯
if(node[i].x == node[index].x){
node[i].status = 0;
}
}
// try y line
next = n;
for(int i = index+1;i<n;i++){
if(node[i].y == node[index].y){
node[i].status = 1;
}else if(node[i].status){
continue;
}else{
next = i;
break;
}
}
for(int i = next+1;i<n;i++){
if(node[i].y == node[index].y){
node[i].status = 1;
}else if(node[i].status){
continue;
}
}
ret = dfs(next,level-1) or ret;
for(int i = index;i<n;i++){
// 回溯
if(node[i].y == node[index].y){
node[i].status = 0;
}
}
return ret;
}
int main(){
cin>>n;
for(int i = 0;i<n;i++){
cin>>node[i].x>>node[i].y;
node[i].status = 0;
}
cout<<dfs(0,3)<<endl;
return 0;
}
边栏推荐
- What elements are indispensable for the development of the character? What are the stages
- Global and Chinese medical protective clothing market supply and demand research and investment value proposal report 2022-2028
- Analysis report on global and Chinese pharmaceutical excipients industry competition and marketing model 2022-2028
- [open source sharing] deeply study KVM, CEPH, fuse features, including open source projects, code cases, articles, videos, architecture brain maps, etc
- Digitalization, transformation?
- JS to determine whether an element exists in the array (four methods)
- 2022 AI trend 8 forecast!
- What is the slice flag bit
- 证券如何在线开户?在线开户是安全么?
- Analysis report on production and sales demand and sales prospect of global and Chinese phosphating solution Market 2022-2028
猜你喜欢

How to realize hierarchical management of application and hardware in embedded projects

Zero foundation wants to learn web security, how to get started?

How to deploy locally developed SAP ui5 applications to ABAP servers

2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w

2022 AI trend 8 forecast!

DNS domain name system
![[hand torn STL] Stack & queue](/img/db/d05c52f8e3fb0aade51460e86cf623.jpg)
[hand torn STL] Stack & queue

Gb28181 protocol -- timing

JS to determine whether an element exists in the array (four methods)

How to use asemi FET 7n80 and how to use 7n80
随机推荐
Global and China chemical mechanical polishing abrasive materials market demand outlook and investment scale forecast report 2022 Edition
The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
Uname command – displays system information
Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
How to deploy locally developed SAP ui5 applications to ABAP servers
[hand torn STL] Stack & queue
Vegetables sklearn - xgboost (2)
General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
Huawei machine test question: splicing URL
[network security] sharing of experience and ideas of an emergency battle
How to chain multiple different InputStreams into one InputStream
PHP and WMI – explore windows with PHP
[speech discrimination] discrimination of speech signals based on MATLAB double threshold method [including Matlab source code 1720]
After five years of software testing in ByteDance, I was dismissed in December to remind my brother of paddling
JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
Optimal Parking
[core content and derivation] the mystery of human memory system may be just like this
TFTP command – uploading and downloading files
SAP QM executes the transaction code qp01, and the system reports an error -material type food is not defined for task list type Q-
Wireless industrial Internet of things data monitoring terminal