#include<iostream>
#include<vector>
using namespace std;
const int N = 20;
int n,k;
bool vis[N][N];
int g[N][N];
bool f = false;
int dx[] = {-1,-1,0,1,1,1,0,-1},dy[] = {0,1,1,1,0,-1,-1,-1};
vector<int> res;
bool check()
{
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
if(vis[i][j] == false)
return false;
}
}
return true;
}
void dfs(int x,int y,int r)
{
if(f) return ;
if( x <0 || x>=n || y<0 || y>=n || g[x][y] != r)
{
return ;
}
if(x == n-1 && y == n-1 && g[x][y] == r)
{
vis[x][y] = true;
if(check())
{
f = true;
for(int i = 0;i<res.size();i++)
{
cout<<res[i];
}
}
else
{
vis[x][y] = false;
return ;
}
}
for(int i = 0;i<=7;i++)
{
if(!vis[x][y])
{
/*vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];*/
if(i == 1)
{
if(x>0 && (!vis[x-1][y] || !vis[x][y+1]))
{
vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];
dfs(xx,yy,(r+1)%k);
vis[x][y] = false;
res.pop_back();
}
}
else if(i == 3)
{
if(!vis[x+1][y] || !vis[x][y+1])
{
vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];
dfs(xx,yy,(r+1)%k);
vis[x][y] = false;
res.pop_back();
}
}
else if(i == 5)
{
if(y>0 && (!vis[x][y-1] || !vis[x+1][y]))
{
vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];
dfs(xx,yy,(r+1)%k);
vis[x][y] = false;
res.pop_back();
}
}
else if(i == 7)
{
if(x>0 && y>0 && (!vis[x-1][y] || !vis[x][y-1]))
{
vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];
dfs(xx,yy,(r+1)%k);
vis[x][y] = false;
res.pop_back();
}
}
else
{
vis[x][y] = true;
res.push_back(i);
int xx = x +dx[i],yy = y + dy[i];
dfs(xx,yy,(r+1)%k);
vis[x][y] = false;
res.pop_back();
}
}
}
}
int main()
{
cin>>n>>k;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
cin>>g[i][j];
}
}
int r = 0;
dfs(0,0,r);
if(!f)
{
cout<<"-1";
}
return 0;
}
R格式
#include<bits/stdc++.h>
using namespace std;
string s;
int n;
vector<char> A;
vector<char> mul(vector<char> A,int n)
{
vector<char> C;
int t = 0;
for(int i = 0;i<A.size();i++)
{
if(A[i] == '.')
{
C.push_back(A[i]);
}
else
{
int x = A[i] - '0';
t += x*n;
C.push_back((t%10) + '0');
t = t/10;
}
}
if(t)
{
while(t)
{
C.push_back((t%10) + '0');
t = t/10;
}
}
return C;
}
int main()
{
cin>>n;
cin>>s;
for(int i = s.size()-1;i>=0;i--)
{
A.push_back(s[i]);
}
vector<char> C;
while(n--)
{
C = mul(A,2);
A = C;
}
string res;
for(int i = C.size()-1;i>=0;i--)
{
if(C[i]!= '.')
res += C[i];
else
{
if(C[i-1] >= '5')
{
int y = C[i+1] -'0';
char c = (y+1) + '0';
res[res.size()-1] = c;
cout<<res;
return 0;
}
else
{
cout<<res;
return 0;
}
}
}
return 0;
}