Rokua P1263 Court Guard

Source: Internet
Author: User

Title Description

Once there was a kingdom, the castle of this kingdom was a rectangle, and was divided into a square of MXN. Some squares are walls, while others are open spaces. The King of the Kingdom set some traps in the castle, and each trap occupied a vacant space.

One day, the king decided to lay the guards in the castle, and he wanted to arrange as many guards as possible. The guards were rigorously trained, so they shot at the man as soon as they found someone in the same row or in the same column. Therefore, the king wanted to be able to properly arrange the guards so that they could not see each other, so that they could not shoot each other. The guards can only be placed in the open space, cannot be placed on traps or walls, and a vacant space can only be placed on a guard. If two guards are in the same row or column, and there is no wall between them, they can see each other. (The guard is like a car in chess)

Your task is to write a program that calculates the maximum number of guards that can be placed, based on a given castle, and designs the layout of the plan.

Input/output format Input Format:

The first line, two integers M and N (1≤m,n≤200), represents the size of the castle.

The next M-row N-column integer that describes the castle's topography. The number of J columns in row I is expressed in ai,j.

Ai,j=0, indicating that the square [i,j] is a vacant space;

Ai,j=1, indicating that the grid [I,J] is a trap;

Ai,j=2, indicating that the square [i,j] is a wall.

output Format:

The first line is an integer k, which indicates that a maximum of K guards can be arranged.

Thereafter k lines, two integers per line, Xi and Yi, describe the position of a guard.

Input/Output sample Input Sample # #:
3 42 0 0 02 2 2 10 1 0 2
Sample # # of output:
21 23 3
Description

Sample Data 5-2 (black squares for walls, white squares for open spaces, circles for traps, G for guards)

The beginning of the formulation is BFS out of each unicom block, alone processing.

↑ obviously wrong

It was later found that the same line, separated by walls, could be treated in two rows.

Then the binary graph matches the coordinates.

The understanding of the dichotomy is still not thorough ...

(Code only provides ideas, not guaranteed correctness) (No SPJ program)

1 /*by Silvern*/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 #defineLL Long Long8 using namespacestd;9 Const intmxn=2200;Ten intRead () { One     intx=0, f=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; the } - structedge{ -     intv,nxt; -}e[mxn* -]; + inthd[mxn*Ten],mct=0; - voidAdd_edge (intUintv) { +E[++MCT].V=V;E[MCT].NXT=HD[U];HD[U]=MCT;return; A } at intMP[MXN][MXN]; - intIdx,idy; - intHS[MXN][MXN]; - intAIDX[MXN],AIDY[MXN]; - intLINK[MXN],VIS[MXN]; - // in intn,m,ans=0; - BOOLDFS (intu) { to      for(intI=hd[u];i;i=e[i].nxt) { +         intv=e[i].v; -         if(!Vis[v]) { thevis[v]=1; *             if(link[v]==-1||DFS (Link[v])) { $link[v]=u;Panax Notoginseng                 return 1; -             } the         } +     } A     return 0; the } + voidsolve () { -memset (link,-1,sizeoflink); $      for(intI=1; i<=idx;i++){ $memset (Vis,0,sizeofvis); -         if(DFS (i)) ans++; -     } the     return; - }Wuyi voidinit () { the     inti,j; -     BOOLflag=1; Wu      for(i=1; i<=m;i++){ -flag=1; About          for(j=1; j<=n;j++){ $             if(mp[i][j]==2) {flag=1;Continue;} -             if(flag) { -++idx; -aidx[idx]=i; Aflag=0; +             } thehs[i][j]=idx; -         } $     } theidy=idx; the      for(j=1; j<=n;j++){ theflag=1; the          for(i=1; i<=m;i++){ -             if(mp[i][j]==2) {flag=1;Continue;} in             if(flag) { the++Idy; theaidy[idy]=J; Aboutflag=0; the             } the             if(Mp[i][j])Continue; the Add_edge (Idy,hs[i][j]); + Add_edge (hs[i][j],idy); - //printf ("%d%d:%d\n", i,j,idy); the         }Bayi     } the     return; the } - intMain () { -M=read (); n=read (); the     inti,j; the      for(i=1; i<=m;i++) the          for(j=1; j<=n;j++) themp[i][j]=read (); - init (); the solve (); the /*For (i=1;i<=m;i++) { the For (j=1;j<=n;j++) {94 printf ("%d", hs[i][j]); the      } the printf ("\ n"); the     }*/98printf"%d\n", ans); About      for(i=idx+1; i<=idy;i++){ -         if(link[i]!=-1) printf ("%d%d\n", Aidx[link[i]],aidy[i]);101     }102     return 0; 103}

Rokua P1263 Court Guard

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.