A - Prime Path

本文介绍了一种基于广度优先搜索(BFS)算法解决特定数值替换问题的方法。该算法在一个四位数范围内寻找从一个素数到另一个素数的最短路径,通过逐一替换数字并检查替换后的数是否为素数来实现。文中详细展示了如何初始化素数判断数组、执行BFS搜索过程以及处理输入输出的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int map[10000];
bool prime[10005];
int a,b;

queue <int> qq;


 

void bfs()
{
	while ( !qq.empty() )
	{
		
		int  point=qq.front();
		
		if (point==b) { return ;    }
		int i,tmp=point;
		
		
		
		for (i=0;i<=9;i++)
		{
			int s=tmp/10*10+i;
			if (s!=tmp && prime[s]==false &&   map[s]==0)
			{   
	 
				qq.push(s); 
				map[s]=map[tmp]+1;
			}
			
		}
			
		for (i=0;i<=9;i++)
		{
			int s=tmp/100*100 +i*10	+ tmp%10;
			if (s!=tmp && prime[s]==false && map[s]==0)
			{   
		 
				qq.push(s);
		map[s]=map[tmp]+1;
			}
			
		}

			
		for (i=0;i<=9;i++)
		{
			int s=tmp/1000*1000+i*100 + tmp%100;
			if (s!=tmp && prime[s]==false && map[s]==0)
			{   
	 
				qq.push(s);
			map[s]=map[tmp]+1;
			}
			
		}

			
		for (i=1;i<=9;i++)
		{
			int s=tmp%1000+i*1000;
			if (s!=tmp && prime[s]==false &&  map[s]==0)
			{   
		 
				qq.push(s);
		map[s]=map[tmp]+1;
			}
			
		}
		

	
	
        qq.pop();
	
	
	}
return  ;	


}


int main()
{
	int i,t,j;
	prime[1]=true;
	
	
	for(i=2;i<=10000;i++)
	{
		if (prime[i]==false)
		{
			for (j=(__int64)i*i;j<=10000;j+=i)
			{
				prime[j]=true; 
			}
		}
	}

 
 
  scanf("%d",&t);
	for (j=1;j<=t;j++)
	{
 	scanf("%d%d",&a,&b);
		while ( !qq.empty() )
		{ 
			qq.pop();
		}
		memset(map,0,sizeof(map));
		
		int tmp=a;
		map[a]=1;
		qq.push(a);
		
		
		bfs();
 

 
		if (map[b]==0) printf("Impossible\n");
		else
			printf("%d\n",map[b]-1);
	}
	
	
	return 0;
}
### Prime Path 测试路径选择方法概述 Prime Path 是一种用于软件工程中的结构化测试技术,旨在通过识别程序控制流图 (CFG) 中的所有基本环路来提高代码覆盖率和测试效率[^1]。 #### 定义与目标 Prime Path 方法的目标是在不重复覆盖其他更短路径的前提下,确保每条独立执行路径至少被测试一次。这有助于发现隐藏在复杂分支逻辑中的潜在缺陷[^2]。 #### 控制流图构建 为了应用 Prime Path 技术,首先需要基于源代码创建一个表示程序流程的有向无环图(DAG),即控制流图(CFG)[^3]。该图表由节点(代表语句或指令)和边(指示可能的转移方向)组成: ```mermaid graph TD; A[Start] --> B{Condition}; B -->|True| C[Action]; B -->|False| D[Else Action]; C --> E[End]; D --> E; ``` #### 路径分类 根据长度不同,可以将 CFG 上的路径分为三类: - **Trivial Paths**: 单一节点到自身的自循环; - **Nonprime Paths**: 可以分解成多个较短子路径组合而成的复合路径; - **Prime Paths**: 无法进一步拆分的独特最小程序单元之间的连接序列[^4]。 #### 算法实现 以下是 Python 实现的一个简单版本算法,用来找出给定 DAG 的所有素数路径(Prime Paths): ```python from collections import defaultdict, deque def find_prime_paths(cfg): """Find all prime paths in a given control flow graph.""" def dfs(node, visited, path_stack): nonlocal result if node not in cfg or len(path_stack) >= max_length: return for neighbor in cfg[node]: new_path = tuple(list(path_stack) + [neighbor]) if new_path not in seen and \ ((len(set(new_path)) == len(new_path)) or any(x in path_stack for x in cycle_nodes)): seen.add(new_path) result.append(new_path) if neighbor != start_node: dfs(neighbor, visited | {node}, path_stack + [neighbor]) # Initialize variables result = [] seen = set() cycles = [] # Store detected loops/cycles # Detect simple cycles first ... # Identify nodes involved in cycles cycle_nodes = set().union(*cycles) # Perform DFS traversal from each starting point for start_node in cfg.keys(): max_length = min(len(cfg), 8) # Limit depth to avoid excessive recursion dfs(start_node, set(), [start_node]) return result ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值