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),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
随机推荐
- 锋利的jQuery-4--阻止事件冒泡和阻止默认行为
阻止事件冒泡: 如果嵌套元素分别有自己的click事件,当点击内层元素时外层元素的事件也会被触发. $("span").bind("click", functi ...
- iconv命令详解
功能] 对于给定文件把它的内容从一种编码转换成另一种编码. [描述] -f encoding :把字符从encoding编码开始转换. -t encoding :把字符转换到encoding编码. ...
- FCKeditor漏洞利用
FCKeditor漏洞利用 查看编辑器版本 FCKeditor/_whatsnew.html fckeditor/editor/dialog/fck_about.html —————————————— ...
- 织梦(dedecms) 5.7 /plus/car.php sql注入0day
测试方法: @Sebug.net dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! <?php $host=$argv[1]; $path=$argv[2]; $ ...
- WCF报 当前已禁用此服务的元数据发布的错误
这是 Windows© Communication Foundation 服务. 当前已禁用此服务的元数据发布. 如果具有该服务的访问权限,则可以通过完成下列步骤来修改 Web 或应用程序配置文件以便 ...
- Entity Framework CodeFirst数据迁移
前言 紧接着前面一篇博文Entity Framework CodeFirst尝试. 我们知道无论是“Database First”还是“Model First”当模型发生改变了都可以通过Visual ...
- "微信全球商业创新大赛-创意中国2015"国际MBA商业挑战赛开启
微信商业化进程一直在摸索中前进,从未间断.近日由中欧国际工商学院与腾讯(Tencent)共同发起举办“微信全球商业创新大赛 - 创意中国2015”(We Win with WeChat - INNOV ...
- Linux/Ubuntu tree 命令以树形结构显示文件夹目录结构
1.安装命令工具 sudo apt-get -y install tree 2.可以查看关于tree命令的帮助信息 $ tree --help usage: tree [-adfghilnpqrstu ...
- 最新 DEDECMS SQL 注入 0day
4月29日消息:国内安全研究团队“知道创宇”称截获到最新DEDECMS SQL注入0day,DEDECMS官网目前提供下载的最新版5.7也受影响,截止本告警发出时官方尚未给出补丁或解决方案,此漏洞利用 ...
- RecContentType有哪些
HTML 页面text/javascript `type="text/javascript"` 是比较老的写法IETF 推荐的是 `type="application ...