HDU 1976 prime path

题意:给你2个数n m,从n变成m最少需要改变多少次。

其中:

1、n  m  都是4位数

2、每次只能改变n的一个位数(个位、十位、百位、千位),且每次改变后后的新数为素数

思路:搜索的变形题,这次我们要搜得方向是改变位数中的一位,然后往下搜,直到求出我们需要的那个解

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define N 10000
int dis[N],cou[N];
bool prime[N];
void make()             //素数打表
{
    int i,j;
    for(i=1000;i<=N;i++)
    {
        int flag=1;
        for(j=2;j<i/2;j++)
        {
            if(i%j==0)
            {
                prime[i]=false;
                flag=0;
                break;
            }
        }
        if(flag)prime[i]=true;
    }

}
int bfs(int x,int y)
{
    queue <int>q;
	int v,i,j,temp,vtemp,t[4];  //t数组存放该数的每一位数
	memset(dis,0,sizeof(dis));
	memset(cou,0,sizeof(cou));
	q.push(x);
	dis[x]=1;
	while(!q.empty())
    {
		v=q.front();
		q.pop();
		t[0]=v/1000;
		t[1]=v%1000/100;
		t[2]=v%100/10;
		t[3]=v%10;
		for(j=0;j<4;j++)
		{
			temp=t[j];
			for(i=0;i<10;i++)
				if(i!=temp)
				{
                    t[j]=i;
                    vtemp=t[0]*1000+t[1]*100+t[2]*10+t[3];
                    if(!dis[vtemp]&&prime[vtemp]){
                        cou[vtemp]=cou[v]+1;
                        dis[vtemp]=1;
                        q.push(vtemp);
					}
                    if(vtemp==y) return cou[vtemp];
				}
            t[j]=temp;
        }
        if(v==y) return cou[v];
    }
	return -1;
}
int main()
{
    int t,n,m,sum;
    make();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        sum=bfs(n,m);
        printf("%d\n",sum);
    }
    return 0;
}

HDU 1976 prime path

时间: 2024-10-13 02:07:53

HDU 1976 prime path的相关文章

hdu 1973 Prime Path

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices

HDU - 1973 - Prime Path (BFS)

Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 987    Accepted Submission(s): 635 Problem Description The ministers of the cabinet were quite upset by the message from the Chief of S

HDU 1973 Prime path(BFS+素数表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 题目大意:给定两个四位素数a  b,要求把a变换到b变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能重复.求从a到b最少需要的变换次数.无法变换则输出Impossible. 如下面的样例:1033 8179 1033 1733 3733 3739 3779 8779 8179 所以答案为6.

(简单) POJ 3126 Prime Path,BFS。

Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now

HDU 1016 Prime Ring Problem 题解

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

双向广搜 POJ 3126 Prime Path

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change th

poj 3126 Prime Path (bfs)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13813   Accepted: 7796 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig

POJ 3126 Prime Path(BFS)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6843 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-dig

Prime Path (poj 3126 bfs)

Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 6640 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to c