我试图在2D网格上实现Lee算法。然而,洪水循环太早停止,声称没有找到任何更多的“空”细胞。我完全不知道为什么。
class LocalNotificationViewController: UIViewController {
var timer = Timer()
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(self.setLocalNotification)), userInfo: nil, repeats: true)
}
@objc func setLocalNotification() {
// timeEntered = time.text!
// let hrMin = timeEntered.components(separatedBy: ":");
let content = UNMutableNotificationContent()
content.title = "Test Local Notification"
content.subtitle = "Testing..."
content.body = "Testing..."
content.badge = 1
content.sound = UNNotificationSound(named: "alarm.aiff");
trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false);
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
var timeEntered: String = "";
var trigger: UNTimeIntervalNotificationTrigger? = nil;
@IBOutlet var time: UITextField!;
@IBAction func setNotification(_ sender: Any) {
runTimer()
}
@IBAction func stopRepeat(_ sender: Any) {
timer.invalidate()
}
通过将单元格指定给-3来指定路径上的结尾。被阻止的细胞是-2。空的单元格是-1。起始单元格为0。
答案 0 :(得分:0)
不确定您的问题是什么,但您的算法似乎很稳定。我已经清理了一下它似乎工作正常:
#include <iostream>
#include <string>
#include <iomanip>
#define WIDTH 7
#define HEIGHT 7
int gridArray[WIDTH][HEIGHT] =
{
{ -1,-1,-1,0,-2,-1,-2 },
{ -2,-1,-1,-1,-1,-2,-2 },
{ -2,-1,-1,-1,-1,-2,-2 },
{ -2,-2,-1,-1,-1,-2,-1 },
{ -1,-2,-2,-2,-1,-1,-2 },
{ -1,-2,-1,-1,-1,-2,-2 },
{ -2,-2,-3,-1,-1,-2,-2 }
};
void DrawGrid()
{
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
std::string message;
if (gridArray[x][y] == 0)
message = "S";
else if (gridArray[x][y] == -3)
message = "E";
else if (gridArray[x][y] == -2)
message = "#";
else if (gridArray[x][y] == -1)
message = ".";
else
message = std::to_string(gridArray[x][y]);
std::cout << std::setw(3) << message << " ";
}
std::cout << std::endl << std::endl;
}
std::cout << std::endl << std::endl;
}
void SolveMaze()
{
bool foundEnd = false;
int it = 0;
while (!foundEnd)
{
bool foundEmpty = false;
for (int x = 0; x < WIDTH && !foundEnd; ++x)
{
for (int y = 0; y < HEIGHT; ++y)
{
if (gridArray[x][y] == it)
{
// check east cell
if (x < WIDTH - 1)
{
int &east = gridArray[x + 1][y];
if (east == -3)
{
foundEnd = true;
break;
}
else if (east == -1)
{
east = it + 1;
foundEmpty = true;
}
}
// check west cell
if (x > 0)
{
int &west = gridArray[x - 1][y];
if (west == -3)
{
foundEnd = true;
break;
}
else if (west == -1)
{
west = it + 1;
foundEmpty = true;
}
}
// check south cell
if (y < HEIGHT - 1)
{
int &south = gridArray[x][y + 1];
if (south == -3)
{
foundEnd = true;
break;
}
else if (south == -1)
{
south = it + 1;
foundEmpty = true;
}
}
// check north cell
if (y > 0)
{
int &north = gridArray[x][y - 1];
if (north == -3)
{
foundEnd = true;
break;
}
else if (north == -1)
{
north = it + 1;
foundEmpty = true;
}
}
}
}
}
if (!foundEnd && !foundEmpty)
{
std::cout << "This maze has no solution!" << std::endl << std::endl;
break;
}
it++;
}
}
int main()
{
DrawGrid();
SolveMaze();
DrawGrid();
system("pause");
return 0;
}