算法强训day5

今天的题比较温和,都是经典题

一、游游的you

链接:游游的you__牛客网

简单题,经可能凑成you,其他全变成oo

#include <iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		int count = 0;
		int a, b, c;
		cin >> a >> b >> c;
		int m = a > c ? c : a;
		if (b < m)
			m = b;
		count = count + 2 * m;
		b -= m;
		if (b)
			count = count + b - 1;
		cout << count << endl;
	}
}

二、腐烂的苹果

链接:腐烂的苹果_牛客题霸_牛客网

这道题有点意思(对于博主这种新手来说比较难),bfs,两个队列,分别记录上一次腐烂坐标和下一次腐烂坐标,如果最后没腐烂的了直接返回时间,否则返回1

#include<stdio.h>
typedef struct QNode
{
	int x;
	int y;
	struct QNode* next;
}QNode;
typedef struct Queue
{
	QNode* head;
	QNode* rear;
	int size;
}Queue;
void push(Queue* q, int x1, int y1)
{
	QNode* n = (QNode*)malloc(sizeof(QNode));
	n->x = x1;
	n->y = y1;
	n->next = NULL;
	if (q->head == NULL)
	{
		q->head = n;
		q->rear = n;
	}
	else
	{
		q->rear->next = n;
		q->rear = n;
	}
	(q->size)++;
}
void pop(Queue* q)
{
	if (q->size == 1)
	{
		free(q->head);
		q->head = NULL;
		q->rear = NULL;
	}
	else if (q->size > 1)
	{
		QNode* n = q->head->next;
		free(q->head);
		q->head = n;
	}
	(q->size)--;
}
void init(Queue* q)
{
	q->head = NULL;
	q->rear = NULL;
	q->size = 0;
}
int rotApple(int grid[][3], int gridRowLen, int* gridColLen) {
	Queue q1;
	Queue q2;
	init(&q1);
	init(&q2);
	int i = 0;
	int count = 0;
	int sec = 0;
	for (i; i < gridRowLen; i++)
	{
		int j = 0;
		for (j; j < *gridColLen; j++)
		{
			if (grid[i][j] == 2)
			{
				push(&q1, i, j);
			}
			if (grid[i][j] == 1)
				count++;
		}
	}
	if (q1.size == 0)
		return -1;
	if (count == 0)
		return 0;//没有好的苹果直接返回0秒
	while (q1.size || q2.size)
	{
	
		sec++;
		Queue* q = q1.size == 0 ? &q2 : &q1;
		Queue* qn = q1.size == 0 ? &q1 : &q2;
		while (q->size)
		{
			int curx = q->head->x;
			int cury = q->head->y;
			if (curx + 1 < gridRowLen && grid[curx + 1][cury] == 1)
			{
				count--;
				grid[curx + 1][cury] = 2;

				push(qn, curx + 1, cury);
			}
			if (cury + 1 < *gridColLen && grid[curx][cury + 1] == 1)
			{
				count--;
				grid[curx][cury + 1] = 2;
				push(qn, curx, cury + 1);
			}
			if (cury - 1 >= 0 && grid[curx][cury - 1] == 1)
			{
				count--;
				grid[curx][cury - 1] = 2;
				push(qn, curx, cury - 1);
			}
			if (curx - 1 >= 0 && grid[curx - 1][cury] == 1)
			{
				count--;
				grid[curx - 1][cury] = 2;
				push(qn, curx - 1, cury);
			}
			pop(q);
			if (count == 0)
				break;
		}
		if (count == 0)
			break;
	}
	if (count > 0)
		return -1;
	return sec;
}
int main()
{
	int a[3][3] = { 2,0,0,0,0,0,0,0,0 };
	int n = 3;
	printf("%d", rotApple(a, 3, &n));
}

三、孩子们的游戏

链接:孩子们的游戏(圆圈中最后剩下的数)_牛客题霸_牛客网

典型约瑟夫环问题,循环队列秒掉

///**
// * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
// *
// *
// * @param n int整型
// * @param m int整型
// * @return int整型
// */
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
typedef struct SLNode
{
	int val;
	struct SLNode* next;
}SLNode;
void Push(SLNode** head, int x)
{
	SLNode* newn = (SLNode*)malloc(sizeof(SLNode));
	newn->val = x;
	if (*head == NULL)
	{
		*head = newn;
		newn->next = newn;
	}
	else
	{
		SLNode* phead = *head;
		while (phead->next != *head)
		{
			phead = phead->next;
		}
		newn->next = phead->next;
		phead->next = newn;
	}
}
int LastRemaining_Solution(int n, int m) {
	int i = 0;
	SLNode* head = NULL;
	for (i; i < n; i++)
	{
		Push(&head, i);
	}
	while (head->next != head)
	{
		int j = 0;
		for (j; j < m - 2; j++)
		{
			head = head->next;
		}
		SLNode* o = head->next->next;
		free(head->next);
		head->next = o;
		head = o;
	}
	return head->val;
}

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值