// newcoder_xiaohongshu_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <queue>
#include <map>
using namespace std;
/*
pair<int, int > p = make_pair(1, 2);
pair<int, int> p(1, 2);
p= new pair<int,int>(1,2)
*/
int dx[4] = {0,1,0,-1};
int dy[4]= {1,0,-1,0};
//迷宫地图,1代表墙壁,0代表通路
int N = 10;
int mat[10][10] = { {1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
class Dot {
public:
int x;
int y;
Dot(int x,int y):x(x),y(y){}
};
class WideBlock:public Dot
{
public:
WideBlock *parent;
WideBlock(int x,int y,WideBlock* p):Dot(x,y),parent(p){}
};
void print(WideBlock *p)//递归顺序输出
{
if (!p->parent)
{
cout << "\n(" << p->x << "," << p->y << ")" << ";";
}
else
{
print(p->parent);
cout << "(" << p->x << "," << p->y << ")" << ";";
}
}
int main()
{
WideBlock *s=new WideBlock(1, 1, NULL), *end=new WideBlock(8, 8, NULL);
queue<WideBlock*> q;
mat[1][1] = 1;
q.push(s);
while (!q.empty())
{
WideBlock* cur = q.front();
q.pop();
int x = cur->x;
int y = cur->y;
if (x == end->x && y == end->y)
{
end = cur;
break;
}
if (y + dy[0] < N && mat[x][y + dy[0]] == 0)
{
q.push(new WideBlock(x, y + dy[0],cur));
mat[x][y + dy[0]] = 1;
}
if (x + dx[1] < N && mat[x+dx[1]][y] == 0)
{
q.push(new WideBlock(x+dx[1], y,cur));
mat[x+dx[1]][y] = 1;
}
if (y + dy[2] > -1 && mat[x][y + dy[2]] == 0)
{
q.push(new WideBlock(x, y + dy[2],cur));
mat[x][y + dy[2]] = 1;
}
if (x + dx[3] >-1 && mat[x+dx[3]][y] == 0)
{
q.push(new WideBlock(x+dx[3], y,cur));
mat[x+dx[3]][y] = 1;
}
}
WideBlock *tmp = end;
while (tmp)
{
cout << "(" << tmp->x << "," << tmp->y << ")" << ";";
tmp = tmp->parent;
}
tmp = end;
print(tmp);
std::cout << "Hello World!\n";
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件