迷宫寻宝(自编简单版)
生活随笔
收集整理的這篇文章主要介紹了
迷宫寻宝(自编简单版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
nyoj - 迷宮尋寶(1) 我覺得有點難...我將其改簡單一些。
題目描述:與原題基本差不多,但開門條件改一下,每個門只需要一把鑰匙即可打開,一把鑰匙可以開所有對應的門。
這樣就簡單很多啦。。。其實只是不想之前寫的那個錯代碼沒用處QwQ...
思路:路過鑰匙時就將門的狀態置為可打開,這樣遇到門就可以正常處理了,如果門離起點比鑰匙近,比如:
S . A G . . . X . . . . . . . a
就先將門的位置記錄到隊列中,每次遇到鑰匙就將該隊列中的門全部拿出來測試。
代碼:
#include <bits/stdc++.h>
using namespace std;
struct ss
{
int i, j;
ss() {}
ss(int x, int y) : i(x), j(y) {}
void startpoint(int x, int y) { i = x, j = y; }
};
#define Index(c) ((c) - 'a')
int key[5], dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
bool bfs(vector<vector<char> > & maze, ss ps)
{
memset(key, 0, sizeof(key));
queue<ss> q, door;
q.push(ps);
while(!q.empty())
{
ps = q.front();
q.pop();
maze[ps.i][ps.j] = 'X';
for (int i = 0; i < 4; i++)
{
ss n(ps.i + dir[i][0], ps.j + dir[i][1]);
char c = 0;
if (n.i > -1 && n.i < maze.size() && n.j > -1 && n.j < maze[0].size())
{
if (maze[n.i][n.j] == 'G')
return true;
c = maze[n.i][n.j];
if (c != 'X')
{
if (c == '.')
q.push(n);
else if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e')
{
key[Index(c)] = 1;
while(!door.empty())
{
q.push(door.front());
door.pop();
}
q.push(n);
}
else if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E')
{
if (key[Index(c + 32)])
q.push(n);
else
door.push(n);
}
}
}
}
}
return false;
}
int main()
{
int i, j;
while(1)
{
ss ps;
cin >> i >> j;
if (i == 0 && j == 0)
break;
vector<vector<char> > maze(i, vector<char>(j));
for (int i = 0; i < maze.size(); i++)
for( int j = 0; j < maze[0].size(); j++)
{
cin >> maze[i][j];
if (maze[i][j] == 'S')
{
ps.startpoint(i, j);
}
}
if (bfs(maze, ps))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
總結
以上是生活随笔為你收集整理的迷宫寻宝(自编简单版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Solidworks修改零件文件名之后工
- 下一篇: MSF——基本使用和Exploit模块(