HDU-1195,Open the Lock(BFS)

简介: HDU-1195,Open the Lock(BFS)

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


程序代码:  


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
  int num[4],step;
}first,last;
int vis[11][11][11][11];
void bfs()
{
  int i;
  node a,next;
  queue<node> q;
  a=first;
  a.step=0;
  q.push(a);
  vis[a.num[0]][a.num[1]][a.num[2]][a.num[3]]=1;
  while(!q.empty())
  {
    a=q.front();
    q.pop();
    if(a.num[0]==last.num[0]&&a.num[1]==last.num[1]
    &&a.num[2]==last.num[2]&&a.num[3]==last.num[3])
    {
      printf("%d\n",a.step);
      return ;
    }
    for(i=0;i<4;i++)
    {
      next=a;
      next.num[i]++;
      if(next.num[i]==10)
        next.num[i]=1;
      if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
      {
        vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
        next.step++;
        q.push(next);
      }
    }
    for(i=0;i<4;i++)
    {
      next=a;
      next.num[i]--;
      if(next.num[i]==0)
        next.num[i]=9;
      if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
      {
        vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
        next.step++;
        q.push(next);
      }
    }
    for(i=0;i<3;i++)
    {
      next=a;
      next.num[i]=a.num[i+1];
      next.num[i+1]=a.num[i];
      if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]])
      {
        vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
        next.step++;
        q.push(next);
      }
    }
  }
}
int main()
{
  int i,t;
  char s1[10],s2[10];
  scanf("%d",&t);
  while(t--)
  {
    scanf("%s%s",s1,s2);
    for(i=0;i<4;i++)
    {
      first.num[i]=s1[i]-'0';
      last.num[i]=s2[i]-'0';
    }
    memset(vis,0,sizeof(vis));
    bfs();
  }
  return 0;
}


相关文章
|
3月前
walk_tg_tree_from的图解
walk_tg_tree_from的图解
|
7月前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
46 0
|
Java
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
63 0
HDU7050.Link with Limit(有向图tarjan找环)
HDU7050.Link with Limit(有向图tarjan找环)
150 0
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
HDU-1016,Prime Ring Problem(DFS+素数)
HDU-1016,Prime Ring Problem(DFS+素数)