Codevs 搜索刷题 集合篇

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。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 #define maxn 16
 6 int n,m,sx,sy,ans=0;;
 7 int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
 8 char map[maxn][maxn];
 9 bool vis[maxn][maxn];
10 void DFS(int x,int y){
11     ans++;vis[x][y]=true;
12     for(int i=0;i<4;i++){
13         int nx=x+dx[i],ny=y+dy[i];
14         if(nx<=n&&ny<=m&&ny>=1&&nx>=1
15             &&map[x][y]==map[nx][ny]&&!vis[nx][ny])
16               DFS(nx,ny);
17     }
18 }
19 int main()
20 {
21     scanf("%d%d%d%d",&n,&m,&sx,&sy);
22     for(int i=1;i<=n;i++)
23         for(int j=1;j<=m;j++)
24             cin>>map[i][j];
25     DFS(sx,sy);
26     printf("%d
",ans);
27     return 0;
28 }

 

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:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 using namespace std;
 6 const int maxn = 10000;
 7 int n,k,a[maxn];
 8 bool flag=0;
 9 void DFS(int sum,int pos){
10     if(pos>n)return;
11     if(pos==n && sum%k==0){
12         flag=true;
13         return;
14     }
15     DFS(sum-a[pos+1],pos+1);
16     DFS(sum+a[pos+1],pos+1);
17 }
18 int main()
19 {
20     scanf("%d%d",&n,&k);
21     for(int i=1;i<=n;i++)
22         scanf("%d",&a[i]);
23     DFS(a[1],1);
24     if(!flag)printf("Not divisible");
25     if(flag)printf("Divisible");
26     return 0;
27 }

记忆话搜索:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int a[10005],vis[10005][105],n,k;
 4 void dfs(int pos,int sum){
 5     if(vis[pos][sum])return;
 6     vis[pos][sum]=1;
 7     if(pos==n ){
 8         if(vis[n][0]==1){
 9             printf("Divisible");
10             exit(0);
11         }
12         return ;
13     }
14     dfs(pos+1,(sum+a[pos])%k);
15     dfs(pos+1,(abs(sum-a[pos]))%k);
16 }
17 int main()
18 {
19     scanf("%d%d",&n,&k);
20     for(int i=0;i<n;i++)
21         scanf("%d",&a[i]);
22     dfs(1,(abs(a[0]))%k);
23     printf("Not divisible");
24     return 0;
25 }

 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

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int n,m,ans;
 6 int dx[]={0,0,1,-1,-1,1,-1,1,2,-2,0,0};
 7 int dy[]={1,-1,0,0,-1,1,1,-1,0,0,2,-2};
 8 char map[105][105];
 9 void DFS(int x,int y){
10     map[x][y]='-';
11     for(int i=0;i<12;i++){
12         int nx=x+dx[i],ny=y+dy[i];
13         if(nx>=1&&nx<=n&&ny>=1
14             &&ny<=m&&map[nx][ny]=='#')
15               DFS(nx,ny);
16     }
17 }
18 int main()
19 {
20     scanf("%d%d",&n,&m);
21     for(int i=1;i<=n;i++)
22         for(int j=1;j<=m;j++)
23             cin>>map[i][j];
24     for(int i=1;i<=n;i++)
25         for(int j=1;j<=m;j++){
26             if(map[i][j]=='#'){
27                 ans++;DFS(i,j);
28             }
29         }
30     
31     printf("%d",ans);
32     return 0;
33 }

                  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[20],n,w,k,x;
bool f[6][10000000];
void Try(long long sum,int step){
    f[step][sum]=true;
    if(step>=w)return;
    for(int i=1;i<=n;i++){
        if(!f[step+1][sum+a[i]])Try(sum+a[i],step+1);
        if(!f[step+1][sum*a[i]])Try(sum*a[i],step+1);
    }
}
int main()
{
    scanf("%lld%lld",&w,&n);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    scanf("%lld",&k);
    for(int i=1;i<=k;i++){
        scanf("%lld",&x);
        for(int j=1;j<=n;j++)
            Try(a[j],0);
        if(f[w][x])printf("Y
");
        else printf("N
");
    }
    return 0;
}

总是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)

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 int n,k,a[30],ans=0;
 5 bool Judge(int x){
 6     for(int i=2;i<=sqrt(x);i++)
 7       if(x%i==0)return false;
 8     return true;
 9 }
10 void DFS(int dep,int sum,int find){
11     if(find==k){
12         if(Judge(sum))ans++;
13         return;
14     }
15     else{
16         for(int i=dep;i<=n;i++)
17             DFS(i+1,sum+a[i],find+1);
18     }
19 }
20 int main(){
21     cin>>n>>k;
22     for(int i=1;i<=n;i++)cin>>a[i];
23     DFS(1,0,0);
24     cout<<ans<<endl;
25 }
原文地址:https://www.cnblogs.com/suishiguang/p/6444363.html