我被要求找到一个包含数字的11x11网格,以便人们可以读取1,...,100的正方形.这里的读取意味着您可以修复起始位置和方向(8种可能性),如果您可以连续找到数字1,0,0,0,0,4,则可以找到1,2,10,100的正方形我做了一个程序(算法不是我自己的.我稍微修改了一个程序,它使用最佳优先搜索来找到解决方案,但速度太慢.有没有人知道更好的算法来解决问题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int val[21][21];//number which is present on position
int vnum[21][21];//number of times the position is used - useful if you want to backtrack
//5 unit borders
int mx[4]={-1,0,1,0};//movement arrays
int my[4]={0,-1,0,1};
int check(int x,int y,int v,int m)//check if you can place number - if you can, return number of overlaps
{
int c=1;
while(v)//extract digits one by one
{
if(vnum[x][y] && (v%10)!=val[x][y])
return …Run Code Online (Sandbox Code Playgroud) algorithm ×1