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. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  2. 重大新闻:借贷宝不用绑卡了,借贷宝APP推出肖像识别新功能!

    动动手指,20元人民币立即到手:http://www.cnblogs.com/mfryf/p/4754384.html 滴滴打车烧钱十几个亿,狂送打车券,很多人天天免费坐车! 去年年初百度钱包注册奖励 ...

  3. Java菜鸟学习笔记--数组篇(三):二维数组

    定义 //1.二维数组的定义 //2.二维数组的内存空间 //3.不规则数组 package me.array; public class Array2Demo{ public static void ...

  4. 各硬件装置在 Linux 中的文件名(笔记)

    各硬件装置在 Linux 中的文件名

  5. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. C#.NET学习笔记11,12---布尔表达式2组合,if语句

    C#.NET学习笔记11---布尔表达式2组合 2013/9/6 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com int ...

  7. C++编程规范和标准总结

    文件名: 每个源代码文件应该有一个包含文件.每个包含文件描述了单个类或者多个类相结合的集合.一般头文件(.h,或.hpp)包含类的定义而不是实例.因此包含文件可以用在多个文件当中,源文件(.c,.或c ...

  8. TS流PAT/PMT详解

    一 从TS流开始 从MPEG-2到DVB,看着看着突然就出现了一大堆表格,什么PAT.PMT.CAT……如此多的表该怎样深入了解呢? 我们知道,数字电视机顶盒接收到的是一段段的码流,我们称之为TS(T ...

  9. NET基础课--组件2

    强命名组件:使用sn.exe生成公钥私钥对,公钥可以用工具查看.snk文件需严格保护. sn -k  d:\iron.snk       生成公钥私钥对 sn -p  d:\iron.snk  d:\ ...

  10. android——ObjectAnimator动画(一)

    直接贴上集中用法 package com.example.test; import com.example.test.views.CircleView; import android.animatio ...