Luggage Lock(ICPC沈阳站BFS)

2 篇文章 0 订阅

Eileen has a big luggage and she would pick a lot of things in the luggage every time when A-SOUL goes out for a show. However, if there are too many things in the luggage, the 4-digit password lock on the luggage will be hard to rotate.

在这里插入图片描述
在这里插入图片描述
链接:https://ac.nowcoder.com/acm/contest/31708/J
来源:牛客网
输入
6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468

输出
1
1
4
5
1
4

从a->b 可以类比0000->C0C1C2C3
c1=abs(a1-b1)等

每一个状态的下一个状态可以有20步
1代表上扭 2代表下扭
“1000”,“0100”,“0010”,“0001”,“1100”,“0110”,“0011”,“1110”,“0111”,“1111”,“2000”,“0200”,“0020”,“0002”,“2200”,“0220”,“0022”,“2220”,“0222”,“2222”

然后进行bfs即可 0000-9999 预处理之后 o(1) 输出

刚开始想用dp 回头试试 四维dp?

#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <time.h>
#include <set>
#include <list>
#include <iostream>

using namespace std;
//map<string,string>ip;
map<string,int>mp; 

typedef long long ll;
const int maxn=500+188;
const ll inf=1e11;
ll n,m,k,flag,cnt,sum;
int ans;/*
struct node{
	int x,y,w;
	//bool operator <(node a) const{
	//	return w<a.w;
	//}
}e[260000];
*/
//string dir[30]={"1000","0100","0010","0001","1100","0110","0011","1110","0111","1111","2000","0200","0020","0002","2200","0220","0022","2220","0222","2222"};
string dir[30]={"1000","0100","0010","0001","1100","0110","0011","1110","0111","1111","2000","0200","0020","0002","2200","0220","0022","2220","0222","2222"};

string a,b;

void bfs(){
	
	mp.clear(); mp["0000"]=0;
	queue<string>q;
	q.push("0000");
	
	while(q.size()){
		string tt=q.front(); q.pop();
		for(int i=0;i<20;i++){
			string t=tt,d=dir[i];
			for(int j=0;j<4;j++){
				if(d[j]=='1'){
					t[j]=(t[j]-'0'+1+10	)%10+'0';
				}else if(d[j]=='2') t[j]=(t[j]-'0'-1+10	)%10+'0';
			}
			if(!mp.count(t))	{		
				mp[t]=mp[tt]+1;
				q.push(t);
				if(t==a) return ;//p[t];
				
			}
		}
	}
} 
int main() {
	 int t; cin>>t;bfs();
	 while(t--){
	 	cin>>a>>b;
	 	for(int i=0;i<4;i++) a[i]=(a[i]-b[i]+10)%10+'0';
	 	//\<<bfs()<<endl;
	 	cout<<mp[a]<<'\n';	
	 }	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值