題目意思:
給一個n*n的矩陣,裡面有m個障礙點,問最多可以在不是角落的邊框上放多少個點,使得所有點的同時向對面移動,不遇到障礙點且不相互沖突。
解題思路:
貪心思想。
首先障礙點所在行和列不能放,然後當n為奇數時,n/2+1行和列只能放一個,其他行或列只要能放就放,因為不沖突,對於任意一行和一列,四種情況中必有一種滿足他們兩之間不沖突。
代碼:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/
bool row[1100],col[1100];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
row[a]=true; //障礙點所在行和列不能放
col[b]=true;
}
int ans=0;
for(int i=2;i<n;i++) //角落的點不能算
{
if(!row[i])
ans++;
if(!col[i])
ans++;
}
if((n&1)&&(!row[n/2+1]&&!col[n/2+1])) //n為奇數時,中間的行和列只能算一次,四種方法都不能解決沖突
ans--;
printf("%d\n",ans);
}
return 0;
}