14:洛谷打题记录

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int ans = 100000000;
int n;
char arr[110][100];
bool vis[110][110];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void dfs(int x, int y, int cnt,char c)
{
	if (arr[x][y] == 'B')
	{
		if (cnt < ans)
		{
			ans = cnt;
		}
		return;
	}
	for (int i = 0;i < 4;i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx >= 1 && yy >= 1 && xx <= n && yy <= n && vis[xx][yy] && arr[xx][yy] != arr[x][y] && cnt + 1 < ans)
		{
			vis[xx][yy] = 0;
			dfs(xx, yy, cnt + 1, arr[xx][yy]);
			vis[xx][yy] = 1;
		}
	}
}
int main()
{
	cin >> n;
	int a, b;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			cin >> arr[i][j];
			if (arr[i][j] == 'A')
			{
				a = i;
				b = j;
			}
			
		}
	}
	memset(vis, 1, sizeof(vis));
	vis[a][b] = 0;
	dfs(a, b, 0, 'q');
	if (ans == 100000000)
	{
		cout << -1 << endl;
	}
	else
	{
		cout << ans << endl;
	}
	return 0;
}

#include<iostream>
#include<cstring>
using namespace std;
#include<string>
int arr[30];
int main()
{
	string str;
	int k;
	cin >> k;
	cin >> str;
	int cnt = 0;
	int l = str.length();
	if (l % k != 0)
	{
		cout << -1 << endl;
		return 0;
	}
	else
	{
		int n = l / k;
		for (int i = 0;i < n;i++)
		{
			memset(arr, 0, sizeof(arr));
			for (int j = 0;j < k;j++)
			{
				char c = str[i + j * n];
				int x = int(c - 96);
				arr[x]++;
			}
			int u = 0;
			for (int i = 1;i <= 27;i++)
			{
				if (arr[i] > u)
				{
					u = arr[i];
				}
			}
			cnt += k - u;
		}
	}
	cout << cnt << endl;
	return 0;
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值