当前位置:文档之家› ACM题目整理

ACM题目整理

题目来源:福州大学acm网站代码:fpcdq一、入门熟悉ACM竞赛规则以及程序提交注意事项例题:Problem 1000 A+B ProblemTime Limit: 1000 mSec Memory Limit : 32768 KB Problem DescriptionCalculate a + b.InputThe input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.Sample Input1 52 3Sample Output65My answer:#include <stdio.h>main(){long a,b;while((scanf("%ld%ld",&a,&b))!=EOF){printf("%ld\n",a+b);}}详情参考/faq.php二、ACM分类主流算法:1.搜索//回溯Problem 1019 猫捉老鼠Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description一只猫和一只老鼠在10*10的迷宫中。

迷宫中的每个方格可以是空的,或者含有障碍。

猫和老鼠可以进入任意一个空的方格中。

当他们相遇时,猫和老鼠在同一个方格中。

但是,无论猫或老鼠都不能进入有障碍的方格。

我们可以用字符组成的二维数组表示迷宫,如下图所示。

老鼠在迷宫中按照一种固定的方式行走:每个时刻,老鼠都向它所面对的方向前进一格,这需要花费1秒时间。

如果前方是一个障碍或者是迷宫的边界,它将花1秒的时间按顺时针方向转90度。

为了抓到老鼠,这只猫决定也按照与老鼠相同的行走方式行进。

猫和老鼠在每个单位时间内是同时行动的。

因此,如果猫和老鼠在行进过程中“擦肩而过”,猫是无法捉到老鼠的。

只有当猫和老鼠同时到达一个相同的格子时,猫才能捉住老鼠。

初始时,猫和老鼠不会在同一个方格中。

并且它们都面向北方。

你的任务是编一个程序,求出猫捉到老鼠的所花时间。

Input输入数据的第一行n,表示输入数据的组数。

每组数据由10行组成,每行10个字符,表示迷宫的地图以及猫和老鼠的初始位置。

输入数据保证只有一只猫和一只老鼠。

每组输入数据之后均有一个空行作为间隔。

Output对于每组给定的输入,输出一行仅含一个数,即猫捉到老鼠所花的时间。

如果猫永远都无法抓到老鼠,则输出0。

Sample Input1*...*...........*......*...*...............*.c....*.....*......*........m......*...*.*.....*.*......Sample Output49My answer:#include <stdio.h>char str[12][12];void doing(int *fs,int *x,int *y){if(*fs==1){if(str[*x-1][*y]=='*') *fs=4;else *x=*x-1;}else if(*fs==2){if(str[*x+1][*y]=='*') *fs=3;else *x=*x+1;}else if(*fs==3){if(str[*x][*y-1]=='*') *fs=1;else *y=*y-1;}else{if(str[*x][*y+1]=='*') *fs=2;else *y=*y+1;}}main(){int i,j,k,n,p,fs1,fs2,x1,y1,x2,y2;scanf("%d",&n);getchar();for(i=0;i<=11;i++){str[i][0]='*';str[i][11]='*';str[0][i]='*';str[11][i]='*';}for(i=1;i<=n;i++){fs1=1;fs2=1;p=1;for(j=1;j<=10;j++){for(k=1;k<=10;k++){scanf("%c",&str[j][k]);if(str[j][k]=='c'){x1=j;y1=k;str[j][k]='.';}if(str[j][k]=='m'){x2=j;y2=k;str[j][k]='.';}}getchar();}for(j=1;j<10000;j++){doing(&fs1,&x1,&y1);doing(&fs2,&x2,&y2);if(x1==x2&&y1==y2){printf("%d\n",j);p=0;break;}}if(p)printf("0\n");if(i<n)getchar();}}Problem 1063 三维扫描Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description工业和医学上经常要用到一种诊断技术——核磁共振成像(Magnetic Resonance Imagers)。

利用该技术可以对三维物体(例如大脑)进行扫描。

扫描的结果用一个三维的数组来保存,数组的每一个元素表示空间的一个象素。

数组的元素是0-255的整数,表示该象素的灰度。

例如0表示该象素是黑色的,255表示该象素是白色的。

被扫描的物体往往是由若干个部件组合而成的。

例如临床医学要对病变的器官进行检查,而器官是由一些不同的组织构成的。

在实际问题中,同一个部件内部的色彩变化相对连续,而不同的部件的交界处色彩往往有突变。

下面是一个简化的植物细胞的例子。

从细胞的平面图来看,该细胞大致是由四个“部件”构成的,细胞壁、细胞核、液泡和细胞质。

为了方便起见,我们对部件的概念做如下的规定:1.如果一个象素属于某部件,则或者该象素至少与该部件的一个象素相邻,或者该象素单独组成一个部件。

(说明:每一个象素与前后、左右、上下的6个象素相邻)2.同一个部件内部,相邻两个象素的灰度差不超过正整数M。

M决定了程序识别部件的灵敏度。

你的任务是对于给定的物体,判断该物体是由几个部件组成的。

Input输入数据由多组数据组成。

每组数据格式如下:第一行是三个正整数L,W,H(L,W,H≤50),表示物体的长、宽、高。

第二行是一个整数M(0≤M≤255),表示识别部件的灵敏度。

接下来是L×W×H个0-255的非负整数,按照空间坐标从小到大的顺序依次给出每个象素的灰度。

说明:对于空间两点P1(x1,y1,z1)和P2(x2,y2,z2),P1<P2当且仅当(x1<x2)或者(x1=x2且y1<y2)或者(x1=x2且y1=y2且z1<z2) Output对于每组数据,输出仅一行包含一个整数M,即一共识别出几个部件。

Sample Input2 2 21 1 1 12 2 2 2Sample Output2My answer:#include <stdio.h>#include <math.h>int m,l,w,h,c[51][51][51];long sum;doing(int i,int j,int r){int q;q=c[i][j][r];c[i][j][r]=-1;if(i-1>0)if(c[i-1][j][r]!=-1)if(fabs(c[i-1][j][r]-q)<=m)doing(i-1,j,r);if(i+1<=l)if(c[i+1][j][r]!=-1)if(fabs(c[i+1][j][r]-q)<=m)doing(i+1,j,r);if(j-1>0)if(c[i][j-1][r]!=-1)if(fabs(c[i][j-1][r]-q)<=m)doing(i,j-1,r);if(j+1<=w)if(c[i][j+1][r]!=-1)if(fabs(c[i][j+1][r]-q)<=m)doing(i,j+1,r);if(r-1>0)if(c[i][j][r-1]!=-1)if(fabs(c[i][j][r-1]-q)<=m)doing(i,j,r-1);if(r+1<=h)if(c[i][j][r+1]!=-1)if(fabs(c[i][j][r+1]-q)<=m)doing(i,j,r+1);}main(){int i,j,r;while(scanf("%d%d%d",&l,&w,&h)!=EOF){scanf("%d",&m);for(i=1;i<=l;i++)for(j=1;j<=w;j++)for(r=1;r<=h;r++)scanf("%d",&c[i][j][r]);sum=0;for(i=1;i<=l;i++)for(j=1;j<=w;j++)for(r=1;r<=h;r++)if(c[i][j][r]!=-1){sum++;doing(i,j,r);}printf("%d\n",sum);}}Problem 1073 Raiders of the Lost Ark Time Limit: 1000 mSec Memory Limit : 32768 KB Problem DescriptionIn this famous film, renowned archeologist and expert in the occult, Dr. Indiana Jones, is hired by the U.S. Government to find the Ark of the Covenant, which is believed to still hold the ten commandments. Unfortunately, agents of Hitler are also after the Ark. Dr. Jones escapes from various close scrapes in a quest that takes him from Nepal to Cairo. Your task is to help him to find the lost Ark of the Covenant in the maze earlier than the enemy.There are several barriers in the maze, Dr. Jones can bypass these barriers on foot or by jump .He can go on foot in four directions of east, south, west and north to reach a neighboring position. He can also jump cross a cell in the directions of southeast, southwest, northeast and northwest to reach a new position. The following figure shows Dr. Jones's action where O denotes the original position and X denotes the new possible positions after one action.InputThe first line of the input is an integer n (0<=n<=20), denoting the number of test cases. The following lines are the data of n test cases. The first line of each test case consists of two integer x and y (0<x<10, 0<y<10). The next x lines describe the maze matrix, which contains x rows and y columns. In the matrix, 'W' denotes the barrier, 'B' denotes the enterable position, 'S' denotes the starting position, and 'X' denotes the position of the ark. There have and only have one 'S' and one 'X' in a maze matrix.OutputFor each test case, output one line. If there is a solution for the problem, output the number of the least steps to find the Ark. Otherwise output "NO ANSWER".Sample Input33 3SWBBWBXBB5 4BXWBBBWSBBWBBBWBBBBB3 2WXWWWSSample Output22NO ANSWERMy answer:#include <stdio.h>int n,m,p[13][13],x1,x2,y1,y2;char s[13][13];doing(int x,int y,int z){if(z>=p[x][y])return 0;p[x][y]=z;if(x-1>0)if(s[x-1][y]=='B')doing(x-1,y,z+1);if(y-1>0)if(s[x][y-1]=='B')doing(x,y-1,z+1);if(x<n)if(s[x+1][y]=='B')doing(x+1,y,z+1);if(y<m)if(s[x][y+1]=='B')doing(x,y+1,z+1);if(x-2>0&&y-2>0)if(s[x-2][y-2]=='B')doing(x-2,y-2,z+1);if(x-2>0&&y+2<=m)if(s[x-2][y+2]=='B')doing(x-2,y+2,z+1);if(x+2<=n&&y-2>0)if(s[x+2][y-2]=='B')doing(x+2,y-2,z+1);if(x+2<=n&&y+2<=m)if(s[x+2][y+2]=='B')doing(x+2,y+2,z+1);}main(){int i,j,r,k;scanf("%d",&k);for(r=1;r<=k;r++){scanf("%d%d%*c",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){p[i][j]=20000;scanf("%c",&s[i][j]);if(s[i][j]=='S'){x1=i;y1=j;s[i][j]='B';}if(s[i][j]=='X'){x2=i;y2=j;s[i][j]='B';}}getchar();}doing(x1,y1,0);if(p[x2][y2]==20000)printf("NO ANSWER\n");else printf("%d\n",p[x2][y2]);}}Problem 1081 等分液体Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description有三种容器R1,R2,R3,其容积分别是L,M,N。

相关主题