POJ-3126 暑假集训-搜索进阶F题
经验就是要认真细心,要深刻理解。num
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<queue>
using namespace std;
#define N 11000
typedef struct yes
{
int l, m;//location, money;
}YES;
YES s, e, p, q;
int isprime(int m);
bool visit[N];
void BFS();
int main()
{
int n;//number
cin >> n;
while(n--)
{
cin >> s.l >> e.l;
s.m=0;
BFS();
}
return 0;
}
void BFS()
{
int i, j, num, figure;
char s1[5], s2[5];
memset(visit, false, sizeof(visit));
queue<YES>que;
que.push(s);
visit[s.l]=true;
while(que.size())
{
p=que.front();
que.pop();
if(p.l==e.l)
{
cout << p.m << endl;
return;
}
figure=p.l;
for(i=3; i>=0; i--)
{
s1[i]=figure%10+'0';
figure/=10;
}
s1[4]='\0';
for(i=0; i<4; i++)
{
strcpy(s2, s1);
for(j=0; j<=9; j++)
{
if((i==0&&j==0)||s2[i]==j+'0')
continue;
s2[i]=j+'0';
num=atoi(s2);
if(num&1==0)
continue;
//if(num%2==0)
//continue;
if(isprime(num)&&!visit[num])
{
visit[num]=true;
q.l=num;
q.m=p.m+1;
que.push(q);
}
}
}
}
cout << "Impossible" << endl;
}
int isprime(int m)
{
int i;
int k=sqrt(m);
k=int(k);
for(i=2; i<=k; i++)
if(m%i==0)
return 0;
return 1;
}
POJ-3126 暑假集训-搜索进阶F题的更多相关文章
- poj3984《迷宫问题》暑假集训-搜索进阶
K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- HDU2612 -暑假集训-搜索进阶N
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/N这两天总是因为一些小错误耽误时间,我希望自己可以细心点.珍惜 ...
- Oil Deposits -----HDU1241暑假集训-搜索进阶
L - Oil Deposits Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
Eight Puzzle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...
- 暑假集训——cf热身赛部分题有感加其题解
刚刚开始集训,集训队队长暂时还没有拉专题,而是拉了部分codeforces上过题人数在2000左右的题组成了一场热身赛(其实就是一场练习),花了一天时间终于把它刷完了,其中很多题让我学到了很多骚操作, ...
- 2016huasacm暑假集训训练五 F - Monkey Banana Problem
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...
- 2016huasacm暑假集训训练三 F - Jungle Roads
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...
- 2016HUAS暑假集训训练2 F - A Simple Problem with Integers
Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). " ...
- AYIT-2020 609暑假集训第一周周赛题 A - A计划
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下 ...
随机推荐
- 【JQuery Easy UI】后台管理系统的简单布局分享
重要说明:本博已迁移到 石佳劼的博客.有疑问请到 文章新地址 留言..! 近期做的一个简单的后台管理系统,当中用到了JQuery Easy UI框架,对于撸主这样的把控件能摆整齐就谢天谢地的码农来说, ...
- 教你用squid做CDN把公司做到上市
我们都知道CDN(内容分发网络)是用来给网站加速用的,通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络的“边缘”,使用户可以就近取得所需的内容,以提高用户访问网站 ...
- C++中没有定义类的引用。
在有时候由于类太大.须要在类在后面定义: 比如: class Y{ void f(X); }; class X{ //一些成员数据和函数 }; //error 由于c++要求不论什么一个变量在引用之前 ...
- Eclipse Plugin Installation and Windows User Access Control
I make Eclipse Plugins and I sell them to developers using Eclipse. Most of the visitors to my web s ...
- Python之布尔运算符
python中的布尔运算符有三种,or,and,not. 布尔运算,根据升序优先进行排序.运算 | 结果 | 提示=============================x or y | x假时,执 ...
- hdu 3367 Pseudoforest(并查集)
题意:有一种叫作Pseudoforest的结构,表示在无向图上,每一个块中选取至多包含一个环的边的集合,又称“伪森林”.问这个集合中的所有边权之和最大是多少? 分析:如果没有环,那么构造的就是最大生成 ...
- Maven的配置地址
http://howtodoinjava.com/tools/eclipse/how-to-import-maven-remote-archetype-catalogs-in-eclipse/ htt ...
- 从分类,排序,top-k多个方面对推荐算法稳定性的评价
介绍 论文名: "classification, ranking, and top-k stability of recommendation algorithms". 本文讲述比 ...
- git学习之简介(一)
一.前言 史上最浅显易懂的Git教程! 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google了一堆Git相关的文章和教程,但令人失望的是,这些教程不是难得令人发指,就是简单得 ...
- iptables启动脚本分析
#!/bin/sh # # iptables Start iptables firewall # # chkconfig: 2345 08 92 # description: Starts, stop ...