[HUD 1195] Open the Lock
13293 단어 Lock
Open the Lock
Problem Description
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
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.
Input
The input file begins with an integer T, indicating the number of test cases.
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.
Output
For each test case, print the minimal steps in one line.
Sample Input
2 1234 2144 1111 9999
Sample Output
2 4
Author
YE, Kai
첫 번째 양방향 BFS 문제, --#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000
struct Node{
int n,t;
Node(){}
Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
int d,t;
Vis(){}
Vis(int _d,int _t):d(_d),t(_t){}
};
int s,e;
Vis vis[N];
inline void getd(int n,int *a)
{
int k=0;
while(n){
a[k++]=n%10;
n/=10;
}
}
inline int getn(int *a)
{
int n=0;
for(int i=3;i>=0;i--){
n=n*10+a[i];
}
return n;
}
int bfs()
{
int sp=0; //
int t1[4],t2[4];
Node now,next;
memset(vis,0,sizeof(vis));
queue<Node> p,q;
p.push(Node(s,0));
q.push(Node(e,0));
vis[s]=Vis(1,0);
vis[e]=Vis(2,0);
while(!p.empty() && !q.empty()){
while(p.front().t==sp){
Node now=p.front();
p.pop();
// 1
getd(now.n,t1);
for(int i=0;i<8;i++){
memcpy(t2,t1,sizeof(t1));
if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==1) continue;
if(vis[next.n].d==2) return next.t+vis[next.n].t;
vis[next.n].d=1;
vis[next.n].t=next.t;
p.push(next);
}
//
for(int i=0;i<3;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+1]);
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==1) continue;
if(vis[next.n].d==2) return next.t+vis[next.n].t;
vis[next.n].d=1;
vis[next.n].t=next.t;
p.push(next);
}
}
while(q.front().t==sp){
Node now=q.front();
q.pop();
// 1
getd(now.n,t1);
for(int i=0;i<8;i++){
memcpy(t2,t1,sizeof(t1));
if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==2) continue;
if(vis[next.n].d==1) return next.t+vis[next.n].t;
vis[next.n].d=2;
vis[next.n].t=next.t;
q.push(next);
}
//
for(int i=0;i<3;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+1]);
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==2) continue;
if(vis[next.n].d==1) return next.t+vis[next.n].t;
vis[next.n].d=2;
vis[next.n].t=next.t;
q.push(next);
}
}
sp++;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&e);
printf("%d
",bfs());
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java Lock 인터페이스 상세 및 실례 코드
java Lock 커넥터
java.util.concurrent.locks
인터페이스 잠금
public interface Loce
Loce는 synchronized 방법과 문장을 사용하는 것보다 더 광범위한 잠금 조작...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
Problem Description
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
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.
Input
The input file begins with an integer T, indicating the number of test cases.
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.
Output
For each test case, print the minimal steps in one line.
Sample Input
2 1234 2144 1111 9999
Sample Output
2 4
Author
YE, Kai
첫 번째 양방향 BFS 문제, --
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000
struct Node{
int n,t;
Node(){}
Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
int d,t;
Vis(){}
Vis(int _d,int _t):d(_d),t(_t){}
};
int s,e;
Vis vis[N];
inline void getd(int n,int *a)
{
int k=0;
while(n){
a[k++]=n%10;
n/=10;
}
}
inline int getn(int *a)
{
int n=0;
for(int i=3;i>=0;i--){
n=n*10+a[i];
}
return n;
}
int bfs()
{
int sp=0; //
int t1[4],t2[4];
Node now,next;
memset(vis,0,sizeof(vis));
queue<Node> p,q;
p.push(Node(s,0));
q.push(Node(e,0));
vis[s]=Vis(1,0);
vis[e]=Vis(2,0);
while(!p.empty() && !q.empty()){
while(p.front().t==sp){
Node now=p.front();
p.pop();
// 1
getd(now.n,t1);
for(int i=0;i<8;i++){
memcpy(t2,t1,sizeof(t1));
if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==1) continue;
if(vis[next.n].d==2) return next.t+vis[next.n].t;
vis[next.n].d=1;
vis[next.n].t=next.t;
p.push(next);
}
//
for(int i=0;i<3;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+1]);
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==1) continue;
if(vis[next.n].d==2) return next.t+vis[next.n].t;
vis[next.n].d=1;
vis[next.n].t=next.t;
p.push(next);
}
}
while(q.front().t==sp){
Node now=q.front();
q.pop();
// 1
getd(now.n,t1);
for(int i=0;i<8;i++){
memcpy(t2,t1,sizeof(t1));
if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==2) continue;
if(vis[next.n].d==1) return next.t+vis[next.n].t;
vis[next.n].d=2;
vis[next.n].t=next.t;
q.push(next);
}
//
for(int i=0;i<3;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+1]);
next.t=now.t+1;
next.n=getn(t2);
if(vis[next.n].d==2) continue;
if(vis[next.n].d==1) return next.t+vis[next.n].t;
vis[next.n].d=2;
vis[next.n].t=next.t;
q.push(next);
}
}
sp++;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&e);
printf("%d
",bfs());
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java Lock 인터페이스 상세 및 실례 코드java Lock 커넥터 java.util.concurrent.locks 인터페이스 잠금 public interface Loce Loce는 synchronized 방법과 문장을 사용하는 것보다 더 광범위한 잠금 조작...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.