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';
}
}