求助一本通的题
  • 板块学术版
  • 楼主cat101
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/2/10 21:57
  • 上次更新2023/10/24 01:12:42
查看原帖
求助一本通的题
916509
cat101楼主2023/2/10 21:57

题目

我的代码

#include<bits/stdc++.h>
using namespace std;
int t,n,m,x,y,ans,vis[10][10];
int dir[8][2]= {{-2,1},{-2,-1},{-1,2},{-1,-2},{2,1},{2,-1},{1,2},{1,-2}};
int read()
{
    int s=0,w=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            w=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        s=s*10+c-'0';
        c=getchar();
    }
    return s*w;
}
void print(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>=10)
		print(x/10);
	putchar(x%10+'0');
	return;
}
void dfs(int x,int y,int step)
{
	if(step==0)
	{
		ans++;
		return;
	}
	for(int i=0;i<8;i++)
	{
		int nx=x+dir[i][0];
		int ny=y+dir[i][1];
		if(nx>=0&&ny>=0&&nx<n&&ny<m&&vis[nx][ny]==0)
		{
			vis[nx][ny]=1;
			dfs(nx,ny,step-1);
			vis[nx][ny]=0;
		}
	}
}
int main()
{
	t=read();
	while(t--)
	{
		n=read(),m=read(),x=read(),y=read();
		ans=0;
		vis[x][y]=1;
		dfs(x,y,n*m-1);
		printf("%d\n",ans);
	}
	return 0;
}

只拿了30分……

2023/2/10 21:57
加载中...