您的位置:首页 > 运维架构

hdu 1195 Open the Lock(bfs)

2015-10-02 16:49 459 查看

Open the Lock

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5355 Accepted Submission(s): 2386


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, print the minimal steps in one line.

[align=left]Sample Input[/align]

2
1234
2144

1111
9999

[align=left]Sample Output[/align]

2
4

[align=left]Author[/align]
YE, Kai

[align=left]Source[/align]
Zhejiang University Local Contest 2005

比较水...直接bfs就好.
妈蛋一直交错题目QAQ

/*************************************************************************
> File Name: code/hdu/1195.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年10月02日 星期五 14时42分21秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>
#define yn hez111qqz
#define j1 cute111qqz
using namespace std;
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
int x,y;
bool vis[10005];
struct Q
{
int g,s,b,q;
int d;

}s,tar;

int add( int x)
{
int res = 0 ;
res = (x+1);
if (res == 10) res = 1;
return res;
}
int minu( int x)
{
int res = 0 ;
res = (x-1);
if (res==0) res =  9;
return res;
}

int get(Q a)
{
int res = 0 ;
res = a.q*1000+a.b*100+a.s*10+a.g;
return res;
}

void bfs()
{
queue<Q>q;
q.push(s);

while (!q.empty())
{

Q pre = q.front();q.pop();
//    cout<<get(pre)<<" "<<pre.d<<endl;
if (get(pre)==y)
{
printf("%d\n",pre.d);
//   cout<<"aaahhh"<<endl;
return;
}
Q nxt = pre;
nxt.d = pre.d + 1;

nxt.g = add(pre.g);
if (!vis[get(nxt)])
{
vis[get(nxt)] = true;
// nxt.d = pre.d  +1;
q.push(nxt);
}

nxt.g = minu(pre.g);
if (!vis[get(nxt)])
{
// nxt.d = pre.d  +1;
vis[get(nxt)] =true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.s = add(pre.s);
if (!vis[get(nxt)])
{
// nxt.d = pre.d  +1;
vis[get(nxt)]=true;
q.push(nxt);
}

nxt.s = minu(pre.s);
if (!vis[get(nxt)])
{
//    nxt.d = pre.d  +1;
vis[get(nxt)] =true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.b =add(pre.b);
if (!vis[get(nxt)])
{
//  nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}

nxt.b =minu(pre.b);
if (!vis[get(nxt)])
{
//nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.q =add(pre.q);
if (!vis[get(nxt)])
{
//    nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}

nxt.q = minu(pre.q);
if (!vis[get(nxt)])
{
//  nxt.d = pre.d  +1;
vis[get(nxt)]= true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.g = pre.s;
nxt.s = pre.g;
if (!vis[get(nxt)])
{
//    nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.s = pre.b;
nxt.b = pre.s;
if (!vis[get(nxt)])
{
//  nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}

nxt = pre;
nxt.d = pre.d+1;
nxt.b = pre.q;
nxt.q = pre.b;
if (!vis[get(nxt)])
{
// nxt.d = pre.d  +1;
vis[get(nxt)] = true;
q.push(nxt);
}
}
}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T;
scanf("%d",&T);
while (T--)
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&x,&y);
s.g = x %10;
s.s = x%100/10;
s.b = x/100%10;
s.q = x/1000;
s.d = 0 ;
vis[x] = true;
bfs();

}

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: