广搜 poj3278 poj1426 poj3126
Catch That Cow
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue> using namespace std;
#define max 100002 ///看了别人的代码 说广搜的标记数组 会很大 如果开成局部的会PE
bool vis[max];
int deep[max];
int n,k; int bfs()
{
queue<int>q;
q.push(n);
memset(vis,false,sizeof(vis)); ///全没有遍历过
vis[n]=true; ///起点遍历过了。。
memset(deep,,sizeof(deep)); ///初始都定义为0 走了0步 17
deep[n]=; while(!q.empty())
{
int tp=q.front();
q.pop();
vis[tp]=true;
if(tp==k)
break;
else
{
int ans1=tp+;
int ans2=tp-;
int ans3=tp*;
if(ans1<=&&!vis[ans1])
{
q.push(ans1);
deep[ans1]=deep[tp]+;
//cout<<deep[ans1]<<endl;
vis[ans1]=true;
}
if(ans2>=&&!vis[ans2]) ///注意边界条件 刚刚>0 WA了
{
q.push(ans2);
deep[ans2]=deep[tp]+;
//cout<<deep[ans2]<<endl;
vis[ans2]=true; }
if(ans3<=&&!vis[ans3])
{
q.push(ans3);
deep[ans3]=deep[tp]+;
// cout<<deep[ans3]<<endl;
vis[ans3]=true; }
}
}
return deep[k];
} int main()
{
cin>>n>>k;
printf("%d\n",bfs());
return ;
}
第一道广搜题 参考别人的代码~~~~http://kymowind.blog.163.com/blog/static/1842222972011717112727688/
poj1426 http://blog.csdn.net/lyy289065406/article/details/6647917
Prime Path
Time Limit: 1000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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
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
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0 题意:从第一个数编导第二个数最少需要多少步 要求:每次只能改变一个数且这个数与之前的数都不一样 同时这个数时素数
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <math.h> using namespace std;
int prime[]; void inin()
{
int i,j;
for(i=;i<=;i++){
for(j=;j<i;j++)
if(i%j==){
prime[i]=false;
break;
}
if(j==i) prime[i]=true;
}
} int vis[]; ///是否遍历过
int t[]; ///存放各个位的数
int num[]; ///最后输出的值 int bfs(int n,int m)
{
int temp;
queue<int>q;
q.push(n);
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
vis[n]=;
memset(t,,sizeof(t)); while(!q.empty())
{
int tp=q.front();
q.pop();
if(tp==m)
return num[tp]; t[]=tp/;
t[]=tp%/;
t[]=tp%/;
t[]=tp%; for(int i=;i<;i++)
{
temp=t[i];
for(int j=;j<=;j++)
if(temp!=j)
{
t[i]=j;
int ans=t[]*+t[]*+t[]*+t[]; if(!vis[ans]&&prime[ans])
{
num[ans]=num[tp]+;
vis[ans]=;
q.push(ans);
}
if(ans==m)
return num[ans];
}
t[i]=temp;
}
}
return -;
} int main()
{
int n,m,tt;
inin();
cin>>tt;
while(tt--)
{
cin>>n>>m;
if(bfs(n,m)!=-)
cout<<bfs(n,m)<<endl;
else
cout<<"Impossible"<<endl;
}
return ;
}
之前判断素数的函数不是这样写的 一直WA 可是我的错在哪里了呢 先贴上 以后看看
int prim(int n)
{
for(int i=;i<sqrt(n);i++)
{
if(n%i==)
{
return false;
break;
}
}
return true;
}
广搜 poj3278 poj1426 poj3126的更多相关文章
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- 双向广搜 codevs 3060 抓住那头奶牛
codevs 3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...
随机推荐
- Python Json模块中dumps、loads、dump、load函数介绍
1.json.dumps() json.dumps()用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json文件中会发生报错,因此在将数据写入时需要用到该函数. import ...
- python imaplib无痕取信的主要
typ, data = M.fetch(num, (UID BODY.PEEK[]))
- Intellij IDEA 快捷键整理-鬼畜版(全键盘开发指南)
一 .何为鬼畜? 鬼畜一词在ACG爱好者中也代指通过影片(或音讯)剪辑,用频率极高的重复画面(或声音)组合而成的一段节奏配合音画同步率极高的一类影片,而这类鬼畜影片多见于NICONICO.AcFun和 ...
- windows driver
C:\Windows\System32\DriverStore\FileRepository
- Firebug & Chrome Console 控制台使用指南
转自:http://visionsky.blog.51cto.com/733317/543789 Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),wi ...
- mybatis不报错,但是查询结果为0
[转载]https://blog.csdn.net/shenzhenNBA/article/details/46673327 在用MyBatis操作数据库的时候相信很多人都用到,当在判断null, 大 ...
- mysql explain中的type列含义和extra列的含义
很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是“访问类型”,更通俗一点就是: ...
- Servet-------JSTL标签库
JSTL标签库 也可以和EL表达式配合使用 作用: 提高在Jsp中的逻辑代码的编写效率,使用标签..(对EL表达式的扩展) 使用: JSTL的核心标签库(重点) JSTL的SQL标签库 JST ...
- centos7 hbase 搭建笔记
1.require:java环境,本地可用的hadoop 2.拷贝hbase文件(hive-1.2.6) 3.设置环境变量 export HBASE_HOME=/data/spark/bin/hbas ...
- vsftpd只能连接不能上传文件问题
Centos7 记得很清楚,vsftpd安装后,不需要配置,本地用户就可以正常使用(登录.上传.下载) 这次配的就是不行,另起了个虚拟机,装了下,就是不需要配置,但是在一台机上,就是不行,只能登录,下 ...