ZOJ2416
#include <iostream>
#include <queue>
#define N 10002
using namespace std;
typedef struct
{
int data;
int tt;//标示尽tt此变换可至此data
}Node;
bool hash[N];
int result;
void init()
{
for(int i = 0; i < N; i++)
hash[i] = false;
}
void formArray(int a,int *p)
{
int i;
for(i = 3; i >= 0; i--)
{
p[i] = a % 10;
a /= 10;
}
}
int formDigit(int *p)
{
int sum = 0;
for(int i = 0; i < 4; i++)
sum = sum * 10 + p[i];
return sum;
}
int change(int a,int i)
{
int dd[4];
formArray(a,dd);
switch(i)
{
case 1: a = (dd[1] * 1000) + (dd[0] * 100) + (dd[2] * 10) + dd[3];break;//千位 and 百位交换
case 2: a = (dd[0] * 1000) + (dd[2] * 100) + (dd[1] * 10) + dd[3];break;//百位 and 十位交换
case 3: a = (dd[0] * 1000) + (dd[1] * 100) + (dd[3] * 10) + dd[2];break;//十位 and 个位交换
}
return a;
}
int add(int a,int i)
{
int dd[4];
formArray(a,dd);
dd[i]++;
if(dd[i] > 9) dd[i] = 1;
a = formDigit(dd);
return a;
}
int sub(int a,int i)
{
int dd[4];
formArray(a,dd);
dd[i]--;
if(dd[i] == 0) dd[i] = 9;
a = formDigit(dd);
return a;
}
void BFS(int s,int e)
{
Node a,b;
int i;
queue<Node> Q;
while(!Q.empty())
Q.pop();//清空队列
a.data = s;
a.tt = 0;
//hash[a.data] = true;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.data == e)//转换到e
{cout<<a.tt<<endl;return;}
if(hash[a.data])continue;
hash[a.data] = true;
for(i = 1; i <= 4; i++)
{
if(i != 4)
{
b.data = change(a.data,i);//i,i - 1易位
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return;}
if(!hash[b.data])Q.push(b);
}
b.data = add(a.data,i - 1);
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return ;}
if(!hash[b.data])
Q.push(b);
b.data = sub(a.data,i - 1);
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return ;}
if(!hash[b.data])
Q.push(b);
}
}
}
int main()
{
int T,s,e;
while(cin>>T)
while(T--)
{
cin>>s>>e;
init();
BFS(s,e);
//cout<<result<<endl;
}
return 0;
}
#include <queue>
#define N 10002
using namespace std;
typedef struct
{
int data;
int tt;//标示尽tt此变换可至此data
}Node;
bool hash[N];
int result;
void init()
{
for(int i = 0; i < N; i++)
hash[i] = false;
}
void formArray(int a,int *p)
{
int i;
for(i = 3; i >= 0; i--)
{
p[i] = a % 10;
a /= 10;
}
}
int formDigit(int *p)
{
int sum = 0;
for(int i = 0; i < 4; i++)
sum = sum * 10 + p[i];
return sum;
}
int change(int a,int i)
{
int dd[4];
formArray(a,dd);
switch(i)
{
case 1: a = (dd[1] * 1000) + (dd[0] * 100) + (dd[2] * 10) + dd[3];break;//千位 and 百位交换
case 2: a = (dd[0] * 1000) + (dd[2] * 100) + (dd[1] * 10) + dd[3];break;//百位 and 十位交换
case 3: a = (dd[0] * 1000) + (dd[1] * 100) + (dd[3] * 10) + dd[2];break;//十位 and 个位交换
}
return a;
}
int add(int a,int i)
{
int dd[4];
formArray(a,dd);
dd[i]++;
if(dd[i] > 9) dd[i] = 1;
a = formDigit(dd);
return a;
}
int sub(int a,int i)
{
int dd[4];
formArray(a,dd);
dd[i]--;
if(dd[i] == 0) dd[i] = 9;
a = formDigit(dd);
return a;
}
void BFS(int s,int e)
{
Node a,b;
int i;
queue<Node> Q;
while(!Q.empty())
Q.pop();//清空队列
a.data = s;
a.tt = 0;
//hash[a.data] = true;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.data == e)//转换到e
{cout<<a.tt<<endl;return;}
if(hash[a.data])continue;
hash[a.data] = true;
for(i = 1; i <= 4; i++)
{
if(i != 4)
{
b.data = change(a.data,i);//i,i - 1易位
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return;}
if(!hash[b.data])Q.push(b);
}
b.data = add(a.data,i - 1);
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return ;}
if(!hash[b.data])
Q.push(b);
b.data = sub(a.data,i - 1);
b.tt = a.tt + 1;
if(b.data == e)
{cout<<b.tt<<endl;return ;}
if(!hash[b.data])
Q.push(b);
}
}
}
int main()
{
int T,s,e;
while(cin>>T)
while(T--)
{
cin>>s>>e;
init();
BFS(s,e);
//cout<<result<<endl;
}
return 0;
}