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

Problem Description
Now 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.

 
Input
The input file begins with an integer T, indicating the number of test cases.

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.

 
Output
For each test case, print the minimal steps in one line.
 
Sample Input
2
1234
2144
 
1111
9999
 
Sample Output
2
4
 
Author
YE, Kai
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1072 1242 1240 1044 1254 

 
  暴力BFS广搜
  利用广搜穷举所有情况。注意每一位可+1可-1,一共有4位,这就是8种情况,另外相邻两位可以交换位置,这又是3种情况。所以每一个节点一共要处理11种情况。用一个isv[][][][]四维数组记录可能出现的数字串出现过没有。例如,"2143"出现过了,则 isv[2][1][4][3]=true。
  代码
 #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广搜)的更多相关文章

  1. hdu 1180:诡异的楼梯(BFS广搜)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  2. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. 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 ...

  6. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  7. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

  8. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  9. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...

随机推荐

  1. linux kernel thread(Daemons)

    内核线程是直接由内核本身启动的进程.内核线程实际上是将内核函数委托给独立的进程,与系统中其他进程“并行”执行(实际上,也并行于内核自身的执行),内核线程经常被称为内核“守护进程”.它们主要用于执行下列 ...

  2. mysql PDO的使用

    原文链接:http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/08/12/1797753.html

  3. C语言之参数传递

    学了四年的计算机,一直让自己比较苦恼的问题是C语言的参数传递问题,之所以说是苦恼,是因为在某年的一个学期,不幸接触到数据结构,光一个链表就把自己弄得死去活来的,而且自已一直就楞以为在操作的过程中,传递 ...

  4. Android往SD卡上存储文件

    public class DataActivity extends Activity { private EditText filenameText; private EditText content ...

  5. win7电脑安装wamp出现httpd.exe无法找到组件MSVCR100.dll的解决办法

    昨天重新安装了系统,今天想装一个wamp,在安装过程中报httpd.exe无法找到组件 如图u 运行wamp,发现图标是黄色的,apache没有选中,测试80端口也未被占用. 重新卸载了,又安装了一遍 ...

  6. CF440C

    C. One-Based Arithmetic time limit per test 0.5 seconds memory limit per test 256 megabytes input st ...

  7. C# Window Service详解

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  8. Poj 1061 青蛙的约会(扩展GCD)

    题目链接:http://poj.org/problem?id=1061 解题报告:两只青蛙在地球的同一条纬度线上,选取一个点位坐标轴原点,所以现在他们都在同一个首尾相连的坐标轴上,那么他们现在的位置分 ...

  9. glut64位操作系统安装

    64位win7下OpenGL的配置 - walkandthink的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/walkandthink/article/detai ...

  10. 粒子滤波particle filter和目标跟踪

    粒子滤波用于跟踪,参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/18/2404817.html http://blog.csdn.net/ ...