问题描述
 We are given a chess-board of size n*n, 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.
n*n的棋盘,有m个点不能放置骑士
之后m行为不得放置骑士的坐标
输出
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.
样例输入
3 2
1 1
3 3
样例输出
5
算法讨论
这里的方法有点慢,把能放置骑士的点枚举,将它所能攻击的点标号,连接起来,然后用匹配做一遍最大覆盖集,将总点数减去最大覆盖集的点数就是我们求的答案。

#include<cstdio>
#include<cstring>
using namespace std;
int dx[9]={0,-1,-2,-2,-1,1,2,2,1};
int dy[9]={0,-2,-1,1,2,2,1,-1,-2};
int a[40005][9];
bool f[405][405];
int num[405][405];
bool v[40005];
int link[40005];
int n,m,s,ans;bool check(int x,int y)
{if (x<1||x>n||y<1||y>n) return false;if (f[x][y]) return false;return true;
}bool find(int x)
{int p;for (int i=1;i<=a[x][0];i++)if (v[a[x][i]]==false){p=link[a[x][i]];link[a[x][i]]=x;v[a[x][i]]=true;if ((p==0)||(find(p)))return true;link[a[x][i]]=p;}return false;
}int main()
{scanf("%d%d",&n,&m);int x,y;for (int i=1;i<=m;i++){scanf("%d%d",&x,&y);f[x][y]=true;}s=0;for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)if (f[i][j]==false){s++;num[i][j]=s;}   int p=0;    for (int i=1;i<=n;i++)for (int j=1;j<=n;j++)if (f[i][j]==false){p=num[i][j];int t=0;for (int k=1;k<=8;k++){x=i+dx[k];y=j+dy[k];if (check(x,y)){t++;a[p][t]=num[x][y];}}a[p][0]=t;}for (int i=1;i<=s;i++){if (a[i][0]==0)continue;memset(v,0,sizeof(v));if (find(i))ans++;}ans=ans/2;   printf("%d",s-ans);return 0;
}


Pixiv ID:46661498

【SSLGZ 1344】Knights相关推荐

  1. 【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)

    题干: Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in dist ...

  2. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

  3. 【51Nod - 1344】走格子 (思维)

    题干: 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去.机器人有一个初始能量,每个格子对应一个整数Aii,表示这个格子的能量值.如果Aii > 0, ...

  4. 【SSLGZ 2812】2017年10月30日提高组T2 凤凰院真凶

    问题描述 输入 输出 第一行一个整数k,表示最长公共合法事件序列的长度. 样例输入 5 1 4 2 5 1 4 1 1 2 4 样例输出 2 数据范围 算法讨论 f[i][j]为a到第i个位置,b到第 ...

  5. 【SSLGZ 1614】医院设置(Dijkstra)

    问题描述 一颗二叉树,圆圈内为居民人数,约定两点之间路径长度为每个居民走的路径总长.现在要找一点设立医院,使所有点到医院距离最短(说白了就是图的中心). 输入 第一行一个整数n,表示树的结点数.(n& ...

  6. 【SSLGZ 2811】2017年10月30日提高组T2 摘Galo

    问题描述 0v0在野外看到了一棵Galo树,看到食物的0v0瞪大了眼睛,变成了OvO. 这棵Galo树可以看做是一棵以1号点为根的n个点的有根数,除了根节点以外,每个节点i都有一个Galo,美味度为w ...

  7. 【SSLGZ 2134】猫猫的小鱼

    问题描述 猫猫是丛林里很多动物心中的天使,她为此十分自豪.猫猫最爱吃鱼了,她每天都要去池塘钓鱼吃.猫猫经常吃鱼脑,数学特别强,然而,小女生的性格决定了她的贪玩. 一天,猫猫钓到了很多条鱼.她并不想马上 ...

  8. 【SSLGZ 1579】泽泽在巴西

    问题描述 泽泽帮助了英国某街道尽量减少酸雨的伤害,街道办主任非常感激他,就把他领到一扇门前,告诉他这扇门能通往好地方,具体好到什么程度要看泽泽人品.泽泽毫不犹豫地走了进去-- 泽泽来到了足球王国--巴 ...

  9. 【SSLGZ 1618】剑鱼行动

    问题描述 给出N个点的坐标,对它们建立一个最小生成树,代价就是连接它们的路径的长度,现要求总长度最小.N的值在100以内,坐标值在[-10000,10000].结果保留二位小数. 样例输入 5 --- ...

最新文章

  1. js 验证码 倒计时60秒
  2. XMPP协议简单介绍
  3. 以太网应用于控制时存在的问题
  4. nginx 80端口重定向到443端口
  5. 数据脱敏:数仓安全隐私保护见真招儿
  6. 在php中使用cookie,PHP中如何使用Cookie
  7. 牛客练习赛24 C PH试纸
  8. 移动应用的黄金四分类
  9. 阿里云天池 零基础入门NLP - 新闻文本分类 2种做法,F1=0.87
  10. 2019.2.4时GitHub超过15个粉丝就可以领取3000元啦!
  11. Latex:TexStudio的使用
  12. ZOJ 1111 Poker Hands
  13. java实现控制继电器_单片机控制继电器
  14. 一、无线通信中,工作频率与带宽的关系
  15. 京东云首次发布数智供应链全景图 锚定产业数字化新赛道
  16. 只有VOB文件也可以制作成DVD——用IfoEdit建立IFO篇(最好的方法)
  17. Java设计模式之外观模式(门面模式,迪米特法则的具体实现,抽象外观类改进)
  18. python笔记更新(正则表达式)
  19. JAVA数组 添加数组元素并排序---下标越界问题
  20. Web前后端缓存技术

热门文章

  1. 显卡html5性能测试工具,电脑显卡性能测试软件
  2. 从《亮剑》看先进的管理(原创)
  3. 基于Thinkphp5+phpQuery 网络爬虫抓取数据接口,统一输出接口数据api
  4. 华为云“链”接元宇宙
  5. 课件(Part 1, PJ)
  6. ActiveMQ双机热备时的连接跳动问题解决
  7. 国内人脸识别发展到什么地步了?一文带你了解详情
  8. SIP协议-04 SIP头域
  9. 【微机原理 实验】可编程外围接口芯片8255(4)(硬件-含汇编代码)
  10. 后端resection部分(后方交会,PnP、P3P、EPnp、Nakano P3P)