Codevs 搜索刷题 集合篇
2919 选择题
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold
某同学考试,在N*M的答题卡上写了A,B,C,D四种答案。
他做完了,又不能交,一看表,离打铃还有N久。
他开始玩一个游戏:选一个格子X,Y,从这个格子出发向4个方向找相同的选项,找到的再如此。
求形成的图形的面积。(一个选项占一个单位面积)
N M X Y
答题卡(矩阵)
面积
3 3 1 2
A C B
C C C
D C A
5
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
对于数列A,如果添加运算符号,+或-,使得式子能够被k整除,则输出Divisible,否则输出Not divisible
第一行,N,K
第二行,描述A数列
如题目所示
2 3
1 4
Divisible
注意,第一个数不能添加符号.
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
圣诞老人回到了北极圣诞区,已经快到12点了。也就是说极光表演要开始了。这里的极光不是极地特有的自然极光景象。而是圣诞老人主持的人造极光。
轰隆隆……烟花响起(来自中国的浏阳花炮之乡)。接下来就是极光表演了。
人造极光其实就是空中的一幅幅n*m的点阵图像。只是因为特别明亮而吸引了很多很多小精灵的目光,也成为了圣诞夜最美丽的一刻。
然而在每幅n*m的点阵图像中,每一个点只有发光和不发光两种状态。对于所有的发光的点,在空中就形成了美丽的图画。而这个图画是以若干个(s个)图案组成的。对于图案,圣诞老人有着严格的定义:对于两个发光的点,如果他们的曼哈顿距离(对于A(x1,y1)和B(x2,y2),A和B之间的曼哈顿距离为|x1-x2|+|y1-y2|)小于等于2。那么这两个点就属于一个图案……
小精灵们一边欣赏着极光,一边数着每一幅极光图像中的图案数。伴着歌声和舞蹈,度过了美丽的圣诞之夜。^_^
第一行,两个数n和m。(1<=n,m<=100)
接下来一共n行,每行m个字符。对于第i行第j个字符,如果其为“-”,那么表示该点不发光,如果其为“#”,那么表示该点发光。不可能出现其他的字符。
第一行,一个数s。
19 48
------------------------------------------------
---####-----#-----#----------------------####---
--######----#-----#---------------------######--
-########--#-#---#-#####--#-##-##---#--########-
-###--###--#-#---#-#----#-##-##--#--#--###--###-
-###--###--#--#-#--######-#--#---#-#---###--###-
-########--#--#-#--#------#--#----##---########-
--######---#---#---######-#--#-----#----######--
---####----------------------------#-----####---
----------------------------------#-------------
------------------------------------------------
---###--#--------#------#-----------------------
--#---#-#---------------#-----------------------
-#------#-##--#-##--##-###-#-##-###--###-#--##--
-#------##--#-##-#-#----#--##--#---##---##-#----
-#------#---#-#--#--#---#--#---#---##----#--#---
--#---#-#---#-#--#---#--#--#---#---##---##---#--
---###--#---#-#--#-##---#--#---#---#-###-#-##---
------------------------------------------------
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
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)
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(包含)的整数,代表你需要用过计算器算出来的数值。
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。数字是可以重复使用的。数字也不需要全部被用完。但注意你不可以输入多位数。
样例输入1
6
3
6
7
8
1
35336
样例输入2
3
2
4
9
2
97
88
样例输出1:
Y
样例输出2:
N
Y
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全国联赛普及组
已知 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)。
键盘输入,格式为:
n , k (1<=n<=20,k<n)
x1,x2,…,xn (1<=xi<=5000000)
屏幕输出,格式为:
一个整数(满足条件的种数)。
4 3
3 7 12 19
1
(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 搜索刷题 集合篇的更多相关文章
- 【codevs】刷题记录→_→(推荐看!)
注:本文是我原先在csdn内写的一篇博文,现转到这里,两篇博文尽量同时更新. //#include<iostream->shuati> //define 为什么刷 学长☞hzwer ...
- 搜索刷题记录by cellur925
我好菜啊!连暴搜都不会! 注意边界退出! 特开此帖,记录搜索学习之路!(逃) 1.全排列 2.八皇后 3.数的划分 由于此题有同一划分方法算一个的限制,我们为了避免搜多,可以使搜出的结果满足单调不降性 ...
- eetCode刷题-递归篇
递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...
- meet in the middle 折半搜索 刷题记录
复杂度分析 假设本来是n层,本来复杂度是O(2^n),如果meet in middle那就是n/2层,那复杂度变为O( 2^(n/2) ),跟原来的复杂度相比就相当于开了个方 比如如果n=40那爆搜2 ...
- 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合
不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
- LeetCode刷题总结-树篇(上)
引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...
- C#LeetCode刷题-广度优先搜索
广度优先搜索篇 # 题名 刷题 通过率 难度 101 对称二叉树 42.1% 简单 102 二叉树的层次遍历 49.7% 中等 103 二叉树的锯齿形层次遍历 43.0% 中等 107 二 ...
随机推荐
- mysql运维-二进制日志BINARY LOG清理
1.1 方法1:PURGE MASTER LOGS 语法: PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetim ...
- nginx 如何配置来获取用户真实IP
- Redis之set类型操作
接口: package com.net.test.redis.base.dao; /** * @author*** * @Time:2017年8月10日 下午2:32:12 * @version 1. ...
- 7、python中的字典
字典是python内置的一种无序.可变的数据结构. 字典也叫哈希表.什么是哈希表?哈希表就是会对表中的键(key)执行哈希计算,并根据计算结果在内存中分配一个区域来储存该键所对应的值(value).这 ...
- 51nod 1554 KMP思维题
题目为中文,因而不再解释题意. 首先遵循如下设定可以有以下几个结论:1,首先谈论下KMP的一个特殊性质:对于某一个特立独行的字符串:例如ABCDEF,在建立有限状态自动机之后,都会有,所有元素的失配边 ...
- (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明
Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...
- java NIO简介
1)java nio简介 nio 是 java New IO 的简称,在 jdk1.4 里提供的新 api . Sun 官方标榜的特性如有:为所有的原始类型提供 (Buffer) 缓存支持:字符集编码 ...
- Tomcat之web.xml中的<url-pattern>标签
关于web.xml配置中的<url-pattern> 标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标 ...
- Django基于Pycharm开发之三[命名空间 与过滤器]
关于命名空间的问题,在project项目中,我们可以设置路由类似于: from django.conf.urls import url,includefrom django.contrib impor ...
- R语言分析朝阳医院数据
R语言分析朝阳医院数据 本次实践通过分析朝阳医院2016年销售数据,得出“月均消费次数”.“月均消费金额”.“客单价”.“消费趋势”等结果,并据此作出可视化图形. 一.读取数据: library(op ...