Https://vijos.org/p/1729
This is a wonderful question. Why n will be re when it reaches 30 .......... N <= 26 ....
Sad
According to the distribution of pawns, each attack must be on a black or white lattice, so the Binary Graph is directly used.
(But I thought, how to do it ............................
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=50;int ihead[N*N], cnt, ly[N*N], n, m, vis[N][N], ans, ins[N*N];const int dx[]={-1, -2, -2, -1, 1, 2, 2, 1}, dy[]={-2, -1, 1, 2, 2, 1, -1, -2};struct ED { int next, to; }e[(N*N)<<1];struct dat{ int x, y; }a[N*N];void add(int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;}bool ifind(int x) {int y;for(int i=ihead[x]; i; i=e[i].next) if(!ins[y=e[i].to]) {ins[y]=1;if(!ly[y] || ifind(ly[y])) {ly[y]=x;return true;}}return false;}int main() {read(n); read(m);char s[10];for1(i, 1, m) {scanf("%s", s+1);a[i].x=s[1]-‘A‘+1;int len=strlen(s+1);for1(j, 2, len) {a[i].y*=10;a[i].y+=s[j]-‘0‘;}vis[a[i].x][a[i].y]=i;}for1(i, 1, m) {rep(k, 8) {int fx=dx[k]+a[i].x, fy=dy[k]+a[i].y;if(fx<1 || fy<1 || fx>n || fy>n || !vis[fx][fy]) continue;add(i, vis[fx][fy]);}}for1(i, 1, m) if((a[i].x+a[i].y)&1) {CC(ins, 0);if(ifind(i)) ++ans;}printf("%d\n", ans);return 0;}
Description
Some knights are placed on an N * n square board. We mark the row of the board with N natural numbers starting from 1, and mark the column with N uppercase letters starting with 'A. For example, the row of a standard 8*8 chess board is marked as 1 .. 8. Mark the column as .. h, D3, and H1 represent the grids of 3rd columns in 4th rows and 1st columns in 8th rows.
A knight is such a pawn. If a server guard is placed in a grid (x, y ). So lattice (X-2, Y-1), (X-2, Y + 1), (x-1, y-2), (x-1, Y + 2), (x + 1, y-2 ), (x + 1, Y + 2), (X + 2, Y-1), (X + 2, Y + 1) If inside the board, the server guard is under attack.
If one of the placement methods of several knights on the Board makes no server guard within the attack range of other knights, it is called a harmonious solution. Given a chessboard, M server guard has been placed on it. Your task is to take as few knights as possible, so that the remaining knights constitute a harmonious solution.
Format input format
The first line contains two positive integers, N and M, indicating the size of the Board and the number of the server guard.
The following M lines, each line of a string, describe the coordinates of a server guard.
Output Format
Output a line, an integer, indicating at least how many server guard are taken.
Example 1 input 1 [copy]
6 9A1A5B3C5C1D2D4E6F5
Sample output 1 [copy]
3
Restrictions
1 s for each test point
Prompt
30% of the data is satisfied, 1 <= n <= 4.
100% of the data is satisfied. 1 <= n <= 26. The coordinate format of the server guard is valid, and the positions of any two server guard are different.
Source
Topcoder
[Vijos] 1729 knights (Hungary)