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. maven跳过单元测试

    24.跳过单元测试 <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>mav ...

  2. 【亲测可用】MySQL 4.1迁移到MySQL 5.0版本的中文乱码问题解决

    核心:先导出结构,再导出数据. 结构最好使用myphpadmin导出.使用mysqldump导出的可能会导致一些问题. ---------------以下为转载---------------- 在生成 ...

  3. NSObject中的isa到底是个什么?

    首先看一下NSObject的定义: @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } 官方解释:Eve ...

  4. jqery基础知识

    选择器按属性,子元素,表单匹配元素 <!doctype html> <html lang="en"> <head> <meta chars ...

  5. 用 C# 如何判断数据库中是否存在一个值

    选定一个列,比如用户编号列 //欲插入的用户编号string ll_userID="xxxxxxxx"; //查询此编号是否存在SqlCommand mycmd = new Sql ...

  6. Unity3D 之UGUI 文本框和编辑框

    这里来讲解一下unity3D自带的UI功能,自带的UI也叫UGUI功能非常的强大,比起NGUI,更加的灵活,让用户能够更加容易的去使用. 首先创建一个文本Text 然后是文本相对应的属性 这里的属性比 ...

  7. UITabBarController 初学

    纯代码编写UITabBarController, 不多说,直接见代码 RViewController1 *vc1 = [[RViewController1 alloc]init]; UINavigat ...

  8. MVC小系列(二)【Razor 模板引擎】

    Razor 模板引擎 Razor模板页:它使我们不用再使用master模板了 一 :@Url.Content:是可以加载CSS和JS等文件比如: <link href="@Url.Co ...

  9. Js编码和Java后台解码

    1.java.将resultMsg 转为utf-8 (1) resultMsg = URLEncoder.encode(resultMsg, "utf-8"); (2) new S ...

  10. (转)iOS中3种正则表达式的使用与比较

    .利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @“nijino_saki@.com”: NSString *regex = @"[A-Z0 ...