COGS746. [Network Flow 24 questions] Knight Coexistence

Source: Internet
Author: User

Knight Coexistence Issues
«Problem Description:
On a n*n chess board, a horse (Knight) can attack a checkerboard. Board

Some squares are disabled and the knight is not allowed to enter.

«Programming Tasks:
For a given n*n checkered chess board and barrier markers, calculate the maximum number of rides on the board that can be placed
They are not attacking each other.
«Data Entry:
The input data is given by the file knight.in. The first line has 2 positive integers n and M (1<=n<=200, 0<=m<=n*n) <n2),< span= "" >
Indicates the size and number of obstacles for the board, respectively. The next M-line gives the position of the barrier. 2 positive integers per line, indicating a barrier
The grid coordinates of the obstruction.
«Result Output:
Outputs the calculated coexistence Knight number to file Knight.out.
Example output file for input file sample
Knight.in
3 2
1 1

3 3

Knight.out

5

The maximum independent set of the binary graph is converted to the maximum matching of two graph, thus solving with maximum flow.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <queue>6#include <vector>7 using namespacestd;8 Const intmx[9]={0,1,1,-1,-1,2,2,-2,-2};9 Const intmy[9]={0,2,-2,2,-2,1,-1,1,-1};Ten Const intmxn=42000; One intRead () { A     intx=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten-'0'+ch;ch=GetChar ();} the     returnx*F; - } - structedge{intV,nxt,f;} e[mxn<<4]; - inthd[mxn],mct=1; + voidAdd_edge (intUintVintf) { -E[++MCT].V=V;E[MCT].F=F;E[MCT].NXT=HD[U];HD[U]=MCT;return; + } A intn,m; at ints,t; - intD[MXN]; - intid[ About][ About]; - intmp[ About][ About]; - BOOLBFS (intSintt) { -queue<int>Q; inmemset (D,0,sizeofd); -d[s]=1; to Q.push (s); +      while(!Q.empty ()) { -         intu=Q.front (); Q.pop (); the          for(intI=hd[u];i;i=e[i].nxt) { *             intv=e[i].v; $             if(!d[v] &&e[i].f) {Panax Notoginsengd[v]=d[u]+1; - Q.push (v); the             } +         } A     } the     returnD[t]; + } - intDFS (intUintLim) { $     if(u==t)returnLim; $     inttmp,f=0; -      for(intI=hd[u];i;i=e[i].nxt) { -         intv=e[i].v; the         if(d[v]==d[u]+1&&e[i].f) { -tmp=DFS (V,min (LIM,E[I].F));Wuyie[i].f-=tmp; thee[i^1].f+=tmp; -lim-=tmp; Wuf+=tmp; -             if(!lim)returnF; About         } $     } -d[u]=0; -     returnF; - } AInlineintDinic () { +     intres=0; the      while(BFS (s,t)) res+=DFS (s,1e9); -     returnRes; $ } the voidsolve () { the     inti,j; the      for(i=1; i<=n;i++) the       for(j=1; j<=n;j++) -id[i][j]= (I-1) *n+J; in      for(i=1; i<=n;i++) the       for(j=1; j<=n;j++){ the          if(Mp[i][j])Continue; About          if((i+j)%2==0)//White the         { theAdd_edge (S,id[i][j],1); theAdd_edge (Id[i][j],s,0);  +               for(intk=1; k<=8; k++){ -                  intnx=i+mx[k],ny=j+My[k]; the                  if(nx<1|| Nx>n | | ny<1|| Ny>n | | Mp[nx][ny])Continue;BayiAdd_edge (Id[i][j],id[nx][ny],1); theAdd_edge (Id[nx][ny],id[i][j],0); the              } -         } -         Else{//Black theAdd_edge (Id[i][j],t,1); theAdd_edge (T,id[i][j],0); the         } the      } -     return; the } the intMain () the {94Freopen ("knight.in","R", stdin); theFreopen ("Knight.out","W", stdout); theN=read (); m=read (); the     inti,j,u,v;98      for(i=1; i<=m;i++){ AboutU=read (); v=read (); -mp[u][v]=1;//Marker Barriers101     }102s=0; t=n*n+1;103 solve ();104      the     intans=dinic ();106ans=n*n-m-ans;107printf"%d\n", ans);108     return 0;109}

COGS746. [Network Flow 24 questions] Knight Coexistence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.