HDU 3446 Daizhenyang ' s chess

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=3446

Test instructions: A chessboard, there is a king, some can go to the point, every time can only go to the place, no way to lose, to determine whether the win.

Idea: First remove the location of king, only consider the other, if the matching number and the number of King to find the same, that the location of the king is not in the match, so the hand win, otherwise the initiator win, why?

Can think about, the matching path is a: matching, mismatch, matching ... Mismatch, matching, so if King is in the match, then the last step can also be the initiator to go.

#include <cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>intdx[]={-1,-1,-1,1,1,1,0,0,2,-2,2,-2,2,-2,2,-2,1,-1,-1,1};intdy[]={-1,1,0,0,1,-1,-1,1,2,2,-2,-2,1,-1,-1,1,2,-2,2,-2};intmatch[ -],g[ -][ -],inpath[ -],q[ -],head,tail,newbase;intinqueue[ -],inblossom[ -],Base[ -],father[ -],n;intfinish,start,r,c,kx,ky,c[ -];Chars[ -][ -];intRead () {intt=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} returnt*F;}intLcaintUintv) {memset (Inpath,0,sizeofInpath);  while(1) {u=Base[u]; Inpath[u]=1; if(!match[u]) Break; U=Father[match[u]]; }     while(1) {v=Base[v]; if(Inpath[v]) Break; V=Father[match[v]]; }    returnv;}voidResetintu) {     while(u!=newbase) {        intv=Match[u]; inblossom[Base[V]] =inblossom[Base[U]] =1; U=Father[v]; if(Base[U]!=newbase] Father[u]=v; }}voidBlossomcontract (intUintv) {Newbase=LCA (U,V); memset (Inblossom,0,sizeofInblossom);    Reset (U);    Reset (v); if(Base[U]!=newbase] Father[u]=v; if(Base[V]!=newbase] Father[v]=u;  for(intI=1; i<=n;i++)      if(inblossom[Base[i]]) {            Base[i]=newbase; if(!inqueue[i]) c[++tail]=i,inqueue[i]=1; }}voidFindaugmentingpath () {memset (Inqueue,0,sizeofinqueue); memset (father,0,sizeoffather);  for(intI=1; i<=n;i++)Base[i]=i; Head=1; tail=1; c[1]=start;inqueue[start]=1; Finish=0;  while(head<=tail) {        intu=c[head++];  for(intv=1; v<=n;v++)         if(g[u][v]&&Base[u]!=Base[v]&&match[v]!=u) {                if(v==start| | (match[v]>0) && (father[match[v]]>0) {blossomcontract (u,v); }Else                if(father[v]==0) {Father[v]=u; if(Match[v]) {c[++tail]=match[v];inqueue[match[v]]=1; }Else{Finish=v; return; }                }         }    }}voidAugmentpath () {intu,v,w; U=finish;  while(u>0) {v=Father[u]; W=Match[v]; Match[u]=v; MATCH[V]=u; U=W; }}intsolve () {intres=0; memset (Match,0,sizeofmatch);  for(intI=1; i<=n;i++)     if(!Match[i]) {Start=i;        Findaugmentingpath (); if(finish) Augmentpath (), res++; }    returnRes;}intMain () {intT=read (), tcase=0;  while(t--) {printf ("Case #%d:daizhenyang",++Tcase); R=read (); C=read (); memset (G,0,sizeofG);  for(intI=1; i<=r;i++) {scanf ("%s", s[i]+1); }         for(intI=1; i<=r;i++)          for(intj=1; j<=c;j++)          if(s[i][j]!='#'){            for(intk=0;k< -; k++){                intx=i+dx[k],y=j+Dy[k]; if(x>=1&&x<=r&&y>=1&&y<=c&&s[x][y]!='#') {g[(i-1) *c+j][(x1) *c+y]=1; g[(x-1) *c+y][(I-1) *c+j]=1; }           }           if(s[i][j]=='K') kx=i,ky=J; } N=r*b; intt1=solve (); intX= (kx-1) *c+KY;  for(intI=1; i<=n;i++)if(G[x][i]) g[x][i]=g[i][x]=0; if(Solve () ==t1) puts ("lose"); ElsePuts"win"); }    return 0;}

HDU 3446 Daizhenyang ' s chess

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.