Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6319126
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: C/C++

2015-01-04 13:19:55

     
    思路:4个字母的单词有26 ^ 4种组合,可以看成是26 ^ 4种状态,用BFS搜索最短步骤
    

点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <list>
  4. #include <string.h>
  5. #include <queue>

  6. using namespace std;

  7. #define WORDS (26*26*26*26)


  8. string forbid[] = { "aaaz", "aaza", "azaa", "zaaa", "azzz", "zazz", "zzaz", "zzza" };
  9. int arraysize = 8;


  10. typedef struct
  11. {
  12.     int num;
  13.     string data;
  14. }wordsNode;

  15. bool visited[WORDS];

  16. char nextChar(char ch)
  17. {
  18.     if (ch == 'z')
  19.     {
  20.         return 'a';
  21.     }
  22.     else
  23.     {
  24.         return ++ch;
  25.     }
  26. }

  27. char perChar(char ch)
  28. {
  29.     if (ch == 'a')
  30.     {
  31.         return 'z';
  32.     }
  33.     else
  34.     {
  35.         return --ch;
  36.     }
  37. }

  38. int getWordIndex(string word)
  39. {
  40.     return ((word[0] - 'a') * (26 * 26 * 26) + (word[1] - 'a') * (26 * 26) +
  41.         (word[2] - 'a') * 26 + (word[3] - 'a'));
  42. }

  43. bool isforbid(string data,string *forbid , int num)
  44. {
  45.     for (int i = 0; i < num; i++)
  46.     {
  47.         if (data == forbid[i])
  48.         {
  49.             return true;
  50.         }
  51.     }
  52.     return false;
  53. }

  54. int minPresses(string start, string end, string forbid[])
  55. {
  56.     int totalPressNumber = 0;
  57.     queue<wordsNode> Q;
  58.     wordsNode Node;
  59.     Node.num = 0;
  60.     Node.data = start;
  61.     Q.push(Node);
  62.     while (1)
  63.     {
  64.         if (Q.empty())
  65.         {
  66.             printf("not found any words\n");
  67.             return -1;
  68.         }
  69.         wordsNode tmpnode = Q.front();
  70.         if (tmpnode.data == end)
  71.         {
  72.             printf("find words\n");
  73.             return tmpnode.num;
  74.         }
  75.         Q.pop();
  76.         tmpnode.num++;
  77.         for (int i = 0; i < 4; i++)
  78.         {
  79.             wordsNode nebNode = tmpnode;
  80.             nebNode.data[i] = nextChar(tmpnode.data[i]);
  81.             if (visited[getWordIndex(nebNode.data)] == false && !isforbid(nebNode.data,forbid,arraysize))
  82.             {
  83.                 visited[getWordIndex(nebNode.data)] = true;
  84.                 Q.push(nebNode);
  85.             }

  86.             wordsNode nebNode2 = tmpnode;
  87.             nebNode.data[i] = perChar(tmpnode.data[i]);
  88.             if (visited[getWordIndex(nebNode.data)] == false && !isforbid(nebNode.data, forbid, arraysize))
  89.             {
  90.                 visited[getWordIndex(nebNode.data)] = true;
  91.                 Q.push(nebNode);
  92.             }
  93.         }
  94.         
  95.     }
  96.     
  97. }


  98. int _tmain(int argc, _TCHAR* argv[])
  99. {
  100.     string start = "aaaa";
  101.     string end = "zzzz";
  102.     
  103.     for (int i = 0; i < WORDS; i++) {
  104.         visited[i] = false;
  105.     }

  106.     int value = minPresses(start, end, forbid);
  107.     printf("Press Value is %d\n",value);
  108.     getchar();
  109.     return 0;
  110. }

阅读(811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~