当前位置:网站首页>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;
}
边栏推荐
- Is it safe to open a stock account on the Internet in Beijing?
- How to chain multiple different InputStreams into one InputStream
- Advantages and disadvantages of using SNMP and WMI polling
- Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
- The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
- Notes on dashboard & kuboard installation in kubernetes cluster
- 3-7sql injection website instance step 3: attack type and attack strategy
- BigDecimal. Summary of setscale usage
- 十大券商公司哪个佣金最低,最安全可靠?有知道的吗
- Handling skills of SQL optimization (2)
猜你喜欢

アルマ / 炼金妹

Cs4344/ht5010 stereo d/a digital to analog converter
![[v2.0] automatic update system based on motion step API (support disconnection reconnection and data compensation)](/img/73/2ec957d58616d692e571a70826787f.jpg)
[v2.0] automatic update system based on motion step API (support disconnection reconnection and data compensation)

Methods for obtaining some information of equipment

Single lithium battery 3.7V power supply 2x12w stereo boost audio power amplifier IC combination solution

Cve-2022-23131 - bypass SAML SSO authentication

BGP - basic concept

How to deploy locally developed SAP ui5 applications to ABAP servers

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

Wechat applet authorization login + mobile phone sending verification code +jwt verification interface (laravel8+php)
随机推荐
Wechat applet simply realizes chat room function
Single lithium battery 3.7V power supply 2x12w stereo boost audio power amplifier IC combination solution
Observation configuring wmic
Analysis report on production and sales demand and sales prospect of global and Chinese phosphating solution Market 2022-2028
How do I check swift if two arrays contain the same elements, regardless of the order in which they appear?
Introduction to sap ui5 tools
Comparison test of mono 120W high power class D power amplifier chip cs8683-tpa3116
2022 AI trend 8 forecast!
[200 opencv routines of youcans] 104 Motion blur degradation model
Is the number of indexes in a table the more the better?
Global and China financial guarantee marketing strategy and channel dynamic construction report 2022
Cs8126t 3.1w mono ultra low EMI unfiltered class D audio power amplifier IC
Research Report on investment share and application prospect of 1,3-propanediol (PDO) industry in the world and China 2022
The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
[kicad image] download and installation
Preliminary practice of niuke.com (summary)
How to deploy locally developed SAP ui5 applications to ABAP servers
JSON. toJSONString(object, SerializerFeature.WriteMapNullValue); Second parameter action
We cannot activate inspection type for article master in transaction code MM41?