洛谷 P2196 挖地雷 题解

每日一题 day5 打卡

Analysis

裸搜就好了的一道水题

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 20+10
 6 using namespace std;
 7 inline int read() 
 8 {
 9     int x=0;
10     bool f=1;
11     char c=getchar();
12     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
13     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
14     if(f) return x;
15     return 0-x;
16 }
17 inline void write(int x)
18 {
19     if(x<0){putchar('-');x=-x;}
20     if(x>9)write(x/10);
21     putchar(x%10+'0');
22 }
23 int n,cnt,ans,len;
24 int bomb[maxn],map[maxn][maxn],book[maxn],path[maxn],ans_path[maxn];
25 inline bool check(int x)
26 {
27     for(int i=x+1;i<=n;i++)
28     {
29         if(map[x][i]==1&&book[i]==0) return true;
30     }    
31     return false;
32 }
33 inline void dfs(int step,int now_i,int sum)
34 {
35     if(check(now_i)==false)
36     {
37         if(sum>ans)
38         {
39             for(int i=1;i<=step;i++) ans_path[i]=path[i];
40             len=step;
41             ans=sum;
42         }
43     }
44     for(int i=now_i+1;i<=n;i++)
45     {
46         if(map[now_i][i]==1&&book[i]==0)
47         {
48             book[i]=0;
49             path[step+1]=i;
50             dfs(step+1,i,sum+bomb[i]);
51             book[i]=1;
52         }
53     }
54 }
55 int main()
56 {
57     n=read();
58     cnt=n-1;
59     for(int i=1;i<=n;i++) bomb[i]=read();
60     for(int i=1;i<n;i++)
61         for(int j=i+1;j<=n;j++)
62             map[i][j]=read();
63     for(int i=1;i<=n;i++)
64     {
65         book[i]=1;
66         path[1]=i;
67         dfs(1,i,bomb[i]);
68         memset(book,0,sizeof(book));
69     }
70     for(int i=1;i<=len;i++) 
71     {
72         write(ans_path[i]);
73         printf(" ");    
74     }
75     printf("
");
76     write(ans);
77     return 0;
78 }
79 /*
80 6
81 5 10 20 5 4 5
82 1 0 1 0 0
83 0 1 0 0
84 1 0 0
85 1 1
86 1
87 
88 ans:
89 3 4 5 6
90 34
91 */

请各位大佬斧正(反正我不认识斧正是什么意思)

原文地址:https://www.cnblogs.com/handsome-zyc/p/11437334.html