ACM俱乐部算法基础练习赛(1)

A:

水题

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int she[25];
 6 
 7 int n,m,c;
 8 
 9 int main()
10 {
11     int ca=1,a;
12     while(scanf("%d%d%d",&n,&m,&c)&&(n+m+c))
13     {
14         printf("Sequence %d\n",ca++);
15         for(int i=1; i<=n; i++)
16             scanf("%d",&she[i]);
17         int ans=0,ma=0;
18         while(m--)
19         {
20             scanf("%d",&a);
21             ans+=she[a];
22             ma=max(ma,ans);
23             she[a]=-she[a];
24         }
25         if(ma>c)puts("Fuse was blown.");
26         else
27         {
28             puts("Fuse was not blown.");
29             printf("Maximal power consumption was %d amperes.\n",ma);
30         }
31         puts("");
32     }
33     return 0;
34 }
View Code

B:

模拟;

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #define maxn 55
 5 using namespace std;
 6 
 7 bool map[maxn][maxn];
 8 
 9 int n,flag;
10 
11 struct node
12 {
13     int x,y;
14 };
15 
16 queue<node>q;
17 
18 void move(char s)
19 {
20     node cur=q.back();
21     node next=q.front();
22     q.pop();
23     map[next.x][next.y]=0;
24     if(s=='E')
25     {
26         next.x=cur.x;
27         next.y=cur.y+1;
28     }
29     else if(s=='N')
30     {
31         next.x=cur.x-1;
32         next.y=cur.y;
33     }
34     else if(s=='W')
35     {
36         next.x=cur.x;
37         next.y=cur.y-1;
38     }
39     else if(s=='S')
40     {
41         next.x=cur.x+1;
42         next.y=cur.y;
43     }
44     if(map[next.x][next.y]==1)
45     {
46         flag=0;
47         return;
48     }
49     else if(next.x<=0||next.x>50||next.y<=0||next.y>50)
50     {
51         flag=-1;
52         return;
53     }
54     else
55     {
56         q.push(next);
57         map[next.x][next.y]=1;
58     }
59 }
60 
61 
62 char s[105];
63 int main()
64 {
65     while(scanf("%d",&n)&&n)
66     {
67         node no;
68         flag=1;
69         memset(map,0,sizeof map);
70         while(!q.empty())q.pop();
71         for(int i=11; i<=30; i++)
72         {
73             no.x=25;
74             no.y=i;
75             map[25][i]=1;
76             q.push(no);
77         }
78         scanf("%s",s);
79         int i;
80         for(i=0; i<n; i++)
81         {
82             move(s[i]);
83             if(flag<1)break;
84         }
85         if(flag==1)printf("The worm successfully made all %d moves.\n",n);
86         else if(flag==-1)printf("The worm ran off the board on move %d.\n",i+1);
87         else if(flag==0)printf("The worm ran into itself on move %d.\n",i+1);
88     }
89     return 0;
90 }
View Code

C:

我用的是bfs;

代码:

 1 #include<cstdio>
 2 #include<queue>
 3 #include<vector>
 4 #define maxn 10005
 5 using namespace std;
 6 
 7 vector<int>ve[maxn];
 8 int di[maxn];
 9 queue<int>q;
10 int main()
11 {
12     int n,x;
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++)
15     {
16         scanf("%d",&x);
17         ve[x].push_back(i);
18     }
19     int l=ve[0].size();
20     for(int i=0;i<l;i++)
21     {
22         q.push(ve[0][i]);
23         di[ve[0][i]]=0;
24     }
25     while(!q.empty())
26     {
27         int cur=q.front();
28         q.pop();
29         l=ve[cur].size();
30         for(int i=0;i<l;i++)
31         {
32             q.push(ve[cur][i]);
33             di[ve[cur][i]]=di[cur]+1;
34         }
35     }
36     int ans=0;
37     for(int i=1;i<=n;i++)
38         if(ans<di[i])ans=di[i];
39     printf("%d\n",ans);
40 }
View Code

 D:

dfs;

我先用一个表来存,一直都是T;

没想到用状态压缩就可以过了;

代码:

 1 #include<cstdio>
 2 #include<vector>
 3 #define maxn 23
 4 using namespace std;
 5 
 6 vector<int>ve[maxn];
 7 int n,s,m;
 8 
 9 bool dfs(int x,int t)
10 {
11     if(t==s)return x==n-1;
12     int l=ve[x].size();
13     for(int i=0;i<l;i++)
14     {
15         int a=ve[x][i];
16         if(~(t>>a)&1)
17             if(dfs(a,t|(1<<a)))
18                 return 1;
19     }
20     return 0;
21 }
22 
23 int main()
24 {
25     int x,y;
26     while(scanf("%d%d",&n,&m)!=EOF)
27     {
28         for(int i=0;i<n;i++)
29             ve[i].clear();
30         for(int i=0;i<m;i++)
31         {
32             scanf("%d%d",&x,&y);
33             x--,y--;
34             ve[x].push_back(y);
35             ve[y].push_back(x);
36         }
37         s=(1<<n)-1;
38         if(dfs(0,1))puts("1");
39         else puts("0");
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/yours1103/p/3391415.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值