A:

水题

代码:

 #include<cstdio>
#include<algorithm>
using namespace std; int she[]; int n,m,c; int main()
{
int ca=,a;
while(scanf("%d%d%d",&n,&m,&c)&&(n+m+c))
{
printf("Sequence %d\n",ca++);
for(int i=; i<=n; i++)
scanf("%d",&she[i]);
int ans=,ma=;
while(m--)
{
scanf("%d",&a);
ans+=she[a];
ma=max(ma,ans);
she[a]=-she[a];
}
if(ma>c)puts("Fuse was blown.");
else
{
puts("Fuse was not blown.");
printf("Maximal power consumption was %d amperes.\n",ma);
}
puts("");
}
return ;
}

B:

模拟;

代码:

 #include<cstdio>
#include<cstring>
#include<queue>
#define maxn 55
using namespace std; bool map[maxn][maxn]; int n,flag; struct node
{
int x,y;
}; queue<node>q; void move(char s)
{
node cur=q.back();
node next=q.front();
q.pop();
map[next.x][next.y]=;
if(s=='E')
{
next.x=cur.x;
next.y=cur.y+;
}
else if(s=='N')
{
next.x=cur.x-;
next.y=cur.y;
}
else if(s=='W')
{
next.x=cur.x;
next.y=cur.y-;
}
else if(s=='S')
{
next.x=cur.x+;
next.y=cur.y;
}
if(map[next.x][next.y]==)
{
flag=;
return;
}
else if(next.x<=||next.x>||next.y<=||next.y>)
{
flag=-;
return;
}
else
{
q.push(next);
map[next.x][next.y]=;
}
} char s[];
int main()
{
while(scanf("%d",&n)&&n)
{
node no;
flag=;
memset(map,,sizeof map);
while(!q.empty())q.pop();
for(int i=; i<=; i++)
{
no.x=;
no.y=i;
map[][i]=;
q.push(no);
}
scanf("%s",s);
int i;
for(i=; i<n; i++)
{
move(s[i]);
if(flag<)break;
}
if(flag==)printf("The worm successfully made all %d moves.\n",n);
else if(flag==-)printf("The worm ran off the board on move %d.\n",i+);
else if(flag==)printf("The worm ran into itself on move %d.\n",i+);
}
return ;
}

C:

我用的是bfs;

代码:

 #include<cstdio>
#include<queue>
#include<vector>
#define maxn 10005
using namespace std; vector<int>ve[maxn];
int di[maxn];
queue<int>q;
int main()
{
int n,x;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
ve[x].push_back(i);
}
int l=ve[].size();
for(int i=;i<l;i++)
{
q.push(ve[][i]);
di[ve[][i]]=;
}
while(!q.empty())
{
int cur=q.front();
q.pop();
l=ve[cur].size();
for(int i=;i<l;i++)
{
q.push(ve[cur][i]);
di[ve[cur][i]]=di[cur]+;
}
}
int ans=;
for(int i=;i<=n;i++)
if(ans<di[i])ans=di[i];
printf("%d\n",ans);
}

D:

dfs;

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

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

代码:

 #include<cstdio>
#include<vector>
#define maxn 23
using namespace std; vector<int>ve[maxn];
int n,s,m; bool dfs(int x,int t)
{
if(t==s)return x==n-;
int l=ve[x].size();
for(int i=;i<l;i++)
{
int a=ve[x][i];
if(~(t>>a)&)
if(dfs(a,t|(<<a)))
return ;
}
return ;
} int main()
{
int x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
ve[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
x--,y--;
ve[x].push_back(y);
ve[y].push_back(x);
}
s=(<<n)-;
if(dfs(,))puts("");
else puts("");
}
return ;
}

ACM俱乐部算法基础练习赛(1)的更多相关文章

  1. Levenberg-Marquardt算法基础知识

    Levenberg-Marquardt算法基础知识 (2013-01-07 16:56:17) 转载▼   什么是最优化?Levenberg-Marquardt算法是最优化算法中的一种.最优化是寻找使 ...

  2. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  3. ACM常用算法及练习(2)

    ACM常用算法及练习 知识类型 重要度 容易度 应掌握度 典型题 其他           数据结构(5) 链表 ★★☆ ★★★ ★★☆     栈 stack ★★★ ★★★ ★★★ HLoj120 ...

  4. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  5. 解读Raft(一 算法基础)

    最近工作中讨论到了Raft协议相关的一些问题,正好之前读过多次Raft协议的那paper,所以趁着讨论做一次总结整理. 我会将Raft协议拆成四个部分去总结: 算法基础 选举和日志复制 安全性 节点变 ...

  6. 腾讯2017年暑期实习生编程题【算法基础-字符移位】(C++,Python)

     算法基础-字符移位 时间限制:1秒 空间限制:32768K 题目: 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间. 你能帮帮小Q吗? ...

  7. 算法基础_递归_求杨辉三角第m行第n个数字

    问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好) ...

  8. ACM主要算法

    ACM主要算法ACM主要算法介绍 初期篇 一.基本算法(1)枚举(poj1753, poj2965)(2)贪心(poj1328, poj2109, poj2586)(3)递归和分治法(4)递推(5)构 ...

  9. 毕业设计预习:SM3密码杂凑算法基础学习

    SM3密码杂凑算法基础学习 术语与定义 1 比特串bit string 由0和1组成的二进制数字序列. 2 大端big-endian 数据在内存中的一种表示格式,规定左边为高有效位,右边为低有效位.数 ...

随机推荐

  1. javascript笔记08:javascript的if...else语句

    案例代码如下: <!DOCTYPE html> <html> <body> <p>点击这个按钮,获得基于时间的问候.</p> <but ...

  2. Java内存模型浅析

    JVM在执行java程序时会将它所管理的内存划分成若干个不同的数据区域.如图所示: 其中方法区和堆是所有线程共享的数据区,其他区域则是线程隔离的数据区. 这些区域的功能各有不同: 程序计数器:可以理解 ...

  3. ASP.NET Identity 用户注册相关设定

    此部分可以在 Web项目中的App_Start目录下的 IdentityConfig.cs 文件进行设置. 1.配置密码的验证逻辑 manager.PasswordValidator = new Pa ...

  4. Retrofit研究1

    以下为来自Github的retrofit的介绍.话说,翻译还真蛋疼,就这个花了快3个小时. Retrofit 一个在Android和Java上类型安全的HTTP客户端 介绍 Retrofit可以把你的 ...

  5. ios NSHashTable & NSMapTable

    在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...

  6. jsp 页面获取xml的内容

    <c:out value="${history.xml}" escapeXml="true" />

  7. redhat5.8无法进入图形界面

    解决办法: 删除/etc/X11/xorg.conf文件

  8. [Twisted] Test

    由于Twisted程序采用事件驱动,并使用Deferred来处理事件,使用Python unittest的写测试并不容易.因此, Twisted拓展了unitest,并使用命令行工具来运行测试.这些组 ...

  9. 绑定本地Service并与之通信

    绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...

  10. 使用link在两个容器之间建立连接(mysql)

    在使用Docker的时候我们会常常碰到这么一种应用,就是我需要两个或多个容器,其中某些容器需要使用另外一些容器提供的服务.比如这么一种情况:我们需要一个容器来提供mysql的数据库服务,而另外两个容器 ...