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 ...
随机推荐
- SpringSecurity的简单使用
导入SpringSecurity坐标 在web.xml中配置过滤器 编写spring-securiy配置文件 编写自定义认证提供者 用户新增时加密密码 配置页面的login和logout 获取登录用户 ...
- [翻译] API测试最佳实践 - 身份验证(Authentication)
API测试最佳实践 - 身份验证 适用等级:高级 1. 概况 身份验证通常被定义为是对某个资源的身份的确认的活动,这里面资源的身份指代的是API的消费者(或者说是调用者).一旦一个用户的身份验证通过了 ...
- iOS之NSAttributedString-------字符属性
NSAttributedString 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFontAttributeName;(字体) N ...
- vue热重载
依据官网使用 webpack 的 Hot Module Replacement API,Vuex 支持在开发过程中热重载 mutation.module.action 和 getter.你也可以在 B ...
- C/C++ 标准输入、输出
一.分类 1.标准输入输出 键盘输入,显示器输出.2.文件输入输出 以外存为对象,即硬盘.光盘等.3.串输入输出 对内存中指定空间进行输入输出. 二.c语言中的输入输出 #include <st ...
- ie11 突然不能加载外部css 很神奇 头部改为 <!DOCTYPE> <html>
<!DOCTYPE html> <html> 改为 <!DOCTYPE> <html> OK了
- lua 之 and or not
and是与运算,返回值如下 如果前者为false或者nil,则返回前者,否则后者 A and B 类似如下语句 if not A then return A else return B end 总结: ...
- react.js工程结构
1.index.html :UI界面入口.挂在点: 2.manifest.json:应用说明 3.package.json:工程说明.依赖说明等 4.source : 代码源文件
- 利用jQuery对li标签操作
<ul class="con" id="products"> <li i=" class=""> < ...
- torch学习笔记(二) nn类结构-Linear
Linear 是module的子类,是参数化module的一种,与其名称一样,表示着一种线性变换. 创建 parent 的init函数 Linear的创建需要两个参数,inputSize 和 outp ...