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),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
随机推荐
- std::shared_ptr
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...
- ECSHOP验证码背景图修改教程
ECSHOP验证码背景图修改教程 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-18 ECSHOP验证码背景图修改教程: ECSHOP前后台的某些地 ...
- 从HashMap透析哈希表
##扯数据结构 先看一下哈希表的概念: 哈希表是一种数据结构,它可以提供快速的插入操作和查找操作.第一次接触哈希表,他会让人难以置信,因为它的插入和删除.查找都接近O(1)的时间级别.用哈希表,很多操 ...
- javascript滚动条之ScrollBar.js
ScrollBar.js是一个仅仅120行的滚动条JS插件,使用非常方便 详情阅读:https://git.oschina.net/wuquanyao/scrollbar.js/*========== ...
- Entity Framework 关系约束配置
前言 简单的说一下自己的理解,大家应该都很明白ADO.NET,也就是原生态的数据库操作,直接通过拼接SQL语句,表与表之间通过链接(inner join left join 或者子查询),也就是在 ...
- 3月23.CSS表格布局
360表格布局: CSS定义标签: @charset "utf-8";/* CSS Document */.bt1{ border:#309 solid 1px; height:1 ...
- editplus快捷键大全之editplus搜索快捷键
前面我们说了editplus快捷键大全之editplus文件快捷键和editplus快捷键大全之editplus光标快捷键,editplus快捷键大全之editplus编辑快捷键这里我们讲一下edit ...
- String的内存分配
1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...
- 在rails下新建表
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天需要新建表,以下是建表语句 rails generate scaffold users ema ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...