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.

 
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的一个素数环。

 
输入
有多组测试数据,每组输入一个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
 代码:
 
 

Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black的更多相关文章

  1. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  2. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  4. HDU 1016 Prime Ring Problem (素数筛+DFS)

    题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...

  5. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  8. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  9. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

随机推荐

  1. UESTC_邱老师玩游戏 2015 UESTC Training for Dynamic Programming<Problem G>

    G - 邱老师玩游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  2. 一张图讲解为什么需要自己搭建自己的git服务以及搭建的途径

    图片信息量有点大.不废话上图 图中的一些链接: gitlab官方安装文档 https://github.com/gitlabhq/gitlabhq/blob/master/doc/install/in ...

  3. 在Linux下如何创建LVM及LVM创建过程

    Linux LVM创建过程:(我用的是Centos6.8) 一.准备工作: LVM可以的创建可以在系统安装的过程中创建,也可以在安装完系统之后再创建建,都是可以的,我的是在系统安装完之后创建的,具体的 ...

  4. Eclipse选中变量名,相同变量都变色显示

    Eclipse选中变量名,相同变量都变色显示 java文件的设置"Window"-"preferences"-"Java"-"Ed ...

  5. poj2503--Babelfish(特里一水)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32988   Accepted: 14189 Descr ...

  6. 那么温暖http合约,入门。

    简介 年提出.经过几年的使用与发展,得到不断地完好和扩展.眼下在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,并且HTTP-NG(Next Generation ...

  7. ECSHOP首页站内快讯在哪里添加和修改?

    “添加新闻后在首页站内快讯处显示不出来?”.“请问首页中站内快讯(最新文章)在后台哪个位置管理”.“如何让发布的文章进入首页站内快讯”等等诸如此类的问题,经常在论坛里看到一些朋友在询问. 本ECSHO ...

  8. Js触发ASP.NET Validation控件的验证, 同时获取前台验证结果(不包括CustomValidator)

    function CallValidate(group) { if (typeof (Page_ClientValidate) == "function") { Page_Bloc ...

  9. Sql Server相关的性能计数器

    OS Memory and Paging 性能计数器: 1.Memory\Availability Mbytes   未使用的物理内存(非页面文件),通常情况下它应该大于100MB 2.Memory\ ...

  10. c#中的peek()方法

    peek()方法用来判断文件是否读取完成,如果完成的话,就会有一个返回值 - 1 所以可以用streamreader的对象sr调用peek()方法来判断文件流是否读取完成 ) { Console.Wr ...