宽度优先搜索 之 CODE[VS] 1026 逃跑的拉尔夫


/*
读懂题意,bfs即可AC。
不过注意“超出空间 Memory Limit Exceeded”,需要记录节点的状态,判重。
bool isUsed[i][j][k] := 是否之前已在第k步走到(i, j)的节点,是则true,否则false // 用于判重
*/

  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cstdio>
  4 #include <cstddef>
  5 #include <iterator>
  6 #include <algorithm>
  7 #include <string>
  8 #include <locale>
  9 #include <cmath>
 10 #include <vector>
 11 #include <cstring>
 12 #include <map>
 13 #include <queue>
 14 #include <stack>
 15 #include <set>
 16 using namespace std;
 17 const int INF = -0x3f3f3f3f;
 18 const int MaxN = 55;
 19 const int modPrime = 3046721; 
 20 
 21 int step[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
 22                 //  北       南      西       东
 23 int R, C;
 24 int N;
 25 struct Node
 26 {
 27     int x;
 28     int y;
 29     int stepCnt;
 30 };
 31 
 32 Node startOff;
 33 char imap[MaxN][MaxN];
 34 string direction[1010];
 35 bool isUsed[MaxN][MaxN][1010];
 36 
 37 int getDirection(string str)
 38 {
 39     if (str == "NORTH") return 0;
 40     if (str == "SOUTH") return 1;
 41     if (str == "WEST") return 2;
 42     if (str == "EAST") return 3;
 43     return -1;
 44 }
 45 
 46 void outPut()
 47 {
 48     for (int i = 0; i < R; ++i)
 49     {
 50         for (int j = 0; j < C; ++j)
 51         {
 52             cout << imap[i][j];
 53         }
 54         cout << endl;
 55     }
 56 }
 57 
 58 void Solve()
 59 {
 60     queue<Node> queNode;
 61     queNode.push(startOff);
 62     while (!queNode.empty())
 63     {
 64         Node node = queNode.front();
 65         queNode.pop();
 66         if (node.stepCnt == (N - 1))
 67         {
 68             imap[node.x][node.y] = '*';
 69             continue;
 70         }
 71         int num = getDirection(direction[node.stepCnt+1]);
 72         int x = node.x + step[num][0];
 73         int y = node.y + step[num][1];
 74         while ((x >= 0) && (x < R) && (y >= 0) && (y < C) && (imap[x][y] != 'X'))
 75         {
 76             Node nodeTmp;
 77             nodeTmp.x = x;
 78             nodeTmp.y = y;
 79             nodeTmp.stepCnt = node.stepCnt + 1;
 80             if (!isUsed[nodeTmp.x][nodeTmp.y][nodeTmp.stepCnt])
 81             {
 82                 queNode.push(nodeTmp);
 83                 isUsed[nodeTmp.x][nodeTmp.y][nodeTmp.stepCnt] = true;
 84             }
 85             x = x + step[num][0];
 86             y = y + step[num][1];
 87         }
 88     }
 89 
 90     outPut();
 91 }
 92 
 93 
 94 int main() 
 95 {
 96 #ifdef HOME
 97     freopen("in", "r", stdin);
 98     //freopen("out", "w", stdout);
 99 #endif
100 
101     memset(isUsed, false, sizeof(isUsed));
102     cin >> R >> C;
103     bool getGoal = false;
104     for (int i = 0; i < R; ++i)
105     {
106         cin >> imap[i];
107         if (!getGoal)
108         {
109             for (int j = 0; j < C; ++j)
110             {
111                 if (imap[i][j] == '*')
112                 {
113                     imap[i][j] = '.';
114                     startOff.x = i;
115                     startOff.y = j;
116                     startOff.stepCnt = -1;
117                     getGoal = true;
118                     break;
119                 }
120             }
121         }
122     }
123     cin >> N;
124     for (int i = 0; i < N; ++i)
125     {
126         cin >> direction[i];
127     }
128     Solve();
129 
130 
131 #ifdef HOME
132     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
133     _CrtDumpMemoryLeaks();
134 #endif
135     return 0;
136 }

 

 
posted @ 2015-12-01 00:16  JmingS  阅读(309)  评论(0编辑  收藏  举报