hdu1195 Open the Lock

一道bfs的题目,不敢说水..好吧,这题的确是入门题....

因为自己做了快2小时..

还是因为思维考虑的不全面的缘故;

一开始把加减的情况都写在了一个循环里...交了一遍,WA..

检查了挺久才检查出来的....把过程打印出来也有不靠谱的地方...

后来分析了一下逻辑...发现之前的修改会影响到后面对数字的修改;其实就是我做了2次修改,才记录了一次;

而且这种做法还会使压入队列的情况减少...

后来修改的时候吧加法改少了一部分...又花了一点时间debug..

不过怎么说..最后还是做出来了...

  1 #include <iostream>
  2 #include <queue>
  3 #include <string.h>
  4 #include <algorithm>
  5 using namespace std;
  6 #define maxn 10005
  7 struct node{
  8     int count;
  9     int a[4];
 10 };
 11 bool visit[maxn];
 12 int N;
 13 int begin_value,end_value;
 14 int getValue(int *a)
 15 {
 16     int result = 0;
 17     for (int i = 0; i < 4; i++){
 18         result = result * 10 + a[i];
 19     }
 20     return result;
 21 }
 22 int bfs()
 23 {
 24     queue<node> game;
 25     node cur, next;
 26     cur.count = 0;
 27     cur.a[0] = begin_value / 1000;
 28     cur.a[1] = (begin_value % 1000) / 100;
 29     cur.a[2] = (begin_value % 100) / 10;
 30     cur.a[3] = begin_value % 10;
 31     game.push(cur);
 32     visit[getValue(cur.a)] = true;
 33     while (!game.empty()){
 34         cur = game.front();
 35         game.pop();
 36         int value = getValue(cur.a);
 37         //printf("%d %d
", value,cur.count);
 38         if (value == end_value){
 39             return cur.count;
 40         }
 41         //减法:
 42         for (int i = 0; i < 4; i++){
 43             next = cur;
 44             if (cur.a[i] == 1){
 45                 next.a[i] = 9;
 46                 if (!visit[getValue(next.a)])
 47                 {
 48                     next.count = cur.count + 1;
 49                     visit[getValue(next.a)] = true;
 50                     game.push(next);
 51                 }
 52             }
 53             else{
 54                 next.a[i] = cur.a[i] - 1;
 55                 if (!visit[getValue(next.a)])
 56                 {
 57                     next.count = cur.count + 1;
 58                     visit[getValue(next.a)] = true;
 59                     game.push(next);
 60                 }
 61 
 62             }
 63         }
 64         //加法
 65         for (int i = 0; i < 4; i++){
 66             next = cur;
 67             if (cur.a[i] == 9){
 68                 next.a[i] = 1;
 69                 if (!visit[getValue(next.a)])
 70                 {
 71                     next.count = cur.count + 1;
 72                     visit[getValue(next.a)] = true;
 73                     game.push(next);
 74                 }
 75             }
 76             else{
 77                 next.a[i] = cur.a[i] + 1;
 78                 if (!visit[getValue(next.a)])
 79                 {
 80                     next.count = cur.count + 1;
 81                     visit[getValue(next.a)] = true;
 82                     game.push(next);
 83                 }
 84             }
 85         }
 86         for (int i = 0; i < 3; i++)
 87         {
 88             next = cur;
 89             swap(next.a[i], next.a[i + 1]);        
 90             if (!visit[getValue(next.a)])
 91             {
 92                 next.count = cur.count + 1;
 93                 visit[getValue(next.a)] = true;
 94                 game.push(next);
 95                 
 96             }
 97         }
 98     }
 99     return 0;
100 }
101 
102 int main()
103 {
104     while (~scanf("%d", &N)){
105         while (N--){
106             scanf("%d %d", &begin_value, &end_value);
107             memset(visit, false, sizeof(visit));
108             printf("%d
", bfs());
109         }
110     }
111     return 0;
112 }
原文地址:https://www.cnblogs.com/xiaoniuniu/p/4483723.html