2919 选择题

时间限制: 1 s    空间限制: 16000 KB    题目等级 : 黄金 Gold

题目描述 Description

某同学考试,在N*M的答题卡上写了A,B,C,D四种答案。

他做完了,又不能交,一看表,离打铃还有N久。

他开始玩一个游戏:选一个格子X,Y,从这个格子出发向4个方向找相同的选项,找到的再如此。

求形成的图形的面积。(一个选项占一个单位面积)

输入描述 Input Description

N M X  Y

答题卡(矩阵)

输出描述 Output Description

面积

样例输入 Sample Input

3 3 1 2

A C B

C C C

D C A

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

N,M<=15.

对于33%数据,只有A。

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 16
int n,m,sx,sy,ans=;;
int dx[]={,,,-},dy[]={,-,,};
char map[maxn][maxn];
bool vis[maxn][maxn];
void DFS(int x,int y){
ans++;vis[x][y]=true;
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<=n&&ny<=m&&ny>=&&nx>=
&&map[x][y]==map[nx][ny]&&!vis[nx][ny])
DFS(nx,ny);
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&sx,&sy);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>map[i][j];
DFS(sx,sy);
printf("%d\n",ans);
return ;
}

 

2925 整除问题

时间限制: 1 s    空间限制: 256000 KB    题目等级 : 黄金 Gold

题目描述 Description

对于数列A,如果添加运算符号,+或-,使得式子能够被k整除,则输出Divisible,否则输出Not divisible

输入描述 Input Description

第一行,N,K

第二行,描述A数列

输出描述 Output Description

如题目所示

样例输入 Sample Input

2 3

1 4

样例输出 Sample Output

Divisible

数据范围及提示 Data Size & Hint

注意,第一个数不能添加符号.

N<=10000,K<=100.

全部TLE:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn = ;
int n,k,a[maxn];
bool flag=;
void DFS(int sum,int pos){
if(pos>n)return;
if(pos==n && sum%k==){
flag=true;
return;
}
DFS(sum-a[pos+],pos+);
DFS(sum+a[pos+],pos+);
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
DFS(a[],);
if(!flag)printf("Not divisible");
if(flag)printf("Divisible");
return ;
}

记忆话搜索:

 #include <stdio.h>
#include <stdlib.h>
int a[],vis[][],n,k;
void dfs(int pos,int sum){
if(vis[pos][sum])return;
vis[pos][sum]=;
if(pos==n ){
if(vis[n][]==){
printf("Divisible");
exit();
}
return ;
}
dfs(pos+,(sum+a[pos])%k);
dfs(pos+,(abs(sum-a[pos]))%k);
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
dfs(,(abs(a[]))%k);
printf("Not divisible");
return ;
}

1293 送给圣诞夜的极光

时间限制: 1 s    空间限制: 128000 KB    题目等级 : 黄金 Gold

题目描述 Description

圣诞老人回到了北极圣诞区,已经快到12点了。也就是说极光表演要开始了。这里的极光不是极地特有的自然极光景象。而是圣诞老人主持的人造极光。 
  轰隆隆……烟花响起(来自中国的浏阳花炮之乡)。接下来就是极光表演了。 
  人造极光其实就是空中的一幅幅n*m的点阵图像。只是因为特别明亮而吸引了很多很多小精灵的目光,也成为了圣诞夜最美丽的一刻。 
  然而在每幅n*m的点阵图像中,每一个点只有发光和不发光两种状态。对于所有的发光的点,在空中就形成了美丽的图画。而这个图画是以若干个(s个)图案组成的。对于图案,圣诞老人有着严格的定义:对于两个发光的点,如果他们的曼哈顿距离(对于A(x1,y1)和B(x2,y2),A和B之间的曼哈顿距离为|x1-x2|+|y1-y2|)小于等于2。那么这两个点就属于一个图案…… 
  小精灵们一边欣赏着极光,一边数着每一幅极光图像中的图案数。伴着歌声和舞蹈,度过了美丽的圣诞之夜。^_^

输入描述 Input Description

第一行,两个数n和m。(1<=n,m<=100) 
接下来一共n行,每行m个字符。对于第i行第j个字符,如果其为“-”,那么表示该点不发光,如果其为“#”,那么表示该点发光。不可能出现其他的字符。

输出描述 Output Description

第一行,一个数s。

样例输入 Sample Input

19 48

------------------------------------------------

---####-----#-----#----------------------####---

--######----#-----#---------------------######--

-########--#-#---#-#####--#-##-##---#--########-

-###--###--#-#---#-#----#-##-##--#--#--###--###-

-###--###--#--#-#--######-#--#---#-#---###--###-

-########--#--#-#--#------#--#----##---########-

--######---#---#---######-#--#-----#----######--

---####----------------------------#-----####---

----------------------------------#-------------

------------------------------------------------

---###--#--------#------#-----------------------

--#---#-#---------------#-----------------------

-#------#-##--#-##--##-###-#-##-###--###-#--##--

-#------##--#-##-#-#----#--##--#---##---##-#----

-#------#---#-#--#--#---#--#---#---##----#--#---

--#---#-#---#-#--#---#--#--#---#---##---##---#--

---###--#---#-#--#-##---#--#---#---#-###-#-##---

------------------------------------------------

样例输出 Sample Output

4

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,ans;
int dx[]={,,,-,-,,-,,,-,,};
int dy[]={,-,,,-,,,-,,,,-};
char map[][];
void DFS(int x,int y){
map[x][y]='-';
for(int i=;i<;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<=n&&ny>=
&&ny<=m&&map[nx][ny]=='#')
DFS(nx,ny);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>map[i][j];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(map[i][j]=='#'){
ans++;DFS(i,j);
}
} printf("%d",ans);
return ;
}

                  1268 选择我自己的算法

                      2012年CCC加拿大高中生信息学奥赛

              时间限制: 1 s    空间限制: 128000 KB    题目等级 : 钻石 Diamond

 
题目描述 Description

In Waterloo, you probably have seen some geese. How can you see geese with your calculator? Start with 6, add 7, multiply by 6, multiply by 8, add 7, multiply by 8, and multiply by 7, giving 35336. Then if you flip your calculator upside down, it says gEESE:

在水城,你可能会看到一些鹅。你怎么在计算器上看到鹅呢?方法是输入35536,然后把计算器倒过来看,你就能看到gEESE——鹅了。

You want to write a program to help automatically build tricks of this type. However, your calcula- tor has a lot of broken buttons: the only mathematical operators that work are + and ×, and only a few of the digits work. Your goal is to figure out whether your half-broken calculator can achieve a given target value, using single-digit inputs and a fixed number of operations.

现在你的计算器的减号不给力了,只有+号和×号可以用,而且也只有部分数字可以工作。现在的任务是对于这个破烂的计算器能否通过简单的数字和固定数目的运算次数得到一个给定的目标值。

Note: the calculator performs the operations as soon as they are entered, rather than following any rules for order of operations (see Sample Input 2).

注意这个计算器的运算在被按下的时候就立即执行了,而不需要按照运算符优先级的规则。(见样例2)

输入描述 Input Description

The first line of input is W, the exact number of operations you must use. W will be an integer between 0 and 6. The second line of input is 1 ≤ D ≤ 10, the number of working digit keys. On each of the D following lines, a working digit is given; these values are distinct integers from 0 to 9. Finally, an integer 1 ≤ V ≤ 5 is given, the number of target values; on each of the following V lines there is an integer between 0 and 5000000 (inclusive) giving a target value which you’d like to achieve on your calculator.

第一行输入一个整数W,表示你能够且必须使用的运算个数。W是是一个介于0到6之间的整数。结下来的一行是一个整数D(1 ≤ D ≤ 10),可以工作的数字键的个数。接下来的D行给出了这D个可以工作的数字键,这些数字键是0-9之间的互不重复的整数。然后给出一个整数V(1 ≤ V ≤ 5),即目标值的个数,接下来的V行每行一个介于0到5,000,000(包含)的整数,代表你需要用过计算器算出来的数值。

输出描述 Output Description

The output consists of V lines corresponding to the target values; each line contains “Y” if that target value can be achieved, and “N” if it cannot be achieved, using exactly W operations with the D given digits.
Precisely, a target value T can be achieved if, starting with one of the D digits, and then by adding or multiplying exactly W times by one of the digits, you end up with T. Digits can be re-used, and you do not need to use all of the digits. You cannot enter multi-digit numbers.

输出包含V行,对应着V个目标值。每行是Y或者N,代表对应的目标值能在使用这D个数字并且刚好在W次运算的情况下被计算得到或者不能被计算得到。精确的说,一个目标值T能够计算得到的话,那是通过从D个数字钟的某个数字开始,通过加或乘其这些数字刚好W次,然后最后得到数值T。数字是可以重复使用的。数字也不需要全部被用完。但注意你不可以输入多位数。

样例输入 Sample Input

样例输入1

6

3

6

7

8

1

35336

样例输入2

3

2

4

9

2

97

88

样例输出 Sample Output

样例输出1:

Y

样例输出2:

N

Y

数据范围及提示 Data Size & Hint

First line: we cannot achieve 97 using the rules of this calculator, so the output is N (even despite that 4×4+9×9 = 97, when the typical order of operations rules are taken into account). Second line: start with 9, add 9, add 4, and multiply by 4; this gives 88.

对于第二个数据,虽然4×4+9×9 = 97,但是由于计算的顺序是从左往右依次进行的,所以输出N。第二行,9+9+4*4就得到了88.

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
long long a[],n,w,k,x;
bool f[][];
void Try(long long sum,int step){
f[step][sum]=true;
if(step>=w)return;
for(int i=;i<=n;i++){
if(!f[step+][sum+a[i]])Try(sum+a[i],step+);
if(!f[step+][sum*a[i]])Try(sum*a[i],step+);
}
}
int main()
{
scanf("%lld%lld",&w,&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%lld",&k);
for(int i=;i<=k;i++){
scanf("%lld",&x);
for(int j=;j<=n;j++)
Try(a[j],);
if(f[w][x])printf("Y\n");
else printf("N\n");
}
return ;
}

总是RE  、、、唉,先不管了、、、、

                            1008 选数   

                              2002年NOIP全国联赛普及组

                    时间限制: 1 s    空间限制: 128000 KB    题目等级 : 黄金 Gold
题目描述 Description

已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n)。从 n 个整数中任选 k 个整数相加,可分别得到一系列的和。例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为:
    3+7+12=22  3+7+19=29  7+12+19=38  3+12+19=34。
  现在,要求你计算出和为素数共有多少种。
  例如上例,只有一种的和为素数:3+7+19=29)。

输入描述 Input Description

 键盘输入,格式为:
  n , k (1<=n<=20,k<n)
  x1,x2,…,xn (1<=xi<=5000000)

输出描述 Output Description

屏幕输出,格式为:
  一个整数(满足条件的种数)。

样例输入 Sample Input

4 3
3 7 12 19

样例输出 Sample Output

1

数据范围及提示 Data Size & Hint

(1<=n<=20,k<n)
(1<=xi<=5000000)

 #include<iostream>
#include<math.h>
using namespace std;
int n,k,a[],ans=;
bool Judge(int x){
for(int i=;i<=sqrt(x);i++)
if(x%i==)return false;
return true;
}
void DFS(int dep,int sum,int find){
if(find==k){
if(Judge(sum))ans++;
return;
}
else{
for(int i=dep;i<=n;i++)
DFS(i+,sum+a[i],find+);
}
}
int main(){
cin>>n>>k;
for(int i=;i<=n;i++)cin>>a[i];
DFS(,,);
cout<<ans<<endl;
}

Codevs 搜索刷题 集合篇的更多相关文章

  1. 【codevs】刷题记录→_→(推荐看!)

    注:本文是我原先在csdn内写的一篇博文,现转到这里,两篇博文尽量同时更新. //#include<iostream->shuati> //define 为什么刷  学长☞hzwer ...

  2. 搜索刷题记录by cellur925

    我好菜啊!连暴搜都不会! 注意边界退出! 特开此帖,记录搜索学习之路!(逃) 1.全排列 2.八皇后 3.数的划分 由于此题有同一划分方法算一个的限制,我们为了避免搜多,可以使搜出的结果满足单调不降性 ...

  3. eetCode刷题-递归篇

    递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...

  4. meet in the middle 折半搜索 刷题记录

    复杂度分析 假设本来是n层,本来复杂度是O(2^n),如果meet in middle那就是n/2层,那复杂度变为O( 2^(n/2) ),跟原来的复杂度相比就相当于开了个方 比如如果n=40那爆搜2 ...

  5. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  6. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  7. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

  8. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  9. C#LeetCode刷题-广度优先搜索

    广度优先搜索篇 # 题名 刷题 通过率 难度 101 对称二叉树   42.1% 简单 102 二叉树的层次遍历   49.7% 中等 103 二叉树的锯齿形层次遍历   43.0% 中等 107 二 ...

随机推荐

  1. mysql 删除 一天前 创建 的数据,计算时间差

    DELETE from table_name WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 24*60*60 https://www.cnb ...

  2. linux配置邮箱服务

    配置邮箱服务Linux常见的邮箱客户端是mail或mutt:服务端有sendmail服务(centos 5).postfix服务(centos 6).这里我们不使用本地的邮件服务,而是使用本地的邮件客 ...

  3. vue.js 图片预览

    Vue.js的图片预览的插件还是不少,但是找了半天还是没找到跟现在项目里能用得很顺手的,其实项目里图片预览功能很简单,点击放大,能双指缩放就可以了.部分vue.js的图片预览库都需要把图片资源单独拿出 ...

  4. 精通SpringBoot--分页查询功能的实现

    本文将介绍如何实现分页查询功能,推荐使用github的pagehelper插件实现(事实上大家基本都是这么干的),但本文的实现方式和大多数不同,废话少说,现在就带着大家看看区别在哪里.先看pom.xm ...

  5. GoF23种设计模式之创建型模式之建造者模式

    一.概述 将一个复杂对象的构建与其表示分离开来,使得同样的构建过程可以创建不同的表示. 二.适用性 1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式的时候. 2.当构造过程必须允许 ...

  6. 748. Shortest Completing Word

    https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...

  7. Green Space【绿色空间】

    Green Space Living in an urban area with green spaces has a long-lasting positive impact on people's ...

  8. B1051 复数乘法(15 分)

    [PAT]B1051 复数乘法(15 分) - 路明天 - 博客园 https://www.cnblogs.com/hebust/p/9496809.html 在此对四舍五入输出结果做总结. 对于do ...

  9. 常见/dev/mapper/centos-root扩容

    系统Centos 7 df -h 查看当前分区使用情况: dfisk /dev/xvda 对/dev/xvda磁盘进行操作(新建分区及格式化) n p 回车 默认分区号: 回车 默认磁盘创建开始位置: ...

  10. 【java】实体类中 Set<对象> 按照对象的某个字段对set排序

    Java利用hibernate进行一对多查询时,把另一张表作为一个属性存进这张表的字段中,返回的类型是set类型,要对返回的set类型进行排序 user表 package onlyfun.caterp ...