解题思路:搜索每一天能到达右下角点的路径,经过每一个位置时,给它的x、y都加上1的贡献,最后到达右下角时,如果x、y的值等于输入的箭靶的数目则为正确答案
#include<bits/stdc++.h>
#define x first
#define y second
#define mem(h) memset(h,-1,sizeof h)
#define mcp(a,b) memcpy(a,b,sizeof b)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
namespace IO{
inline LL read(){
LL o=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){o=o*10+c-'0';c=getchar();}
return o*f;
}
}using namespace IO;
const int N=1e2+7,M=2e5+7,INF=0x3f3f3f3f,mod=1e8+7,P=131;
int g[N][N];
int rx[N],ry[N],nx[N],ny[N];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
bool st[N][N],flag;
vector<int>path;
int n;
bool check(){
for(int i=0;i<n;i++){
if(rx[i]!=nx[i]||ry[i]!=ny[i])return false;
}
return true;
}
void dfs(int x,int y){
if(flag)return ;
if(x==n-1&&y==n-1&&check()){
flag=1;
for(int i=0;i<path.size();i++){
cout<<path[i]<<" ";
}
cout<<endl;
return ;
}
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(st[tx][ty])continue;
if(tx<0||tx>=n||ty<0||ty>=n)continue;
st[tx][ty]=1;
nx[tx]++,ny[ty]++;
path.push_back(g[tx][ty]);
dfs(tx,ty);
st[tx][ty]=0;
nx[tx]--,ny[ty]--;
path.pop_back();
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++)cin>>ry[i];
for(int i=0;i<n;i++)cin>>rx[i];
for(int x=0,i=0;i<n;i++){
for(int j=0;j<n;j++,x++){
g[i][j]=x;
}
}
path.push_back(0);
st[0][0]=1;
nx[0]++;
ny[0]++;
dfs(0,0);
return 0;
}