AcWing 1491.圆桌座位 解题思路及代码

看数论看烦了,随便找到题换换脑子,结果就遇到了这题,还挺有意思的,有几个思维难点。

先贴个题目:

 以及原题链接:1491. 圆桌座位 - AcWing题库icon-default.png?t=N7T8https://www.acwing.com/problem/content/description/1493/

 几个思维难点,1.怎么确认能否坐在上一个人旁边,我选择用类似建图的方式,用一个unordered_map存每个人身边不能坐的人。2.仅有旋转角不同的方案视为一种方案,这个思维难点让我考虑了很久,怎么去确定你所谓的旋转角呢?然后思考后发现,这所谓的排排坐,就好像一个环,那你只要在某个点固定一个数,那其实就可以搜得所有的答案,所以我把1固定在了第一位(这里指存储的数组第一位,环没有第一位之说),解决了这两个问题,剩下的就是一个简单的排列型枚举。

代码如下:

#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long ll;

const int N = (2 << 10) + 10;
const int M = 20;

int n, m;
ll cnt = 0;
int tmp[M];
bool st[M];
unordered_map<int, int> h[M];

void dfs(int x, int father)
{
    if (x > n)
    {
        if(h[1].count(tmp[x-1]))
            return;
        cnt++;
        return;
    }
    for (int i = 2; i < n + 1; ++i)
    {
        if (!st[i] && !h[father].count(i))
        {
            tmp[x] = i;
            st[i] = true;
            dfs(x + 1, i);
            st[i] = false;
        }
    }
}

int main()
{
    cin >> n >> m;
    tmp[1] = 1;
    int a, b;
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d", &a, &b);
        h[a][b] = 1;
        h[b][a] = 1;
    }
    dfs(2, 1);
    cout << cnt;
}

注意在搜完要退出时,不要忘记判断最后一个数和1是否可以相邻,因为是一个环。

if(h[1].count(tmp[x-1]))
            return;

by————2024.4.11刷题记录

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的圆桌会议环形队列的 C 语言代码实现: ```c #include <stdio.h> #define MAX_SIZE 10 int queue[MAX_SIZE]; int front = -1; int rear = -1; int isFull() { if ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1)) { return 1; } else { return 0; } } int isEmpty() { if (front == -1) { return 1; } else { return 0; } } void enqueue(int data) { if (isFull()) { printf("Queue is full!\n"); } else { if (front == -1) { front = 0; } rear = (rear + 1) % MAX_SIZE; queue[rear] = data; } } int dequeue() { int data; if (isEmpty()) { printf("Queue is empty!\n"); return (-1); } else { data = queue[front]; if (front == rear) { front = -1; rear = -1; } else { front = (front + 1) % MAX_SIZE; } return (data); } } void display() { int i; if (isEmpty()) { printf("Queue is empty!\n"); } else { printf("Queue elements are: \n"); for (i = front; i != rear; i = (i + 1) % MAX_SIZE) { printf("%d ", queue[i]); } printf("%d", queue[i]); } } int main() { int choice, data; while (1) { printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Display\n"); printf("4. Quit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter data to enqueue: "); scanf("%d", &data); enqueue(data); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(1); default: printf("Invalid choice!\n"); } } return 0; } ``` 这个实现使用了一个固定大小的数组来存储元素,并使用了 front 和 rear 指针来跟踪队列的开头和结尾。enqueue 和 dequeue 操作都考虑了环形队列的特殊性质。isplay 操作只是简单地遍历队列并输出元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值