hdu 1195(搜索)
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5970 Accepted Submission(s): 2666
an emergent task for you is to open a password lock. The password is
consisted of four digits. Each digit is numbered from 1 to 9.
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.
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<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
bool vis[][][][];
struct Node{
int v[];
int step;
};
Node s,t;
bool _equal(Node a,Node b){
for(int i=;i<;i++){
if(a.v[i]!=b.v[i]) return false;
}
return true;
}
Node operate(int x,Node now){
Node next;
for(int i=;i<;i++){
next.v[i] = now.v[i];
}
next.step=now.step+;
if(x<){ ///+
if(now.v[x]==) next.v[x]=;
else next.v[x]=now.v[x]+;
}else if(x<){ ///-
if(now.v[x%]==) next.v[x%]=;
else next.v[x%]=now.v[x%]-;
}else{ ///exchange
int a = now.v[x%];
int b = now.v[x%+];
next.v[x%]=b;
next.v[x%+] = a;
}
return next;
}
int bfs(Node s){
memset(vis,false,sizeof(vis));
queue <Node>q;
vis[s.v[]][s.v[]][s.v[]][s.v[]]=true;
q.push(s);
s.step = ;
while(!q.empty()){
Node now = q.front();
q.pop();
if(_equal(now,t)){
return now.step;
}
for(int i=;i<;i++){ ///总共11种操作,[1-4]+ [1-4]- exwchange[1,2][2,3][3,4]
Node next=operate(i,now);
if(vis[next.v[]][next.v[]][next.v[]][next.v[]]==false){
vis[next.v[]][next.v[]][next.v[]][next.v[]]=true;
q.push(next);
}
}
}
return -;
}
int main()
{
char s1[],s2[];
int tcase;
scanf("%d",&tcase);
while(tcase--){ scanf("%s",s1);
scanf("%s",s2);
for(int i=;i<;i++){
s.v[i]=s1[i]-'';
t.v[i]=s2[i]-'';
}
int res = bfs(s);
printf("%d\n",res);
} return ;
}
hdu 1195(搜索)的更多相关文章
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Square HDU 1518 搜索
Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- hdu 1195 广度搜索
这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的.需要注意的时,加个vi[]数组,判读当前数是否已经存在于队列中.我做的很烦啊~~~ #include&l ...
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- hdu 4848 搜索+剪枝 2014西安邀请赛
http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为 当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...
- hdu 1495 (搜索) 非常可乐
http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...
随机推荐
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- Golang 简单web测试
// mhoso project main.go package main import ( "log" "net/http" "./controll ...
- Python基础学习总结__Day2
一.模块初始 1.标准库模块: (1) Os模块 ① 和操作系统交互:例:执行命令代码 (2) Sys模块 ① 脚本+参数——>结果 2.第三方库模块:Django,Mysql... 存在E:\ ...
- SQL语句小练习
一.创建如下表结构(t_book) Id 主键 自增一 bookName 可变长 20 Price 小数 Author 可变长20 bookTypeId 图书类 ...
- python数据类型之字符串(str)和其常用方法
字符串是有序的,不可变的. 下面的例子说明了字符串是不可变的 name = 'alex' name = 'Jack' """ 并没有变,只是给name开启了一块新内存,储 ...
- 关于stm32优先级大小的理解
转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4 组别优先顺序(第0组优先级最强,第4组优 ...
- Android开发——AsyncTask的使用以及源码解析
.AsyncTask使用介绍 转载请标明出处:http://blog.csdn.net/seu_calvin/article/details/52172248 AsyncTask封装了Thread和 ...
- BZOJ 3351: [ioi2009]Regions
对于一个询问(x,y)对y出现次数分类,若<=lim,在儿子处统计答案,若>lim则y的种类肯定<lim,在祖先处统计(仿佛要去重?但是没去重也过了,那个时限仿佛怎么做都能过) #i ...
- UVa 1629 DP Cake slicing
题意: 一块n×m的蛋糕上有若干个樱桃,要求切割若干次以后,每块蛋糕上有且仅有1个樱桃.求最小的切割长度. 分析: d(u, d, l, r)表示切割矩形(u, d, l, r)所需要的最小切割长度. ...
- 【Jenskins】安装与配置
Jenskins教程:http://www.yiibai.com/jenkins/ 一.Jenskins的安装 1.jenskins下载和启动 Jenskins下载地址:https://jenkins ...