Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

时间:2022-02-08 16:34:16

Prime Ring Problem

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 18   Accepted Submission(s) : 7
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.

Prime Ring Problem  +   nyoj 素数环  +  Oil Deposits  +  Red and Black

 
Input
n (0 < n < 20).
 
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order. You are to write a program that completes above process. Print a blank line after each case.
 
Sample Input
6 8
 
Sample Output
Case 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2
 题意:就是一串数相邻和不能为素数;包括首尾;
代码:
 #include<stdio.h>
#include<string.h>
int n,t,z[],mark[];
//int m[10010][21];
bool isprime(int x){
if(x==||x==)return false;
for(int i=;i<x;i++){
if(x%i==)return false;
}
return true;
}
void dfs(int flot){
if(flot>=n){
for(int i=;i<n;i++){
//m[t][i]=z[i];
if(i)printf(" ");
printf("%d",z[i]);
}puts("");
t++;return;
}
for(int i=;i<=n;i++){
if(isprime(z[flot-]+i)&&!mark[i]){if(flot==n-){
if(!isprime(+i))break;
}//判断首尾;
mark[i]=;
z[flot]=i;
dfs(flot+);
mark[i]=;
}
//else dfs(top-1,flot);
}
return ;
}
int main(){
int k=;
while(~scanf("%d",&n)){
k++;
printf("Case %d:\n",k);
memset(mark,,sizeof(mark));
z[]=;
dfs();
puts("");
}
return ;
}

素数环

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述

有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环。

为了简便起见,我们规定每个素数环都从1开始。例如,下图就是6的一个素数环。

Prime Ring Problem  +   nyoj 素数环  +  Oil Deposits  +  Red and Black

 
输入
有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束。
输出
每组第一行输出对应的Case序号,从1开始。
如果存在满足题意叙述的素数环,从小到大输出。
否则输出No Answer。
样例输入
6
8
3
0
样例输出
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
Case 3:
No Answer 题解:由于当n是奇数时肯定组不成素数,所以当n是奇数时,如果不是1就是no anwser;
代码:
 #include<stdio.h>
#include<string.h>
const int MAXN=;
int n,flot;
int vis[MAXN],ans[MAXN];
bool isprime(int x){
if(x==||x==)return false;
for(int i=;i<x;i++){
if(x%i==)return false;
}
return true;
}
void dfs(int num){
if(num==n){
if(isprime(ans[num-]+ans[])){
flot=;
for(int i=;i<num;i++){
if(i)printf(" ");
printf("%d",ans[i]);
}
puts("");
}
return;
}
for(int i=;i<=n;i++){
if(vis[i]||!isprime(i+ans[num-]))continue;
vis[i]=;ans[num]=i;
dfs(num+);
vis[i]=;
}
}
int main(){
int t=;
while(~scanf("%d",&n),n){
memset(vis,,sizeof(vis));
printf("Case %d:\n",++t);
if(n&){
if(n==)puts("");
else puts("No Answer");
}
else{
flot=;
ans[]=;
dfs();
if(!flot)puts("No Answer");
}
}
return ;
}

Oil Deposits

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 62   Accepted Submission(s) : 40
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
 
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 
Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
 
Sample Output
0 1 2 2
 题意:简单搜索,跟南阳水池数目一样:
代码:
 #include<stdio.h>
int m,n;
char map[][];
void dfs(int x,int y){
if(map[y][x]=='*'||x<||x>=n||y<||y>=m)return ;
map[y][x]='*';
dfs(x+,y);
dfs(x,y+);
dfs(x-,y);
dfs(x,y-);
dfs(x+,y+);
dfs(x-,y-);
dfs(x-,y+);
dfs(x+,y-);
}
int main(){int tot;
while(~scanf("%d%d",&m,&n),m||n){tot=;
for(int y=;y<m;y++)scanf("%s",map[y]);
for(int y=;y<m;y++){
for(int x=;x<n;x++){
if(map[y][x]=='@'){
dfs(x,y);tot++;
}
}
}
printf("%d\n",tot);
}
return ;}

Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 71   Accepted Submission(s) : 65
Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

 
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. '.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set)
 
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
 
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
 代码: