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. The mean shift clustering algorithm

    The mean shift clustering algorithm MEAN SHIFT CLUSTERING Mean shift clustering is a general non-par ...

  2. Nginx变量的实现机制

    Nginx有两种定义变量的方式,一种是在配置文件中使用set指令(由rewrite模块提供支持),另一种是在模块内定义变量. 变量相关结构体: struct ngx_http_variable_s { ...

  3. 安装PL/SQL Developer 遇到的问题及解决方法

    在用PL/SQL Developer安装Oracle客户端时,报错误,初始化失败,一直找不到原因,换Oracle版本也解决不了问题,之后才发现,是Oracle的环境变量配置错了,之前用户配了Oracl ...

  4. Run UliPad 4.1 Under Windows 7 64bit and wxPython 3.0.2

    Abstract: UliPad that is developed by limodou is an excellent code editor. It works well with wxPyth ...

  5. 悦动达人 (多维dp)

    悦动达人 Description 一个游戏,在屏幕上有5个格子形成一行,每一秒都会有一个格子闪烁,格子闪烁时你需要保证至少有一只手指在格子上面, 现在我们已经知道第i秒时,第xi个格子会闪烁,我们假设 ...

  6. Ninth scrum meeting - 2015/11/3

    今天课上老师询问了每个团队的进度,我们发现有好多团队都已经基本完成了, 距离预定的alpha版本开发完成时间也越来越近了,我们的工作也都在有条不紊的进行着.今天又出现了git pull时有冲突的情况, ...

  7. [Android教程]EditText怎样限制用户的输入?数字/字母/邮箱

    有输入必有验证.为了防止用户随便输入确保提交数据的合法性,程序不得不在文本输入框(EditText)中增加限制或验证. 关于输入类型有数字.字母.邮箱.电话等形式,这些具体得根据业务来.那么Andro ...

  8. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

  9. bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  10. Linux Haproxy 安装和部署

    一.Haproxy 安装 下载地址 http://pan.baidu.com/s/1mggViXE cd /usr/local tar xzvf haproxy-.tar.gz cd haproxy- ...