hdu 1195 Open the Lock 双广
//题目大意:求解开4位密码锁的最少步数.
// <变化一次:加1;减1;邻位交换>
思路:简单bfs能过,时间很高,双bfs好点。
简单bfs

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <queue> #include <algorithm> using namespace std; struct nd { int num,st; }; int hash[10000]; int s,t; int tmp[100],k; void swap(char& a,char& b){ char c = a; a = b; b = c; } void fun(int x) { char str[5],i; sprintf(str,"%d",x); k = 0; for(i = 0; i < 4; ++ i) { if(str[i]=='9'){ str[i]='1'; sscanf(str,"%d",tmp+k); k++; str[i]='8'; sscanf(str,"%d",tmp+k); k++; str[i]='9'; }else if(str[i]=='1'){ str[i]='9'; sscanf(str,"%d",tmp+k); k++; str[i]='2'; sscanf(str,"%d",tmp+k); k++; str[i]='1'; }else{ str[i]++; sscanf(str,"%d",tmp+k); k++; str[i] -= 2; sscanf(str,"%d",tmp+k); k++; str[i]++; } } swap(str[0],str[1]); sscanf(str,"%d",tmp+k); k++; swap(str[0],str[1]); swap(str[1],str[2]); sscanf(str,"%d",tmp+k); k++; swap(str[1],str[2]); swap(str[2],str[3]); sscanf(str,"%d",tmp+k); k++; } int bfs() { int i,j; if(s==t)return 0; memset(hash,0,sizeof(hash)); hash[s] = 1; queue<nd>que; nd ps; ps.num = s; ps.st = 0; que.push(ps); while(!que.empty()) { nd qs = que.front(); que.pop(); qs.st++; fun(qs.num); for(i = 0; i < k; ++ i)if(!hash[tmp[i]]) { if(tmp[i]==t)return qs.st; qs.num = tmp[i]; hash[tmp[i]] = 1; que.push(qs); } } return 0; } int main() { int i,j,ans,cas; scanf("%d",&cas); while(cas--) { scanf("%d %d",&s,&t); ans = bfs(); printf("%d\n",ans); }return 0; }
双广:

#include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<bitset> #include<string> #include<climits> #include<cstdio> #include<vector> #include<utility> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define IN puts("in") #define OUT puts("out") #define FR(x) freopen(x,"r",stdin) #define FW(x) freopen(x,"w",stdout) #define MSET(x,y) memset(x,y,sizeof(x)) #define ST system("pause") using namespace std; int hash[10000],hash2[10000]; int s,t; int tmp[100],k; void swap(char& a,char& b){ char c = a; a = b; b = c; } void fun(int x) { char str[5],i; sprintf(str,"%d",x); k = 0; for(i = 0; i < 4; ++ i) { if(str[i]=='9'){ str[i]='1'; sscanf(str,"%d",tmp+k); k++; str[i]='8'; sscanf(str,"%d",tmp+k); k++; str[i]='9'; }else if(str[i]=='1'){ str[i]='9'; sscanf(str,"%d",tmp+k); k++; str[i]='2'; sscanf(str,"%d",tmp+k); k++; str[i]='1'; }else{ str[i]++; sscanf(str,"%d",tmp+k); k++; str[i] -= 2; sscanf(str,"%d",tmp+k); k++; str[i]++; } } sprintf(str,"%d",x); for(i = 0; i < 3; ++ i){ swap(str[i],str[i+1]); sscanf(str,"%d",tmp+k); k++; swap(str[i],str[i+1]); } } int bfs() { int i,j; if(s==t)return 0; memset(hash,-1,sizeof(hash)); memset(hash2,-1,sizeof(hash2)); hash[s] = 0; hash2[t] = 0; queue<int>que; queue<int>que2; que.push(s); que2.push(t); int pre1 = 0; int pre2 = 0; while(1) { while(!que.empty()){ int qs = que.front(); if(hash[qs]!=pre1){ pre1 = hash[qs]; break;} que.pop(); fun(qs); for(i = 0; i < k; ++ i)if(hash[tmp[i]]<0) { hash[tmp[i]] = hash[qs] + 1; if(hash2[tmp[i]]>=0)return hash[tmp[i]]+hash2[tmp[i]]; que.push(tmp[i]); }else if(hash2[tmp[i]]>=0)return hash[tmp[i]]+hash2[tmp[i]]; } while(!que2.empty()){ int qs = que2.front(); if(pre2 != hash2[qs]){ pre2 = hash2[qs]; break;} que2.pop(); fun(qs); for(i = 0; i < k; ++ i)if(hash2[tmp[i]]<0) { hash2[tmp[i]] = hash2[qs] + 1; if(hash[tmp[i]]>=0)return hash2[tmp[i]]+hash[tmp[i]]; que2.push(tmp[i]); }else if(hash[tmp[i]]>=0)return hash2[tmp[i]]+hash[tmp[i]]; } } return 0; } int main() { int i,j,ans,cas; scanf("%d",&cas); while(cas--) { scanf("%d %d",&s,&t); ans = bfs(); printf("%d\n",ans); }return 0; }