• poj3126 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. 
    — It is a matter of security to change such things every now and then, to keep the enemy in the dark. 
    — But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! 
    — I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. 
    — No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! 
    — I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. 
    — Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime. 

    Now, the minister of finance, who had been eavesdropping, intervened. 
    — No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. 
    — Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you? 
    — In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above. 
    1033
    1733
    3733
    3739
    3779
    8779
    8179
    The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

    Input

    One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

    Output

    One line for each case, either with a number stating the minimal cost or containing the word Impossible.

    Sample Input

    3
    1033 8179
    1373 8017
    1033 1033

    Sample Output

    6
    7
    0
    题意是给你两个四位数的素数n,m,没有前导0,让你每次变化一位数,使得n变成m,问最小的次数。这题是典型的bfs.因为是4位数,所以判断素数的时候只要判断能不能被2~100整除就行了。
    #include<stdio.h>
    #include<string.h>
    int num,n,m;
    int a[10];
    int q[1111111][2],vis[50000];    //q[][0]数字,q[][1]次数 
    int prime(int n)
    {
    	int i,flag=1;
    	for(i=2;i<=100;i++){
    		if(n%i==0){
    			flag=0;break;
    		}
    	}
    	if(flag)return 1;
    	else return 0;
    }
    
    
    int bfs()
    {
    	int front=1,rear=1,x,t,i,xx,y;
    	memset(vis,0,sizeof(vis));
    	vis[n]=1;
    	q[front][0]=n;q[front][1]=0;
    	while(front<=rear)
    	{
    		x=q[front][0];
    		if(x==m) return q[front][1];
    		front++;
    		vis[x]=1;
    		t=0;
    		xx=x;
    		while(xx){
    			a[++t]=xx%10;
    			xx=xx/10;
    		}
    		
    		for(i=1;i<=9;i++){
    			y=i*1000+a[3]*100+a[2]*10+a[1];
    			if( vis[y]==0 && prime(y)){
    				vis[y]=1;
    				rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;
    			}
    		}
    		
    		for(i=0;i<=9;i++){
    			y=i*100+a[4]*1000+a[2]*10+a[1];
    			if( vis[y]==0 && prime(y)){
    				vis[y]=1;
    				rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;
    			}
    		}
    		
    		for(i=0;i<=9;i++){
    			y=a[4]*1000+a[3]*100+i*10+a[1];
    			if( vis[y]==0 && prime(y)){
    				vis[y]=1;
    				rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;
    			}
    		}
    		
    		for(i=0;i<=9;i++){
    			y=a[4]*1000+a[3]*100+a[2]*10+i;
    			if( vis[y]==0 && prime(y)){
    				vis[y]=1;
    				rear++;q[rear][0]=y;q[rear][1]=q[front-1][1]+1;
    			}
    		}
    	}
    }
    
    
    
    
    int main()
    {
    	int T,i,j;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d%d",&n,&m);
    		printf("%d
    ",bfs());
    	}
    	return 0;
    }
    
  • 相关阅读:
    Restful WCF问题总结
    vs2010发布、打包安装程序(超全超详细)
    WCF and Android Part I
    GCC安装及配置
    mysql5.58的编译安装(转)
    源于魔兽!《植物大战僵尸》成功奥秘 (转)
    mysql5.5.8安装问题解决方法(转)
    RHEL5(CentOS)下nginx+php+mysql+tomcat+memchached配置全过程(转)
    已经安装curses,但cmake安装mysql时,依然提示No curses/termcap library found(转)
    centos5安装飞信机器人监控web服务器(转)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464817.html
Copyright © 2020-2023  润新知