zoukankan      html  css  js  c++  java
  • hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock

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


    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
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1072 1242 1240 1044 1254 
     
      暴力BFS广搜
      利用广搜穷举所有情况。注意每一位可+1可-1,一共有4位,这就是8种情况,另外相邻两位可以交换位置,这又是3种情况。所以每一个节点一共要处理11种情况。用一个isv[][][][]四维数组记录可能出现的数字串出现过没有。例如,"2143"出现过了,则 isv[2][1][4][3]=true。
      代码
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 using namespace std;
     6 struct Node{
     7     char a[4];
     8     int step;
     9 };
    10 char a[5],b[5];
    11 bool isv[10][10][10][10];
    12 int bfs()
    13 {
    14     memset(isv,0,sizeof(isv));
    15     queue <Node> q;
    16     Node cur,next;
    17     int i;
    18     strcpy(cur.a,a);
    19     cur.step = 0;
    20     isv[cur.a[0]-'0'][cur.a[1]-'0'][cur.a[2]-'0'][cur.a[3]-'0'] = true;
    21     q.push(cur);
    22     while(!q.empty()){
    23         cur = q.front();
    24         q.pop();
    25         //printf("%d	%s
    ",cur.step,cur.a);
    26         for(i=0;i<4;i++)
    27             if(cur.a[i] != b[i])
    28                 break;
    29         if(i>=4)    //找到
    30             return cur.step;
    31 
    32         //前八种,4个位置的数字+1或者-1
    33         for(i=0;i<8;i++){
    34             int nc = cur.a[i/2]-'0';
    35             if(i%2)    //+1
    36                 nc = nc%9+1;
    37             else //-1
    38                 nc = nc==1?9:nc-1;
    39             next = cur;
    40             next.a[i/2] = nc + '0';
    41             if(isv[next.a[0]-'0'][next.a[1]-'0'][next.a[2]-'0'][next.a[3]-'0'])
    42                 continue;
    43             //可以放
    44             isv[next.a[0]-'0'][next.a[1]-'0'][next.a[2]-'0'][next.a[3]-'0'] = true;
    45             next.step = cur.step + 1;
    46             q.push(next);
    47         }
    48         //后三种,相邻的数字交换位置
    49         for(i=0;i<3;i++){
    50             next = cur;
    51             char t = next.a[i];
    52             next.a[i] = next.a[i+1];
    53             next.a[i+1] = t;
    54             if(isv[next.a[0]-'0'][next.a[1]-'0'][next.a[2]-'0'][next.a[3]-'0'])
    55                 continue;
    56             isv[next.a[0]-'0'][next.a[1]-'0'][next.a[2]-'0'][next.a[3]-'0'] = true;
    57             next.step = cur.step + 1;
    58             q.push(next);
    59         }
    60     }
    61     return -1;
    62 }
    63 int main()
    64 {
    65     int i,T;
    66     scanf("%d",&T);
    67     for(i=1;i<=T;i++){
    68         scanf("%s",a);
    69         scanf("%s",b);
    70         if(i!=1)    //读取空行
    71             scanf("%[^
    ]%*");
    72         int step = bfs();
    73         printf("%d
    ",step);
    74     }
    75     return 0;
    76 }

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    有关token
    JQuery自定义resize事件代码解析
    vue--为网页添加动态响应背景
    如何查询数据库中所有表格,或者查询是否存在某个表格-mysql和oracle
    Foxmail: 错误信息::ssl连接错误, errorCode: 5,各种解决方案的大杂烩。
    [转] Hadoop入门系列(一)Window环境下搭建hadoop和hdfs的基本操作
    java连数据库和数据库连接池踩坑日记(二)-------数据库连接池c3p0
    java实现将汉字转为拼音
    Sublime 如何修改默认编码格式
    JSP 表单处理
  • 原文地址:https://www.cnblogs.com/yym2013/p/3699557.html
Copyright © 2011-2022 走看看