function aaa(){ window.close(); } function ck() { console.profile(); console.profileEnd(); if(console.clear) { console.clear() }; if (typeof console.profiles =="object"){ return console.profiles.length > 0; } } function hehe(){ if( (window.console && (console.firebug || console.table && /firebug/i.test(console.table()) )) || (typeof opera == 'object' && typeof opera.postError == 'function' && console.profile.length > 0)){ aaa(); } if(typeof console.profiles =="object"&&console.profiles.length > 0){ aaa(); } } hehe(); window.onresize = function(){ if((window.outerHeight-window.innerHeight)>200) aaa(); }

POJ 3126【长度为素数的路径个数】

描述

对于正整数n (3≤n<20),可以画出n阶的回形矩阵。下面画出的分别是3阶的,4阶的和7阶的回形矩阵:

对于n阶回形矩阵,从左上角出发,每步可以向右或向下走一格,走2* n-2步,可以到达右下角。我们把这样的路
径上所有格子中的数值之和,叫做该路径的长度。本题要求,对于给出n值,求出n阶回形矩阵有多少路径的长度为
素数?如n=3时,路径及长度有:

因此说,3阶回形矩阵有2条路径的长度为素数。

 

输入输出格式

 

输入

一个自然数n (3≤n<20,不必判错)。

输出

一个正整数,即n阶回形矩阵中长度为素数的路径的个数。

 

输入输出样例

 

输入样例1

3

输出样例1

2

解题思路

  其实这个题主要难点就是构图,剩下的只要搜索再判断素数就行了。

题解

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,ans;
 4 int nn;
 5 int mp[1100][1100];
 6 bool _ss(int sum)//判断素数 
 7 {
 8     if(sum%2==0)return false;
 9     for(int i=3;i<=sqrt(sum);i+=2)
10     {
11         if(sum%i==0)return false;
12     }
13     return true;
14 }
15 void dfs(int x,int y,int sum)
16 {
17     if(x>n||y>n)return;//越界 
18     if(x==n&&y==n)
19     {
20         if(_ss(sum))//找到还是素数就加加 
21         {
22             ans++;
23         }
24         return;
25     }
26     dfs(x+1,y,sum+mp[x+1][y]);//向右走 
27     dfs(x,y+1,sum+mp[x][y+1]);//向下走 
28 }
29 int main()
30 {
31     cin>>n;
32     nn=n;
33     if(nn%2==1)nn=nn/2+1;//构图的基本操作 
34     else nn=nn/2;
35     for(int i=1;i<=nn;i++)
36     {
37         for(int j=i;j<=n-i+1;j++)
38         {
39             for(int l=i;l<=n-i+1;l++)
40             {
41                 mp[j][l]=i;//其实就是一圈一圈的向内标记 
42             }
43         }
44     }
45     dfs(1,1,mp[1][1]);
46     cout<<ans;
47     return 0;
48 }

 

 

posted @ 2019-07-09 15:34  华恋~韵  阅读(335)  评论(0编辑  收藏  举报