跳到正文

剪贴板

剪贴板 52yg50tk

原文
剪贴板信息

剪贴板信息

收录
更新

一

题目链接

C++
#include<bits/stdc++.h>
using namespace std;

int T,n;
int ha,hb,la,lb;
char c[1314][1314];
bool v[1314][1314];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

bool dfs(int x,int y)
{
	if(x == hb and y == lb)
		return 1;
	v[x][y]=1;
	for(int i=0;i<4;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx >= 0 and xx < n and yy >= 0 and yy < n and v[xx][yy] == 0 and c[xx][yy] == '.')
			if(dfs(xx,yy))
				return 1;
	}
	return 0;
}

int main()
{
	cin >> T;
	while(T--)
	{
		memset(v,0,sizeof(v));

		cin >> n;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				cin >> c[i][j];
		cin >> ha >> la >> hb >> lb;

		if(c[ha][la] == '#' or c[hb][lb] == '#')
		{
			cout << "NO" << "\n";
			continue;
		}

		if(!dfs(ha,la))
			cout << "NO" << "\n";
		else
			cout << "YES" << "\n";
	}
	return 0;
}

这是照着视频里编的代码

二

题目链接

C++
#include<bits/stdc++.h>
using namespace std;

int T;
int n,m;
int x,y;
bool v[520][520];
int ans;
int dx[8]={-1,-2,-2,-1,1,2,2,1};
int dy[8]={-2,-1,1,2,2,1,-1,-2};

void dfs(int x,int y,int sum)
{
	if(n*m == sum)
	{
		ans++;
		return ;
	}
	for(int i=0;i<8;i++)
	{
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx >= 0 and xx < n and yy >= 0 and yy <m and v[xx][yy] == 0)
		{
			v[xx][yy]=1;
			dfs(xx,yy,sum+1);
			v[xx][yy]=0;
		}
	}
}

int main()
{
	int T;
	cin >> T;

	while(T--)
	{
		ans=0;

		cin >> n >> m >> x >> y;
		memset(v,0,sizeof(v));
		n++,m++;//不知道为什么一定要有这个,原本代码没有的
		v[x][y]=1;;
		dfs(x,y,1);

		cout << ans << "\n";
	}

	return 0;
}

这个代码有点迷,没看懂