HDU--1195--bfs--Open the Lock
/*
Name: HDU--1195--Open the Lock
Author: shen_渊
Date: 17/04/17 08:54
Description: bfs,用四维数组标记状态,每位数字可以做三种操作 +1,-1,交换
可以不用优先队列,题意决定,优先队列更费时间
*/
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
int steps;
string s;
node():steps(){
};
bool operator <(const node &a)const{
return steps>a.steps;
}
};
int bfs();
][][][];
string str1,str2;
int main()
{
ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
int n;cin>>n;
while(n--){
memset(vis,,sizeof(vis));
cin>>str1>>str2;
if(str1 == str2)cout<<"0\n";
else cout<<bfs()<<endl;
}
;
}
int bfs(){
priority_queue<node> q;
node p;
p.s = str1;
q.push(p);
vis[p.s[]-]-]-]-;
while(!q.empty()){
node a,temp;
temp = q.top();q.pop();
if(temp.s == str2)return temp.steps;
; i<; ++i) {
//+1
a.s = temp.s;
';
;
]-]-]-]-']){
a.steps = temp.steps + ;
q.push(a);
vis[a.s[]-]-]-]-;
}
//-1
a.s = temp.s;
';
;
]-]-]-]-']){
a.steps = temp.steps + ;
q.push(a);
vis[a.s[]-]-]-]-;
}
//交换
){
a.s = temp.s;
a.s[i] = temp.s[i+];
a.s[i+] = temp.s[i];
]-]-]-]-']){
a.steps = temp.steps + ;
q.push(a);
vis[a.s[]-]-]-]-;
}
}
}
}
;
}
HDU--1195--bfs--Open the Lock的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 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(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
随机推荐
- 【Android Developers Training】 62. 搭建一个OpenGL ES环境
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- laravel blade模板介绍
第一步:首先写好模板 在sites下新建立一个app.blade.php文件 写入模板如: <!DOCTYPT html><html><head> <titl ...
- if和for的几个经典题目
1.有一对幼兔,幼兔1个月后长成小兔,小兔1个月后长成成兔并生下一对幼兔,问几年后有多少对兔子,幼兔.小兔.成兔对数分别是多少. 幼兔 1 小兔 0 成兔 0幼兔 0 小兔 1 成兔 0 幼兔 1 小 ...
- Linux内核互斥锁--mutex
一.定义: /linux/include/linux/mutex.h 二.作用及访问规则: 互斥锁主要用于实现内核中的互斥访问功能.内核互斥锁是在原子 API 之上实现的,但这对于内核用户是不可见 ...
- RedHat安装中文支持和字体
操作系统: Red Hat Enterprise Linux 6.3 x86 安装中文语言支持: yum install "@chinese support" 安装完中文支持后,可 ...
- 算法设计与分析 上机题Mergesort
#include <iostream>using namespace std; #define N 100 int g_array[N]; //存放输入的数字static int ...
- Java ee el表达式
以前在开发的时候,偶尔会遇到jsp页面不支持el表达式的情况. 这个的原因是因为El功能被关闭了, 当时的解决办法是关闭忽略.isELIgnored 设设置 但是为什么有时候不用设置也可以了呢.发现原 ...
- Angular表单控件需要类型和实际值类型不一致时实现双向绑定
适用Angular版本为:>=2.本文同样适用于Ionic这类的基于Angular实现的框架. 本文的思路也适用于控件显示的值和实际的值不一样时实现双向绑定. 1. 问题描述 在使用md2的da ...
- JS事件响应的学习总结
点击事件:ocnlick 鼠标经过事件:onmouseover 鼠标移开事件:onmouseout 获得焦点事件:onfocus 失去焦点事件:onblur 内容选中事件:onselect 文本框内容 ...
- Entity Framework Core 生成跟踪列
本文翻译自<Entity Framework Core: Generate tracking columns>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意:我使用的是 ...