HENAU冬令营搜索专题

A

#include<bits/stdc++.h>
using namespace std;
const int N = 10;
char map1[N][N];
int biaoji[N];
int n, k, ans;
void dfs(int a, int sheng)
{
	if (sheng == 0)
	{
		ans++;
		return;
	}
	if (a >= n)return;
	int i;
	for (i = 0; i < n; i++)
	{
		if (map1[a][i] == '#'&&biaoji[i] == 0)
		{
			biaoji[i] = 1;
			dfs(a + 1, sheng - 1);
			biaoji[i] = 0;
		}
	}
	dfs(a + 1, sheng);
	return;
}
int main()
{
	while (scanf("%d%d", &n, &k) && n != -1 && k != -1)
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				cin >> map1[i][j];
		ans = 0;
		dfs(0, k);
		cout << ans << endl;
	}
	return 0;
}

B

#include<bits/stdc++.h>
using namespace std;
int s[10], b[10];
int f[10];
int time1 = 1, plus1 = 0;
int N;
int ans = 0xfffffff;
void dfs(int x)
{
	if (x >= N)return;
	else
	{
		for (int i = 0; i < N; i++)
		{
			if (f[i] == 0)
			{
				time1 *= s[i];
				plus1 += b[i];
				f[i] = 1;
				ans = ans < abs(time1 - plus1) ? ans : abs(time1 - plus1);
				dfs(i + 1);
				time1 /= s[i];
				plus1 -= b[i];
				f[i] = 0;
			}
		}
	}
}
int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> s[i] >> b[i];
	}
	dfs(0);
	cout << ans << endl;
	return 0;
}

C

#include<bits/stdc++.h>
using namespace std;
char s[10];
int main()
{
	gets(s);
	int i;
	do{
		for (i = 0; i < strlen(s); i++)
			cout << s[i];
		cout << endl;
	} while (next_permutation(s, s + strlen(s)));
	return 0;
}

D

#include<bits/stdc++.h>
using namespace std;
int N, n;
int ans[50];
int weizhi = 0;
void chai(int x)
{
	if (n == 0 && weizhi == 1)
	{
		return;
	}
	if (n == 0)
	{
		cout << N << "=";
		for (int i = 1; i < weizhi; i++)
		{
			cout << ans[i] << "+";
		}
		cout << ans[weizhi] << endl;
		return;
	}
	for (int i = x; i <= n; i++)
	{
		ans[++weizhi] = i;
		n -= i;
		chai(i);
		weizhi--;
		n += i;
	}
	return;
}

int main()
{
	cin >> N;
	n = N;
	chai(1);
	return 0;
}

E

#include<bits/stdc++.h>
using namespace std;
int n,N;
int sushu[35];
int a[25];
int chachong[25];
void sushushai()
{
	sushu[1] = 1;
	int i;
	for (i = 2; i <= 34; i++)
	{
		if (sushu[i] == 0)
		{
			for (int j = i * 2; j <= 35; j += i)sushu[j] = 1;
		}
	}
}
void s()
{
	for (int i = 0; i <= n; i++)
		a[i] = i;
}
void dfs(int qwe,int n)
{
	if (qwe == n&&sushu[a[1] + a[n]] == 0)
	{
		cout << a[1];
		for (int i = 2; i <= n; i++)
			cout << " " << a[i];
		cout << endl;
	}
	else
	{
		for (int i = 2; i <= n; i++)
		{
			if (chachong[i] == 0 && sushu[i + a[qwe]] == 0)
			{
				a[qwe+1] = i;
				chachong[i] = 1;
				dfs(qwe + 1,n);
				chachong[i] = 0;
			}
		}
	}
}
int main()
{
	sushushai();
	int i = 1;
	while (scanf("%d", &n) != -1)
	{
		if(i!=1)cout << endl;
		s();
		cout << "Case " << i++ << ":" << endl;
		dfs(1,n);
	}
	return 0;
}

F

#include<bits/stdc++.h>
using namespace std;
char map1[25][25];
int panduan[25][25];
int dx[4] = { 1, 0, 0, -1 }, dy[4] = { 0, 1, -1, 0 };
int w, h;
int shuliang;
int x, y;
void dfs(int x,int y)
{
	shuliang++;
	panduan[x][y] = 1;
	for (int i = 0; i < 4; i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx >= 0 && xx < h&&yy >= 0 && yy < w&&panduan[xx][yy] == 0 && map1[xx][yy] != '#')
			dfs(xx, yy);
	}
}
int main()
{
	while (scanf("%d%d", &w, &h) && w != 0 && h != 0)
	{
		memset(map1, 0, sizeof map1);
		memset(panduan, 0, sizeof panduan);
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
			{
				cin >> map1[i][j];
				if (map1[i][j] == '@')
				{
					x = i;
					y = j;
				}
			}
		shuliang = 0;
		dfs(x,y);
		cout << shuliang << endl;
	}
	return 0;
}

G

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int L, x, y, xxx, yyy;
int step1[305][305];
int dx[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }, dy[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
struct yyx
{
	int a, b;
};
void bfs()
{
	queue<yyx>q;
	q.push({xxx, yyy});
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		for (int i = 0; i < 8; i++)
		{
			int xx = dx[i] + t.a, yy = dy[i] + t.b;
			if (xx >= 0 && xx < L&&yy >= 0 && yy < L&&step1[xx][yy] == 0)
			{
				step1[xx][yy] = step1[t.a][t.b] + 1;
				q.push({ xx, yy });
			}
		}
	}
}
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		cin >> L;
		cin >> xxx >> yyy;
		cin >> x >> y;
		memset(step1, 0, sizeof(step1));
		step1[xxx][yyy] = 1;
		bfs();
		cout << step1[x][y] - 1 << endl;
	}
	return 0;
}

H

#include<bits/stdc++.h>
using namespace std;
int m, n;
char map1[105][105];
int ans;
int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 };
int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };
void dfs(int x, int y)
{
	map1[x][y] = '*';
	for (int i = 0; i < 8; i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx >= 0 && xx < m&&yy >= 0 && yy < n&&map1[xx][yy] == '@')
			dfs(xx, yy);
	}
}
int main()
{
	while (scanf("%d%d", &m, &n), m != 0)
	{
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> map1[i][j];
		ans = 0;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
			{
				if (map1[i][j] == '@')
				{
					dfs(i, j);
					ans++;
				}
			}
		cout << ans << endl;
	}
	return 0;
}

I

#include<bits/stdc++.h>
using namespace std;
int m, n;
char map1[105][105];
int ans;
int dx[8] = { 1, 1, 1, 0, -1, -1, -1, 0 };
int dy[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };
void dfs(int x, int y)
{
	map1[x][y] = '.';
	for (int i = 0; i < 8; i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx >= 0 && xx < m&&yy >= 0 && yy < n&&map1[xx][yy] == 'W')
			dfs(xx, yy);
	}
}
int main()
{
	scanf("%d%d", &m, &n);
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> map1[i][j];
		ans = 0;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
			{
				if (map1[i][j] == 'W')
				{
					dfs(i, j);
					ans++;
				}
			}
		cout << ans << endl;
	return 0;
}

J

#include<bits/stdc++.h>
using namespace std;
int shu[100005][2];
void dfs(int zuo)
{
	cout << zuo << endl;
	if (shu[zuo][0] != 0)dfs(shu[zuo][0]);
	if (shu[zuo][1] != 0)dfs(shu[zuo][1]);
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> shu[i][0] >> shu[i][1];
	dfs(1);
	return 0;
}

K

#include<bits/stdc++.h>
using namespace std;
struct PII
{
	int a;
	int b;
};
const int N = 15;
int n, m;
int a, b;
int aa,bb;
char g[N][N];
int d[N][N];
int bfs()
{
	queue<PII>q;
	memset(d, -1, sizeof d);
	d[a][b] = 0;
	q.push({ a, b });
	int dx[4] = { -1, 0, 0, 1 },dy[4] = { 0, 1, -1, 0 };
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = t.a + dx[i];
			int y = t.b + dy[i];
			if (x >= 0 && x < n&&y >= 0 && y < m&&(g[x][y] == '.'||g[x][y]=='T') && d[x][y] == -1)
			{
				d[x][y] = d[t.a][t.b] + 1;
				q.push({ x, y });
			}
		}
	}
	return d[aa][bb];
}
int main()
{
	cin >> n >> m;
	for (int l = 0; l < n; l++)
		for (int o = 0; o < m; o++)
		{
			cin >> g[l][o];
			if (g[l][o] == 'S')
			{
				a = l;
				b = o;
			}
			if (g[l][o] == 'T')
			{
				aa = l;
				bb = o;
			}
		}
	if (bfs() == -1)cout << "no" << endl;
	else cout << "yes" << endl;
	return 0;
}

L

#include<bits/stdc++.h>
using namespace std;
int t, n, m, x, y;
int ans = 0;
int gezi;
int map1[10][10];
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 step)
{
	if (step == gezi)
	{
		ans++;
		return;
	}
	else
	{
		for (int i = 0; i < 8; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx >= 0 && xx < n&&yy >= 0 && yy < m&&map1[xx][yy] == 0)
			{
				map1[xx][yy] = 1;
				dfs(xx, yy, step + 1);
				map1[xx][yy] = 0;
			}
		}
	}
}
int main()
{
	cin >> t;
	while (t--)
	{
		ans = 0;
		memset(map1, 0, sizeof map1);
		cin >> n >> m >> x >> y;
		gezi = m*n;
		map1[x][y] = 1;
		dfs(x, y, 1);
		cout << ans << endl;
	}
	return 0;
}

M

#include<bits/stdc++.h>
using namespace std;
int k;
int map1[10][10];
int panduan[10];
int duijiao[20];
int xieduijiao[20];
int sum=0;
int sum1 = 0;
void dfs(int a,int sheng)
{
	if (sheng == 0)
	{
		sum1 = max(sum, sum1);
		return;
	}
	if (a >= 8)
	{
		sum = 0;
		return;
	}
	for (int i = 0; i < 8; i++)
	{
		if (panduan[i]==0&&duijiao[a+i]==0&&xieduijiao[a-i+8]==0)
		{
			int aa = a;
			int ii = i;
				panduan[i] = 1;
				duijiao[a + i] = 1;
				xieduijiao[a-i+8] = 1;
				sum += map1[a][i];
				dfs(a + 1, sheng - 1);
				panduan[i] = 0;
				duijiao[a + i] = 0;
				xieduijiao[a-i+8] = 0;
				sum -= map1[a][i];
		}
	}
}
int main()
{
	cin >> k;
	while (k--)
	{
		sum1 = 0;
		sum = 0;
		memset(map1, 0, sizeof map1);
		memset(panduan, 0, sizeof panduan);
		memset(duijiao, 0, sizeof duijiao);
		memset(xieduijiao, 0, sizeof xieduijiao);
		for (int i = 0; i < 8; i++)
			for (int j = 0; j < 8; j++)
				cin >> map1[i][j];
		sum = 0;
		dfs(0, 8);
		cout << sum1 << endl;
	}
}

 N

#include<bits/stdc++.h>
using namespace std;
const long long N = 1e8 + 5;
int chachong[25];
int n, k;
int s[25];
int ans = 0;
int sum;
int sushu(int a)
{
	if (a == 1)return 0;
	for (int i = 2; i <= sqrt(a); i++)
	{
		if (a%i == 0)return 0;
	}
	return 1;
}
void dfs(int geshu,int last)
{
	if (geshu == k)
	{
		if (sushu(sum) == 1)ans++;
		return;
	}
	else
	{
		for (int i = last; i <= n; i++)
		{
			if (chachong[i] == 0)
			{
				chachong[i] = 1;
				sum += s[i];
				dfs(geshu + 1, i + 1);
				chachong[i] = 0;
				sum -= s[i];
			}
		}
	}
}
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)cin >> s[i];
	dfs(0, 1);
	cout << ans << endl;
	return 0;
}

O

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll r, c;
char map1[505][505];
ll ans[505][505];
ll zouxiang[505][505][4];
ll panduan[505][505];
int dx[4] = { 1, 1, -1, -1 }, dy[4] = { 1, -1, -1, 1 };
struct yyx
{
	int a, b;
};
void bfs()
{
	deque<yyx>q;
	q.push_front({ 0, 0 });
	ans[0][0] = 0;
	while (q.size())
	{
		auto t = q.front();
		q.pop_front();
		ll x = t.a, y = t.b;
		panduan[x][y] = 1;
		for (int i = 0; i < 4; i++)
		{
			ll xx = x + dx[i], yy = y + dy[i];
			if (xx >= 0 && xx <= r&&yy >= 0 && yy <= c&&panduan[xx][yy] == 0)
			{
				if (zouxiang[x][y][i] == 1)
				{
					if (ans[x][y] < ans[xx][yy])
					{
						ans[xx][yy] = ans[x][y];
						q.push_front({ xx, yy });
					}
				}
				else if (ans[x][y] + 1 < ans[xx][yy])
				{
					ans[xx][yy] = ans[x][y] + 1;
					q.push_back({ xx, yy });
				}
			}
		}
	}
	cout << ans[r][c]<<endl;
}
int main()
{
	cin >> r >> c;
	memset(ans, 0x7f, sizeof ans);
	memset(zouxiang, 0, sizeof zouxiang);
	memset(panduan, 0, sizeof panduan);
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
		{
			cin >> map1[i][j];
			if (map1[i][j] == '/')
			{
				zouxiang[i][j + 1][1] = 1;
				zouxiang[i + 1][j][3] = 1;
			}
			else
			{
				zouxiang[i][j][0] = 1;
				zouxiang[i + 1][j + 1][2] = 1;
			}
		}
	if (abs(r - c) & 1)cout << "NO SOLUTION" << endl;
	else bfs();
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值