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时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下 ...
随机推荐
- CentOs yum源安装 nginx
1 更新源 [root@server ~]#rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.no ...
- StringUtils工具类详解
StringUtils判断字符串大概有四种方法: 下面是 StringUtils 判断是否为空的示例: 判断是否为空,但是要注意,空格不算空,这个最好能不用则不用. StringUtils.isEmp ...
- nginx內建模块使用
目录 nginx內建模块使用 1. 內建模块的引入 1.1 查看安装信息 1.2 重新指定配置信息 2. 內建模块的使用 2.1 http_stub_status_module 2.2 http_ra ...
- md5加密--32位16进制小写
public class ttgameMd5 { public final static String MD5(String str) { char hexDigits[] = { // 用来将字节转 ...
- hadoop partitioner个数与reducer个数的试验
job.setPartitionerClass(myPartitioner.class);//设置了5个 job.setNumReduceTasks(2); 1.当分区数等于rducer数量时,正常运 ...
- 自动make工具--CMake
http://www.cnblogs.com/lyq105/archive/2010/12/03/1895067.html http://www.linuxidc.com/Linux/2015-10/ ...
- CentOS7 安装 Node.js
1.首先安装node.js 的版本管理工具 NVM,执行以下命令: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/ ...
- Linux基础知识之挂载详解(mount,umount及开机自动挂载)
Linux基础知识之挂载详解(mount,umount及开机自动挂载) 转载自:http://www.linuxidc.com/Linux/2016-08/134666.htm 挂载概念简述: 根文件 ...
- python 利用pymssql连接MSSQL数据库,简单示例
#-*- coding:GBK -*- import pymssql print 'Connect to the Datebase....' conn = pymssql.connect(host=' ...
- YUM安装(卸载)KDE和GNOME
YUM安装(卸载)KDE和GNOME显示系统已经安装的组件,和可以安装的组件:#yum grouplist 如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装:#yum groupin ...