[Puzzle + Summary]20150815

Source: Internet
Author: User

1. Preface

Water test once .... The theme was search, last November, when there were only 200 points. This is a very simple one in the recent exams, just a little bit of skill in search.

2, Queen n Queen problem

Probably test instructions: So the classic problem will not have to talk about the topic.

Solution: Direct DFS, note the need for output scenarios.

Code:

-------------------------------------------------------------------------------------------------------

#include <cstdio>

int N,h[100],g[100],l[100],a[100],flag;

void print ()
{
for (int i=1;i<=n;i++) printf ("%d", a[i]);
printf ("\ n");
}

void DFS (int k)
{
for (int i=1;i<=n;i++)
{
if (!h[i] &&!g[i+k] &&!l[i-k+n])
{
h[i]=g[i+k]=l[i-k+n]=1;
a[k]=i;
if (k==n) flag=1,print (); else DFS (k+1);
h[i]=g[i+k]=l[i-k+n]=0;
}
}
}

int main ()
{
Freopen ("Queen.in", "R", stdin);
Freopen ("Queen.out", "w", stdout);
scanf ("%d", &n);
DFS (1);
if (!flag) printf ("-1");
return 0;
}

-------------------------------------------------------------------------------------------------------

3, Grz Mountain climbing

Approximate test instructions: given a n*n matrix, find out the number of lattice sets that meet one of the two conditions. Condition one: All lattice weights are the same, all Happo unicom, and the periphery of all lattice weights is greater than the lattice set; condition two: All lattice weights are the same, all of them are connected, and all the outer lattice weights are smaller than the lattice set.

Summary: At that time the problem is actually written out, but in the define aspect of the error that was not clear at the time, causing the array to open small.

The problem: The same direct BFS can, a little attention to the boundary of the way to judge, directly take-inf or INF is not advisable.

Code slightly.

4. Quantum Transform

Probably test instructions: The fourth question in http://www.cnblogs.com/jinkun113/p/4714076.html. The only change is that the weights are 1.

Summary: The topic has been done, the first direct NP rolling coarse; the second is August 8, because the bitwise operator is unfamiliar, causing the constant 50 to be stuck. Now, of course, it's AC.

The puzzle: See the link in the approximate test instructions.

Code:

-------------------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#define MAXN 21
#define INF 0x3f3f3f3f

int len,m,n,q[1<<maxn],f[1<<maxn],s,t,a[maxn*3],b[maxn*3],c[maxn*3];
Char CH[MAXN],T1[MAXN],T2[MAXN];

void init ()
{
freopen ("quantum.in", "R", stdin);
freopen ("Quantum.out", "w", stdout);
scanf ("%d%d%d", &len,&m,&n);
for (int i=1;i<=m;i++)
{
scanf ("%s", ch);
for (int j=0;j<=len-1;j++)
{
if (ch[j]!= ' C ') a[i]+=1<<j;
if (ch[j]== ' S ') b[i]+=1<<j;
if (ch[j]== ' F ') c[i]+=1<<j;
}
}
}

int work ()
{
int head=1,tail=2;
memset (f,inf,sizeof (f)), memset (q,0,sizeof (q));
q[1]=s,f[s]=0;
While (Head!=tail)
{
for (int i=1;i<=m;i++)
{
int temp= ((q[head]&a[i]) |b[i]) ^c[i];
if (f[q[head]]+1<f[temp]) f[temp]=f[q[head]]+1,q[tail]=temp,tail++;
}
head++;
}
return (F[t]==inf) -1:f[t];
}

int main ()
{
init ();
for (int i=1;i<=n;i++)
{
s=t=0;
scanf ("%s%s", t1,t2);
for (int i=0;i<=len-1;i++) s+= (t1[i]-' 0 ') <<i,t+= (t2[i]-' 0 ') <<i;
int ret=work ();
if (ret==-1) printf ("np\n"), Else printf ("%d\n", ret);
}
return 0;
}

-------------------------------------------------------------------------------------------------------

5. Ant anti-prime

Approximate test instructions: Define g (x) to denote approximately several numbers of x, define x as inverse prime if and only if G (x) >g (i) (i∈[1,x]), find the maximum inverse prime number not exceeding N.

Problem: I remember that Brother Jiang seems to have a data range than this most of the bereavement topic? But at this moment n<=10^14, so we do not falsely hit a quality tables and then violence DFS find just ...

Code:

-------------------------------------------------------------------------------------------------------

#include <cstdio>

typedef long Long LL;

int prime[14]={2,3,5,7,11,13,17,19,23,29,31,37,41,43};
ll Maxn,ans,n;

void DFS (ll num,int k,ll sum,int t)
{
ll temp;
if (SUM>MAXN) maxn=sum,ans=num;
if (sum==maxn && ans>num) ans=num;
if (k>=14) return;
Temp=num;
for (int i=1;i<=t;i++)
{
if (temp*prime[k]>n) break;
Temp*=prime[k];
DFS (temp,k+1,sum* (i+1), i);
}
}

int main ()
{
Freopen ("Ant.in", "R", stdin);
Freopen ("Ant.out", "w", stdout);
scanf ("%i64d", &n);
DFS (1,0,1,50);
printf ("%i64d", ans);
return 0;
}

-------------------------------------------------------------------------------------------------------

[Puzzle + Summary]20150815

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.