hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3847 Accepted Submission(s): 1661
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.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node{
char a[];
int step;
};
char a[],b[];
bool isv[][][][];
int bfs()
{
memset(isv,,sizeof(isv));
queue <Node> q;
Node cur,next;
int i;
strcpy(cur.a,a);
cur.step = ;
isv[cur.a[]-''][cur.a[]-''][cur.a[]-''][cur.a[]-''] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
//printf("%d\t%s\n",cur.step,cur.a);
for(i=;i<;i++)
if(cur.a[i] != b[i])
break;
if(i>=) //找到
return cur.step; //前八种,4个位置的数字+1或者-1
for(i=;i<;i++){
int nc = cur.a[i/]-'';
if(i%) //+1
nc = nc%+;
else //-1
nc = nc==?:nc-;
next = cur;
next.a[i/] = nc + '';
if(isv[next.a[]-''][next.a[]-''][next.a[]-''][next.a[]-''])
continue;
//可以放
isv[next.a[]-''][next.a[]-''][next.a[]-''][next.a[]-''] = true;
next.step = cur.step + ;
q.push(next);
}
//后三种,相邻的数字交换位置
for(i=;i<;i++){
next = cur;
char t = next.a[i];
next.a[i] = next.a[i+];
next.a[i+] = t;
if(isv[next.a[]-''][next.a[]-''][next.a[]-''][next.a[]-''])
continue;
isv[next.a[]-''][next.a[]-''][next.a[]-''][next.a[]-''] = true;
next.step = cur.step + ;
q.push(next);
}
}
return -;
}
int main()
{
int i,T;
scanf("%d",&T);
for(i=;i<=T;i++){
scanf("%s",a);
scanf("%s",b);
if(i!=) //读取空行
scanf("%[^\n]%*");
int step = bfs();
printf("%d\n",step);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1195:Open the Lock(暴力BFS广搜)的更多相关文章
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- 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 (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
随机推荐
- js中按钮控制显示隐藏以及下拉功能
<script> function show() { var a2=document.getElementById("div2"); if(a2.style.displ ...
- lnux下源码安装MySQL 5.6
nux下源码安装MySQL 5.6 说明:本文是我自己测试的MySQL5.6源码安装,经本人亲自实践,完全可用,另在5.6之前的版本也是可以按照本文源码安装的.我是在两台linux下一台安装5.5,另 ...
- Spring实战学习笔记之SpEL表达式
在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现 SpEL表达式的首要目标是通过计算获得某个值. ...
- mysql gb2312与lanti1
1.如果数据库编码为lanti1,页面编码utf-8和gb2312均可,并且不用set names,设置就会乱码: 2.如果数据库编码为utf8,页面编码utf-8和gb2312均可,一定要设置好se ...
- object-c 的ARC 问答/介绍
原文:http://blog.csdn.net/kmyhy/article/details/8895606 概念" Clangstatic analyzer "是一个非常有用的查找 ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
- String的内存分配
1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...
- App开发到App Store上架,发布流程。
http://blog.csdn.net/wojsg001/article/details/12005887 App开发到App Store上架,发布流程. 分类: IOS2013-09-25 11 ...
- UIImagePickerController详解
转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093 1.UIImagePickerController的静态方法: imagep ...
- PCA
理论部分可以看斯坦福大学的那份讲义,通俗易懂:http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html opencv中有PCA这 ...