Knights 题解

Knights

题目

We are given a chess-board of size nn, from which some fields have been removed. The task is to determine the maximum number of knights that can be placed on the remaining fields of the board in such a way that none of them check each other.
  一张大小为n
n的国际象棋棋盘,上面有一些格子被拿走了,棋盘规模n不超过200。马的攻击方向如下图,其中S处为马位置,标有X的点为该马的攻击点。
  
Fig.1: A knight placed on the field S checks fields marked with x.
Write a program, that:
reads the description of a chess-board with some fields removed, from the input file kni.in,
determines the maximum number of knights that can be placed on the chess-board in such a way that none of them check each other,
writes the result to the output file kni.out.
你的任务是确定在这个棋盘上放置尽可能多的马,并使他们不互相攻击。


输入

The first line of the input file kni.in contains two integers n and m, separated by a single space, 1<=n<=200, 0<=m<n2; n is the chess-board size and m is the number of removed fields. Each of the following m lines contains two integers: x and y, separated by a single space, 1<=x,y<=n – these are the coordinates of the removed fields. The coordinates of the upper left corner of the board are (1,1), and of the bottom right are (n,n). The removed fields are not repeated in the file.


输出

The output file kni.out should contain one integer (in the first and only line of the file). It should be the maximum number of knights that can be placed on the given chess-board without checking each other.


样例

input
3 2
1 1
3 3

output
5


解题思路

分黑白点两个集合
黑点只能跳到白点
被拿走的格子不能跳
连接两个点的编号 (i-1)* n+j
遍历黑点
求出最大匹配数
用总格子数-被拿去的格子数-最大匹配数=最大独立集


代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int fx[9]={0,1,2,1,2,-1,-1,-2,-2};
const int fy[9]={0,2,1,-2,-1,2,-2,1,-1};
struct hhx{int to,next;
}a[500020];
long long n,m,ans;
int x,y,t,c[520][520],b[42000],p[42000],head[42000];
void add(long long x,long long y)  //连接
{a[++t].to=y;a[t].next=head[x];head[x]=t;
}
bool dfs(int d)
{for (int i=head[d];i;i=a[i].next)  //找匹配if (p[a[i].to]==0)  //没有走过{p[a[i].to]=1;  //标记if (b[a[i].to]==0 || dfs(b[a[i].to]))  //没有被匹配,或者可以被让出来{b[a[i].to]=d;  //匹配return 1;}}return 0;
}
int main()
{scanf("%lld%lld",&n,&m); for (int i=1;i<=m;i++){scanf("%d%d",&x,&y);c[x][y]=1; } for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)if (!c[i][j] && (i+j)%2==0)  //求黑点for (int k=1;k<=8;k++)if (i+fx[k]<=n && i+fx[k]>0 && j+fy[k]<=n && j+fy[k]>0 && !c[i+fx[k]][j+fy[k]])  //可以走到的点add((i-1)*n+j,(i+fx[k]-1)*n+(j+fy[k])); for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)if (!c[i][j] && (i+j)%2==0)   //求黑点{memset(p,0,sizeof(p));if (dfs((i-1)*n+j))  //遍历黑点ans++;}printf("%lld",n*n-m-ans);  //最终答案return 0;
}

Knights 题解相关推荐

  1. [题解] Knights of Ni 骑士 C++

    Knights of Ni 骑士 题目 Description Input Output Sample Input Sample Output 思路 代码 题目 Description 给出一张W*H ...

  2. AOAPC I: Beginning Algorithm Contests 题解

    AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...

  3. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  4. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  5. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  6. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  7. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  8. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  9. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

最新文章

  1. 解决虚拟器device support x86 but apk only supports armeabi-v7
  2. java8 stream_使用Java Stream摘要统计
  3. smbus使用 树莓派_树莓派学习笔记——I2C使用 PCF8574 Python SMBUS
  4. change事件判断ajax,jquery中change事件里面if语句失效
  5. 由旋转画廊,看自定义RecyclerView.LayoutManager
  6. 将RGB值转换为灰度值的简单算法(转)
  7. ArcGIS 10——地理数据库管理GIS数据
  8. 【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式队列
  9. 9.22 keep studying
  10. windows程序窗体创建流程模型A--利用基本数据类型
  11. rebar3 的使用
  12. Mac重启和关机没反应解决办法
  13. 大学兼职一般做什么?有哪些职业?
  14. Mac 开启局域网smb文件共享(附全平台连接方法)
  15. Boofuzz协议漏洞挖掘入门教程与使用心得
  16. SSH端口转发Forwarding及隧道Tunneling
  17. android自定义大括弧
  18. SpringBoot HATEOAS用法简介(入门)
  19. 机械学习篇——数据预处理
  20. 李宏毅老师《机器学习》课程笔记-2.2 为什么是“深度”学习?

热门文章

  1. 语音识别研究综述——阅读笔记4(总结与展望)
  2. PIC单片机之中断程序
  3. Unity Live2D 让模型自己动起来 Harmonic Motion Controller 学习使用
  4. Springboot 自定义模板导出Excel文件
  5. 你不知道的CSS霓虹灯文字总结
  6. 快手校招Java后端面经
  7. 中兴回应被列入被执行人名单;摩拜否认裁员 30%;LG 支持韩国对高通罚款 9 亿美元 | 雷锋早报...
  8. SIP协议之呼叫流程
  9. 中国的市场营销部到底是怎么分工的,每个人到底在干什么事?
  10. 独立版:零点城市社交电商V2.1.9.8 新增多宝鱼第三方商品插件