Skip to content

Latest commit

 

History

History
144 lines (135 loc) · 3.58 KB

hdu1195.MD

File metadata and controls

144 lines (135 loc) · 3.58 KB

日期 2021 / 10 / 28

题目

[题目链接]https://acm.dingbacode.com/showproblem.php?pid=1195

Open the Lock

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

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

解题思路

这个题是从一个初始四位数密码得到一个目标密码所需要的步数,对于密码锁可以进行加减操作已经两位直接换位操作,对于每一个种操作我们都应该放入队列中进行bfs 加减操作利用for循环对每一位进行操作,然后放入队列中,并用vis数组判断,然后找到后返回步骤数

代码演示

#include <bits/stdc++.h>

#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int 
#define uint unsigned int
const int maxn = 1e6 + 10;
using namespace std;

char a[11], b[11];
int ans[5];
int vis[11][11][11][11];


struct node {
  int now[5];
  int step;
};

int bfs() {
  bool flag;
  node x, y;
  queue<node> q;
  for (int i = 0; i < 4; i++) {
    x.now[i] = a[i] - '0';
    ans[i] = b[i] - '0';
  }
  x.step = 0;
  q.push(x);
  format(vis);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    flag = true;
    for (int i = 0; i < 4; i++) {
      if (x.now[i] != ans[i]) {
	flag = false;
	break;
      }
    }
    if (flag) {
      return x.step;
    }
    for (int i = 0; i < 4; i++) {
      y = x;
      if (x.now[i] == 9) {
        y.now[i] = 1;
      } else {
	y.now[i] = x.now[i] + 1;
      }
      if (!vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]]) {
        vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]] = 1;
	y.step = x.step + 1;
	q.push(y);
      }
    }
    for (int i = 0; i < 4; i++) {
      y = x;
      if (x.now[i] == 1) {
        y.now[i] = 9;
      } else {
        y.now[i] = x.now[i] - 1;
      }
      if (!vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]]) {
        vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]] = 1;
	y.step = x.step + 1;
	q.push(y);
      }
    } 
    for (int i = 0; i < 3; i++) {
      y = x;
      y.now[i] = x.now[i + 1];
      y.now[i + 1] = x.now[i];
      if (!vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]]) {
        vis[y.now[0]][y.now[1]][y.now[2]][y.now[3]] = 1;
	y.step = x.step + 1;
	q.push(y);
      }
    }
  }
  return 0;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    cin >> a >> b;
    cout << bfs() << endl;
  }
  return 0;
}