zoukankan      html  css  js  c++  java
  • hdu 1195 Open the Lock

    Open the Lock

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


    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
     
    Source
     
     
    PS:思路来自其他大神
    还是功力不足啊,针对一个数,就只有三种情况,+1(4),-1(4),与邻位交换(3),也就是说一个4位数,动一次可以有12种变化
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstdlib>
      5 #include<cstring>
      6 #include<queue>
      7 using namespace std;
      8 struct node{
      9     int num[6];
     10     int step;
     11 };
     12 node first,last;
     13 bool vis[10][10][10][10],flag;
     14 
     15 int bfs()
     16 {
     17     node p,q;
     18     int i;
     19     memset(vis,false,sizeof(vis));
     20     queue<struct node>Q;
     21     first.step=0;
     22     p=first;
     23     vis[p.num[0]][p.num[1]][p.num[2]][p.num[3]]=true;
     24     Q.push(p);
     25     while(!Q.empty())
     26     {
     27         flag=true;
     28         p=Q.front();
     29         Q.pop();
     30         for(i=0;i<4;i++)
     31         {
     32             if(p.num[i]!=last.num[i])
     33             {
     34                 flag=false;
     35                 break;
     36             }
     37         }
     38         if(flag)
     39         {
     40             return p.step;
     41         }
     42         for(i=0;i<4;i++)//加法
     43         {
     44             q=p;
     45             if(p.num[i]==9)
     46             q.num[i]=1;
     47             else
     48             q.num[i]=p.num[i]+1;
     49             q.step=p.step+1;
     50             if(!vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]])
     51             {
     52                 vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]]=true;
     53                 Q.push(q);
     54             }
     55         }
     56         for(i=0;i<4;i++)//减法
     57         {
     58             q=p;
     59             if(p.num[i]==1)
     60             q.num[i]=9;
     61             else
     62             q.num[i]=p.num[i]-1;
     63             q.step=p.step+1;
     64             if(!vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]])
     65             {
     66                 vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]]=true;
     67                 Q.push(q);
     68             }
     69         }
     70         for(i=0;i<3;i++)//邻位交换
     71         {
     72             q=p;
     73             if(q.num[i+1]!=p.num[i])
     74             {
     75                 q.num[i+1]=p.num[i];
     76                 q.num[i]=p.num[i+1];
     77             }
     78             q.step=p.step+1;
     79             if(!vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]])
     80             {
     81                 vis[q.num[0]][q.num[1]][q.num[2]][q.num[3]]=true;
     82                 Q.push(q);
     83             }
     84         }
     85     }
     86     return 0;
     87 }
     88 
     89 
     90 int main()
     91 {
     92     int n;
     93     char s1[6],s2[6];
     94     scanf("%d",&n);
     95     while(n--)
     96     {
     97         int i;
     98         scanf("%s%s",s1,s2);
     99         for(i=0;i<4;i++)
    100         {
    101             first.num[i]=s1[i]-'0';
    102             last.num[i]=s2[i]-'0';
    103         }
    104         int count;
    105         count=bfs();
    106         printf("%d
    ",count);
    107 
    108     }
    109     return 0;
    110 }
    View Code
  • 相关阅读:
    vue框架-基础5-vue-使用第三方ui组件快速开发页面,vuetify
    vue框架-基础4-vue-使用vue-cli工具
    vue框架-基础3-vue-component组件化开发
    vue框架-基础2-vue生命周期
    vue框架-基础1-vue包引入,指令,模板语言
    python apscheduler的使用研究
    Python中Flask框架SQLALCHEMY_ECHO设置
    通过淘宝镜像下载python3.8.3的安装包
    pycharm 报错Connection to Python debugger failed socket closed
    github fork项目,和删除fork项目
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4125425.html
Copyright © 2011-2022 走看看