POJ 3126 Prime Path(搜索)
来源:互联网 发布:数控车螺纹m24x1.5编程 编辑:程序博客网 时间:2024/04/28 10:58
AC代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <queue>using namespace std;struct node{ int pos,t; bool operator < (const node &a)const{ return t > a.t; }};int prime[10005];int vis[10005];void is_prime(){ memset(prime,0,sizeof(prime)); for(int i = 1000; i < 10000; i++){ int tmp = sqrt(i*1.0),flag = 1; for(int j = 2; j <= tmp; j++){ if(i%j == 0){ flag = 0; break; } } if(flag) prime[i] = 1; }}int bfs(int a,int b){ memset(vis,0,sizeof(vis)); node cur; int num[4]; cur.pos = a;cur.t = 0; vis[a] = 1; priority_queue<node> q; q.push(cur); while(!q.empty()){ cur = q.top(); q.pop(); if(cur.pos == b){ return cur.t; } int tmppos = cur.pos; for(int i = 3; i >= 0; i--,tmppos/=10){ num[i] = tmppos%10; } for(int i = 0; i < 4; i++){ int tmp = num[i]; for(int j = 0; j < 10; j++){ if(num[i] != j){ num[i] = j; int pos = num[0]*1000+num[1]*100+num[2]*10+num[3]; if(prime[pos] && !vis[pos]){ vis[pos] = 1; q.push(node{pos,cur.t+1}); } } num[i] = tmp; } } } return -1;}int main(){ is_prime(); int T; scanf("%d",&T); while(T--){ int a,b; scanf("%d%d",&a,&b); int ans = bfs(a,b); if(ans < 0) printf("Impossible\n"); else printf("%d\n",ans); } return 0;}
0 0
- poj 3126 Prime Path(bfs搜索)
- POJ 3126 Prime Path(基础搜索)
- POJ 3126 Prime Path(搜索)
- POJ 3126-Prime Path【搜索】
- Prime Path - POJ 3126 BFS宽度(广度)优先搜索
- POJ - 3126 Prime Path(15.10.10 搜索专题)bfs
- POJ 3126-Prime Path 简单搜索 BFS
- POJ 3126(Prime Path)
- poj poj 3126 Prime Path(BFS)
- POJ 3126 Prime Path
- POJ 3126 Prime Path
- poj 3126Prime Path
- POJ -3126-Prime Path
- POJ 3126 Prime Path
- poj 3126 Prime Path
- POJ 3126 - Prime Path
- POJ 3126 Prime Path
- poj 3126 prime path
- C++ std::vector元素的内存分配问题
- FFMPEG的用法
- 闰年判断
- 8个小故事,秒懂“执行力”!
- 骗子的逻辑
- POJ 3126 Prime Path(搜索)
- PyCairo指南--基本绘图
- 【转】将Oracle中的表结构导出到word
- next数组介绍
- LeetCode Gray Code
- Mysql函数大全
- POJ3087 Shuffle'm Up
- UESTC 1263 The Desire of Asuna (贪心)
- 基于空间金字塔池化的卷积神经网络物体检测