JZOJ_7.15C组第四题 城市统计

前言

13、14日的题被我吃掉了,有时间再吐出来。

题意

给出一个矩阵,上面有商业区和居民区,以每个点为中心,在它不超过r的直径下计算出这个矩形里每个居民区到离它最近商业区的曼哈顿距离的总和,最后输出一个矩形,上面代表每个点为矩形的答案。

思路

先用bfs求出每个居民区到最近的商业区的距离,之后n22处理以每个点为最右下角,以(1,1)为最左下角的矩形之间的距离总和,后面n22输出就好了。
计算答案:这里写图片描述

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<queue>
using namespace std;
queue<int> x,y;
short dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int f[151][151],ans[151][151],c[151][151],n,t,bx,by,sx,sy,r;
void read(int &tot)
{
    tot=0;
    char c=getchar();
    while (!isdigit(c)) c=getchar();
    while (isdigit(c)) tot=(tot<<3)+(tot<<1)+c-48,c=getchar();
}
void bfs()
{
    int xx,yy,a,b;
    while(!x.empty())
    {
        a=x.front();b=y.front();
        x.pop();y.pop();
        for(int i=0;i<4;i++)
        {
            xx=a+dx[i];yy=b+dy[i];
            if(xx<1||yy<1||xx>n||yy>n||c[xx][yy]==1) continue;
            c[xx][yy]=1;
            f[xx][yy]=f[a][b]+1;
            x.push(xx);y.push(yy);
        }
    }
}
int main()
{   
    read(t);
    while (t--)
    {
        memset(f,0,sizeof(f));
        memset(ans,0,sizeof(ans));
        read(n);
        read(r);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                read(c[i][j]);
                if(c[i][j]) //把商业区入队
                {
                    x.push(i);
                    y.push(j);
                }
            }
        bfs();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                ans[i][j]=ans[i-1][j]+ans[i][j-1]+f[i][j]-ans[i-1][j-1];//处理所有的ans
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                bx=min(n,i+r);by=min(n,j+r);
                sx=max(0,i-r-1);sy=max(0,j-r-1);
                printf("%d ",ans[bx][by]-ans[bx][sy]-ans[sx][by]+ans[sx][sy]);
            }
            printf("\n");
        }
        printf("\n");
    }
}
![](https://cdn.luogu.com.cn/upload/image_hosting/unknowndomain/2025/11/28/IMG20251128143506.jpg) ### 目重述 利用消息队列实现一个简单的客户端/服务器应用:多个客户端进程可并发地向服务器进程发送消息,服务器收到后向每个客户端发送回执。例如,客户端(PID=1234)发送 `"hello"`,服务器回执 `"receipt of<hello> to Process 1234"`。 要求编写服务器和客户端程序,使用消息队列进行通信。 --- ### 给出答案(答案为带注释的代码块) #### 服务端程序:`server.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; // 消息类型 long client_pid; // 客户端 PID char text[MAX_MSG_SIZE]; // 消息正文 }; int main() { key_t key = SERVER_KEY; int msgid = msgget(key, 0666 | IPC_CREAT); if (msgid == -1) { perror("msgget failed"); exit(1); } printf("Server started, waiting for messages...\n"); struct msg_buffer msg; while (1) { // 接收任意类型为1的消息(来自客户端) if (msgrcv(msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv failed"); break; } printf("Server received from PID %ld: %s\n", msg.client_pid, msg.text); // 构造回执消息 char receipt[128]; snprintf(receipt, sizeof(receipt), "receipt of<%s> to Process %ld", msg.text, msg.client_pid); // 根据客户端 PID 查找其专属消息队列 int client_msgid = msgget((key_t)(msg.client_pid % 65535), 0666); if (client_msgid != -1) { struct msg_buffer reply; reply.mtype = 1; // 回复类型 strncpy(reply.text, receipt, MAX_MSG_SIZE); msgsnd(client_msgid, &reply, strlen(reply.text) + 1, 0); } else { printf("No reply queue for PID %ld\n", msg.client_pid); } } // 清理消息队列 msgctl(msgid, IPC_RMID, NULL); return 0; } ``` --- #### 客户端程序:`client.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; long client_pid; char text[MAX_MSG_SIZE]; }; int main() { // 创建自己的消息队列(基于 PID) key_t client_key = getpid() % 65535; int client_msgid = msgget(client_key, 0666 | IPC_CREAT); if (client_msgid == -1) { perror("msgget (client)"); exit(1); } // 连接服务器消息队列 int server_msgid = msgget(SERVER_KEY, 0666); if (server_msgid == -1) { perror("msgget (server)"); exit(1); } struct msg_buffer msg; msg.mtype = 1; // 类型固定为1 msg.client_pid = getpid(); // 记录自身 PID strcpy(msg.text, "hello"); // 发送内容 // 发送到服务器 if (msgsnd(server_msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 0) == -1) { perror("msgsnd"); exit(1); } printf("Client %ld sent: hello\n", msg.client_pid); // 接收回执 struct msg_buffer reply; if (msgrcv(client_msgid, &reply, MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv (reply)"); exit(1); } printf("Client received: %s\n", reply.text); // 删除自己的消息队列 msgctl(client_msgid, IPC_RMID, NULL); return 0; } ``` --- ### 代码解析 1. **服务器逻辑**:创建一个全局消息队列(`SERVER_KEY`),持续监听类型为1的消息。提取客户端PID和消息内容,生成回执,并通过客户端独立的消息队列返回响应。 2. **客户端逻辑**:基于自身PID创建私有消息队列用于接收回执;向服务器公共队列发送包含自身PID和消息内容的数据包;随后等待并打印回执。 3. **通信机制**:使用 `msgget` 按键获取队列ID,`msgsnd` 发送消息,`msgrcv` 接收指定类型消息,`msgctl` 控制资源释放。 4. **多客户端支持**:每个客户端使用其PID模值得到唯一键建立独立接收通道,确保服务器能准确路由回执。 --- ### 知识点(列出该代码中遇到的知识点) - **消息队列(Message Queue)**:内核级进程间通信机制,支持多进程异步收发结构化消息,通过键值定位队列。 - **消息类型(mtype)**:用于过滤接收特定类型消息,实现多路复用或优先级调度。 - **IPC资源标识**:`msgget` 使用 `ftok` 或显式键生成唯一ID,不同进程可通过相同键访问同一资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值