hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195
这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个状态都能遍历到。
得到四个数之后,分三种情况处理,每次改变一个数之后都要加入队列,最先输出的就是步数最少。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct point
{
int f[];
int step;
}s,e;
int vis[][][][];
void bfs()
{
memset(vis,,sizeof(vis));
queue<point>que;
s.step=;
que.push(s);
vis[s.f[]][s.f[]][s.f[]][s.f[]]=;
while(!que.empty())
{
point t=que.front(); que.pop();
// printf("%d %d %d %d %d\n",t.f[0],t.f[1],t.f[2],t.f[3],t.step);
if(t.f[]==e.f[]&&t.f[]==e.f[]&&t.f[]==e.f[]&&t.f[]==e.f[]) {printf("%d\n",t.step);return;}
for(int i=;i<;i++)
{
s=t;
if(s.f[i]==) s.f[i]=;
else s.f[i]++;
if(!vis[s.f[]][s.f[]][s.f[]][s.f[]])
{
vis[s.f[]][s.f[]][s.f[]][s.f[]]=;
s.step++;
que.push(s);
}
}
for(int i=;i<;i++)
{
s=t;
if(s.f[i]==) s.f[i]=;
else s.f[i]--;
if(!vis[s.f[]][s.f[]][s.f[]][s.f[]])
{
vis[s.f[]][s.f[]][s.f[]][s.f[]]=;
s.step++;
que.push(s);
}
}
for(int i=;i<;i++)
{
s=t;
s.f[i]=t.f[i+],s.f[i+]=t.f[i];
if(!vis[s.f[]][s.f[]][s.f[]][s.f[]])
{
vis[s.f[]][s.f[]][s.f[]][s.f[]]=;
s.step++;
que.push(s);
}
}
}
}
int main()
{
// freopen("a.txt","r",stdin);
int t;
char s1[],s2[];
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s%s",s1,s2);
// printf("%s %s\n",s1,s2);
for(int i=;i<;i++)
{
s.f[i]=s1[i]-'';
e.f[i]=s2[i]-'';
}
bfs();
}
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1973
这道题很上面那道题一样,也是求一个四位数质数到另一个四位数质数的最小步数,不过这里要求转换的每一步都是质数.
每次只能替换四位中的一位。
我是用了比较笨的方法,把所有情况都找出来。枚举4位数每一位换成0-9中任何一位的情况,然后不断更新一个最小值就行.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; struct point
{
int num[];
int step;
}s,e;
bool is_prime(int a[])
{
int n=;
n=a[]*+a[]*+a[]*+a[];
//printf("%d\n",n);
for(int i=;i*i<=n;i++)
if(n%i==) return false;
return true;
}
int vis[][][][];
int ans;
void bfs()
{
memset(vis,,sizeof(vis));
queue<point>que;
s.step=;
que.push(s);
vis[s.num[]][s.num[]][s.num[]][s.num[]]=;
while(!que.empty())
{
point t=que.front();que.pop();
//printf("%d %d %d %d %d\n",t.num[0],t.num[1],t.num[2],t.num[3],t.step);
if(t.num[]==e.num[]&&t.num[]==e.num[]&&t.num[]==e.num[]&&t.num[]==e.num[]&&t.step<ans)
{
ans=t.step;
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
s=t;
s.num[i]=j;
if(!(i==&&j==)&&!vis[s.num[]][s.num[]][s.num[]][s.num[]]&&is_prime(s.num)) //注意首位不为0
{
vis[s.num[]][s.num[]][s.num[]][s.num[]]=;
s.step=t.step+;
que.push(s);
}
}
}
}
if(ans!=inf)
printf("%d\n",ans);
else printf("Impossible\n");
}
int main()
{
//freopen("a.txt","r",stdin);
int n;
char s1[],s2[];
scanf("%d",&n);
while(n--)
{
scanf("%s%s",s1,s2);
for(int i=;i<;i++)
{
s.num[i]=s1[i]-'';
e.num[i]=s2[i]-'';
//printf("%d %d\n",s.num[i],e.num[i]);
}
if(strcmp(s1,s2)==) printf("0\n");
else
{
ans=inf;
bfs();
}
}
return ;
}
hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)的更多相关文章
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- POJ 3126 Prime Path(BFS 数字处理)
意甲冠军 给你两个4位质数a, b 每次你可以改变a个位数,但仍然需要素数的变化 乞讨a有多少次的能力,至少修改成b 基础的bfs 注意数的处理即可了 出队一个数 然后入队全部能够由这个素 ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- android开发学习--网络请求框架RxJava+Retrofit
看了好多的博客,终于弄清楚了Rxjava和Retrofit,给大家推荐几个不错的文章: https://gank.io/post/56e80c2c677659311bed9841 这个文章是只用Ret ...
- AJPFX总结关于JVM的基础知识
写在前面 之前老大让做一些外包面试,我的问题很简单: 介绍一下工作中解决过比较 有意思的问题. HashMap使用中需要注意的点. 第一个问题主要是想了解一下对方项目经验的含金量,第二个问题则是测试下 ...
- Thinkphp删除缓存
控制器代码 public function delcache(){ //当找到有Runtime的文件夹时,进入if if(is_dir(RUNTIME_PATH)){ delDir(RUNTIME ...
- iOS programming Delegation and Text Input
iOS programming Delegation and Text Input 1.1 Text Fields CGRect textFieldRect = CGRectMake(40, ...
- vue路由细节探讨
1.使用router-link 不会让页面刷新,使用a标签会使页面刷新.2.router-link 里面的to="/路由地址" tag=""自定义标签" ...
- asterisk-java ami3 属性改变监听
asteriskServer.addAsteriskServerListener(new AsteriskListenerInit());//服务属性监听会自动连接服务 实现AsteriskServe ...
- CentOS7 Install Consul
Centos7 Install Consul 原文链接:http://www.cnblogs.com/caoguo/p/5959962.html 1) 环境 2) 安装 # yum install - ...
- linux查看内核版本和发行版本号
1.查看Linux内核版本号:1.1 uname -r #查看当前linux系统的内核版本号显示举例:2.6.21-1.3194.fc71.2 uname -a #可以查看包括内核版本号.机器硬件信息 ...
- WPF学习- AllowDrop 用户控件启用拖放功能
知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...
- [转]解决右键用notepad++打开提示【ShellExecute failed (2): Is this command Correct? (Fix) 】
最近发现右键使用notepad++打开文件时提示如下错误: ShellExecute failed (2): Is this command Correct? ... 经用搜索引擎搜索得知,应该是开启 ...