码迷,mamicode.com
首页 > 其他好文 > 详细

Prime Ring Problem

时间:2015-08-03 22:29:13      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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
 题意:就是一串数相邻和不能为素数;包括首尾;
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int n,t,z[21],mark[21];
 4 //int m[10010][21];
 5 bool isprime(int x){
 6 if(x==0||x==1)return false;
 7 for(int i=2;i<x;i++){
 8     if(x%i==0)return false;
 9 }
10 return true;
11 }
12 void dfs(int flot){
13     if(flot>=n){
14         for(int i=0;i<n;i++){
15             //m[t][i]=z[i];
16             if(i)printf(" ");
17             printf("%d",z[i]);
18         }puts("");
19         t++;return;
20     }
21     for(int i=2;i<=n;i++){
22         if(isprime(z[flot-1]+i)&&!mark[i]){if(flot==n-1){
23             if(!isprime(1+i))break;
24         }//判断首尾; 
25         mark[i]=1;
26             z[flot]=i;
27         dfs(flot+1);
28         mark[i]=0;
29         }
30         //else dfs(top-1,flot);
31     }
32     return ;
33 }
34 int main(){
35     int k=0;
36     while(~scanf("%d",&n)){
37     k++;
38     printf("Case %d:\n",k);
39     memset(mark,0,sizeof(mark));
40     z[0]=1;
41         dfs(1);
42         puts("");
43     }
44     return 0;
45 }

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
 题意:简单搜索,跟南阳水池数目一样:
代码:
 1 #include<stdio.h>
 2 int m,n;
 3 char map[110][110];
 4 void dfs(int x,int y){
 5     if(map[y][x]==*||x<0||x>=n||y<0||y>=m)return ;
 6     map[y][x]=*;
 7     dfs(x+1,y);
 8     dfs(x,y+1);
 9     dfs(x-1,y);
10     dfs(x,y-1);
11     dfs(x+1,y+1);
12     dfs(x-1,y-1);
13     dfs(x-1,y+1);
14     dfs(x+1,y-1);
15 }
16 int main(){int tot;
17 while(~scanf("%d%d",&m,&n),m||n){tot=0;
18     for(int y=0;y<m;y++)scanf("%s",map[y]);
19     for(int y=0;y<m;y++){
20         for(int x=0;x<n;x++){
21             if(map[y][x]==@){
22                 dfs(x,y);tot++;
23             }
24         }
25     }
26     printf("%d\n",tot);
27 }
28 return 0;}

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

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4700533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!