当前位置:网站首页>Acwing2013. three lines
Acwing2013. three lines
2022-06-25 06:36:00 【AlwaysDayOne】

sample input :
6
1 7
0 0
1 2
2 0
1 4
3 4
sample output :
1
Sample explanation
y=0,x=1,y=4 It can cover the positions of all cows .
Answer key (DFS)
First select a point , He has two options , use x perhaps y Line of direction ,
After selecting the direction , Delete all points in this direction
Then randomly select one of the undeleted points , Repartition x or y Direction , Repeat the first 2 Step
Then repeat step three
After the first four steps , The deleted points are crossed by three lines , At this point, the result can be obtained by judging whether there is any left
If there are still points left, it means that this scheme is not tenable , Then you can backtrack and rotate in different directions
So there are 8 Select the direction combination of lines , Here's the picture

Code thinking :
use dfs Recursive three-tier implementation
Use one Struct To determine whether coordinates and record points are deleted
Use an array to record all points
To prevent traversing the array every time, judge whether the point is deleted , The starting point of each layer traversal is the new line decision point , All the previous points have been deleted ( That is, it has been wired through ), The point after the start point contains both deleted and undeleted points in the array
#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++){
// to flash back
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++){
// to flash back
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;
}
边栏推荐
- Global and China financial guarantee marketing strategy and channel dynamic construction report 2022
- 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
- Sophomores majoring in mechanics build a manipulator by hand -- full of compromise
- Arm instructions and others
- アルマ / 炼金妹
- PHP and WMI – explore windows with PHP
- How to deploy locally developed SAP ui5 applications to ABAP servers
- After unplugging the network cable, does the original TCP connection still exist?
- A + B Again
- How to realize the stable output of 3.3v/3.6v (1.2-5v) voltage of lithium battery by using the voltage rise and fall chip cs5517
猜你喜欢
![[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]](/img/a1/0cb61368cb1d0817d74781084a4466.jpg)
[short time energy] short time energy of speech signal based on MATLAB [including Matlab source code 1719]

cos(a+b)=cosa*cosb-sina*sinb的推导过程

Microsoft issued a document to celebrate Net 20th anniversary!

ACWING2013. 三条线

JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
![[200 opencv routines of youcans] 104 Motion blur degradation model](/img/a9/8841ffc8bd3c486bc4011a1a84ff45.jpg)
[200 opencv routines of youcans] 104 Motion blur degradation model

Socket, network model notes
![[network security] sharing of experience and ideas of an emergency battle](/img/9b/dd6e47ad7610eefd2b8c93602d6a7e.jpg)
[network security] sharing of experience and ideas of an emergency battle

Gb28181 protocol -- timing

System dilemma and software complexity: Why are our systems so complex?
随机推荐
How do I turn off word wrap in iterm2- How to turn off word wrap in iTerm2?
Global and Chinese kaolin market operation scale and investment development proposal report 2022
China rehabilitation hospital industry operation benefit analysis and operation situation investigation report 2022
Global and China chemical mechanical polishing abrasive materials market demand outlook and investment scale forecast report 2022 Edition
Ht81293 built in adaptive dynamic boost 20W mono class D power amplifier IC solution
Wan Yin revealed that he was rejected by MIT in this way: "the department doesn't like you". He confronted the principal and realized
Viewing Chinese science and technology from the Winter Olympics (V): the Internet of things
VMware virtual machine prompt: the virtual device ide1:0 cannot be connected because there is no corresponding device on the host.
How do I check swift if two arrays contain the same elements, regardless of the order in which they appear?
Count the grid
Wechat applet simply realizes chat room function
@Principle of preauthorize permission control
Ht513 I2S input 2.8W mono class D audio power amplifier IC
Cve-2022-23131 - bypass SAML SSO authentication
Explain @builder usage
How to open an account online? Is it safe to open an account online?
How to deploy locally developed SAP ui5 applications to ABAP servers
From file system to distributed file system
Zero foundation wants to learn web security, how to get started?
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance