[HUD 1195] Open the Lock
Open the Lock
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
Input
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
1234
2144
1111
9999
4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000 struct Node{
int n,t;
Node(){}
Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
int d,t;
Vis(){}
Vis(int _d,int _t):d(_d),t(_t){}
}; int s,e;
Vis vis[N]; inline void getd(int n,int *a)
{
int k=;
while(n){
a[k++]=n%;
n/=;
}
}
inline int getn(int *a)
{
int n=;
for(int i=;i>=;i--){
n=n*+a[i];
}
return n;
}
int bfs()
{
int sp=; //层数控制
int t1[],t2[];
Node now,next;
memset(vis,,sizeof(vis));
queue<Node> p,q;
p.push(Node(s,));
q.push(Node(e,));
vis[s]=Vis(,);
vis[e]=Vis(,);
while(!p.empty() && !q.empty()){
while(p.front().t==sp){
Node now=p.front();
p.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
}
while(q.front().t==sp){
Node now=q.front();
q.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
}
sp++;
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&e);
printf("%d\n",bfs());
}
return ;
}
[HUD 1195] Open the Lock的更多相关文章
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- BFS && DFS
HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...
- CREATE A ENERGY / HEALTH BAR HUD
Now then, let's get started. 1. Open the Play scene which you had created in the previous post. If y ...
随机推荐
- Netty 中文教程 Hello World !详解
1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...
- Win7(包括32和64位)使用GitHub
关于安装路径:32位可选择安装目录,但64位建议使用默认安装目录,否则Git Extensions配置会出问题 安装参考网址 http://code.google.com/p/tortoisegit/ ...
- struts文件上传拦截器maximumSize设置文件大小不起作用
<interceptor-ref name="fileUpload"> <param name="allowedTypes ...
- 能"干掉"苹果的中国"黑客"
他是全球发现苹果漏洞最多的人,他曾穷的住在小黑屋,他经常接到国家安全部门的电话,他差点堵住周鸿祎的路,他是谁? 无名英雄 我们最终还是没有见到吴石本人,即便他的生意合伙人刘盛(化名)已经应承了帮我们牵 ...
- log4j记录exception异常有价值信息及log4j级别
try{ }catch(Exception e){ e.printStackTrace(); log.error("配件导出excel错误:", e.fillInStackTrac ...
- Ubuntu环境下Hadoop1.2.1, HBase0.94.25, nutch2.2.1各个配置文件一览
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- hdu 4315 Climbing the Hill 博弈论
题意:有n个人爬山,山顶坐标为0,其他人按升序给出,不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面的人,且不能和前面一个人在相同的位置.现在有一个 ...
- 恢复mdf文件到数据库方法
CREATE DATABASE crm_testdb1 ON (FILENAME = N'C:\e527051\crm_testdb\crm_testdb_20121104.mdf')FOR ATTA ...
- 1050 Moving Tables
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 朴素贝叶斯方法(Naive Bayes Method)
朴素贝叶斯是一种很简单的分类方法,之所以称之为朴素,是因为它有着非常强的前提条件-其所有特征都是相互独立的,是一种典型的生成学习算法.所谓生成学习算法,是指由训练数据学习联合概率分布P(X,Y ...