蓝桥云客 数字接龙
2.数字接龙 - 蓝桥云课
问题描述
小蓝最近迷上了一款名为《数字接龙》的迷宫游戏,游戏在一个大小为 N×N 的格子棋盘上展开,其中每一个格子处都有着一个 0…K−1 之间的整数。游戏规则如下:
- 从左上角 (0,0) 处出发,目标是到达右下角 (N−1,N−1) 处的格子,每一步可以选择沿着水平/垂直/对角线方向移动到下一个格子。
- 对于路径经过的棋盘格子,按照经过的格子顺序,上面的数字组成的序列要满足:0,1,2,…,K−1,0,1,2,…,K−1,0,1,2,…。
- 途中需要对棋盘上的每个格子恰好都经过一次(仅一次)。
- 路径中不可以出现交叉的线路。例如之前有从 (0,0) 移动到 (1,1),那么再从 (1,0) 移动到 (0,1) 线路就会交叉。
为了方便表示,我们对可以行进的所有八个方向进行了数字编号,如下图2所示;因此行进路径可以用一个包含 0…7 之间的数字字符串表示,如下图1是一个迷宫示例,它所对应的答案就是:41255214。
输入格式
第一行包含两个整数 N, K。
接下来输入 N 行,每行 N 个整数表示棋盘格子上的数字。
输出格式
输出一行表示答案。如果存在答案输出路径,否则输出 -1。
样例输入
3 3
0 2 0
1 1 1
2 0 2
样例输出
41255214
样例说明
行进路径如图 1 所示。
评测用例规模与约定
对于 80% 的评测用例: 1 ≤ N ≤ 5。
对于 100% 的评测用例: 1 ≤ N ≤ 10, 1 ≤ K ≤ 10。
思路:
75%无判断交叉的代码
代码:
#include <bits/stdc++.h>
using namespace std;
// 0 1 2 3 4 5 6 7
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n,k;
int a[15][15];
bool found;
bool vis[15][15];
vector <int> num;
unordered_map <int,int> mp;
bool check(int x,int y,int kx,int ky)
{
if(vis[x+kx][y] && vis[x][y+ky])//如果这两个点为真,那么就是斜线
return false;
else
return false;
}
void dfs(int x,int y,int step,int cnt)
{
if(found)
return;
if(x == n && y == n)
{
if(step == n*n)
{
found = true;
for(int j = 0 ; j < num.size() ; j++)
cout << num[j];
return;
}
}
for(int i = 0 ; i < 8 ; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
/* if(mp[dx[i]*8+dy[i]]== 1 || mp[dx[i]*8+dy[i]]== 3 || mp[dx[i]*8+dy[i]]== 5 || mp[dx[i]*8+dy[i]]== 7) //判断斜线
{
if(check(tx,ty,dx[i],dy[i]))
continue;
}*/
if(!vis[tx][ty] && tx >= 1 && tx <= n && ty >=1 && ty <= n && a[tx][ty] == cnt)
{
vis[tx][ty] = true;
num.push_back(mp[dx[i]*8+dy[i]]);
dfs(tx,ty,step+1,(cnt+1)%k);
vis[tx][ty] = false;
num.pop_back();
}
}
}
int main()
{
cin >> n >> k;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cin >> a[i][j];
}
}
for(int i = 0 ; i < 8 ; i++)
{
// cout <<"dx[i]*dy[i] :" << dx[i]*8+dy[i] <<"----------------mp[dx[i]*8+dy[i]]] :" << mp[dx[i]*8+dy[i]] << endl;
mp[dx[i]*8+dy[i]] = i;
}
vis[1][1] = true;
dfs(1,1,1,1);
if(found)
{
}
else
cout << -1;
return 0;
}
思路:
利用图论知识,将边连接起来,然后就可以判断是否有交叉,点的话,将二维转化为一维作为点的下标。记得数组要开大点。
代码:
#include <bits/stdc++.h>
using namespace std;
// 0 1 2 3 4 5 6 7
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n,k;
int a[15][15];
bool found;
bool vis[15][15];
bool attached[500][500];
vector <int> num;
unordered_map <int,int> mp;
bool check(int x,int y,int kx, int ky)
{
if(attached[(x+kx)*10+y][x*10+(y+ky)])//如果这两个点为真,那么就是交叉
return true;
else
return false;
}
void dfs(int x,int y,int step,int cnt)
{
if(found)
return;
if(x == n && y == n)
{
if(step == n*n)
{
found = true;
for(int j = 0 ; j < num.size() ; j++)
cout << num[j];
return;
}
}
for(int i = 0 ; i < 8 ; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if(!vis[tx][ty] && tx >= 1 && tx <= n && ty >=1 && ty <= n && a[tx][ty] == cnt)
{
if(mp[dx[i]*8+dy[i]]== 1 || mp[dx[i]*8+dy[i]]== 3 || mp[dx[i]*8+dy[i]]== 5 || mp[dx[i]*8+dy[i]]== 7) //判断会出现斜线的情况
{
if(check(x,y,dx[i],dy[i]))
continue;
}
attached[x*10+y][tx*10+ty] = true;//二维转化为一维,对应是否建立连接
attached[tx*10+ty][x*10+y] = true;//二维转化为一维,对应是否建立连接
vis[tx][ty] = true;
num.push_back(mp[dx[i]*8+dy[i]]);
dfs(tx,ty,step+1,(cnt+1)%k);
attached[x*10+y][tx*10+ty] = false;//二维转化为一维,对应是否建立连接
attached[tx*10+ty][x*10+y] = false;//二维转化为一维,对应是否建立连接
vis[tx][ty] = false;
num.pop_back();
}
}
}
int main()
{
cin >> n >> k;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= n ; j++)
{
cin >> a[i][j];
}
}
for(int i = 0 ; i < 8 ; i++)//作为一种映射方向
{
// cout <<"dx[i]*dy[i] :" << dx[i]*8+dy[i] <<"----------------mp[dx[i]*8+dy[i]]] :" << mp[dx[i]*8+dy[i]] << endl;
mp[dx[i]*8+dy[i]] = i;
}
vis[1][1] = true;
dfs(1,1,1,1);
if(found)
{
}
else
cout << -1;
return 0;
}