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> ...
随机推荐
- DeepFaceLab小白入门(5):训练换脸模型!
训练模型,是换脸过程中最重要的一部分,也是耗时最长的一部分.很多人会问到底需要多少时间?有人会告诉你看loss值到0.02以下就可以了.我会告诉你,不要看什么数值,看预览窗口的人脸.看第二列是否和第一 ...
- 消息中间件ActiveMQ及Spring整合JMS
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- day 37 MySQL行(记录)的详细操作
MySQL行(记录)的详细操作 阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操 ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- PAT Basic 1079
1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 ak⋯a1a0 的形式,其中对所有 i 有 0≤ai<10 且 ak>0.N 被称 ...
- python基础学习笔记——类的成员
一. 细分类的组成成员 之前咱们讲过类大致分两块区域,如下图所示: 每个区域详细划分又可以分为: class A: company_name = '老男孩教育' # 静态变量(静态字段) __ipho ...
- luogu2766 最长不下降子序列问题
第一问DP水过.dp[i]代表以i结尾的最长不下降子序列长度. 二三问网络流. 第二问是说每个子序列不能重复使用某个数字. 把每个点拆成p(i),q(i).连边. 要是dp[i]=1,连源,p(i) ...
- 教你玩App怎么赚钱(一)
在看这篇文章之前,你一定要接受一下谋哥的观点:金钱就是价值流通的手段,不要高看了钱. 玩App怎么赚钱?貌似谋哥写的文章超级多,把这个最重要的忘记了.说实在的,我为啥要写“玩App"呢?其实 ...
- Selenium WebDriver- 操作JavaScript的Alert弹窗
弹层和弹框是有区别的,弹框是那种完全没样式的框子:弹层是可以直接看到html的,有样式 #encoding=utf-8 import unittest import time from seleniu ...
- day01_01.了解php
1.了解PHP 第一个程序 echo 'hello world'; 和python的区别,python是 print (hello world) 并且python结尾没有;2.X版本不需要加括号,但是 ...