"Recursive" conflicts

Source: Internet
Author: User

"Recursive" conflicts The title describes each cell in the prison as a square with no more than a 4x4, with some obstacles in it, and a large number of prisoners who live in the cell, as long as two inmates are in the same row or column and conflict occurs, but obstacles can block clashes between peers or inmates of the same column. Ask for a maximum of several inmates without conflict. As shown, the left side represents the initial cell sample, the right 4 shows the placement scheme, of course, the last two options are wrong.
Input has multiple sets of test data, each set of data first behavior an integer n indicates the cell size. Then n lines describe the cell, where x represents the barrier.

All test data ends with a flag of 0. The maximum number of prisoners to be placed in the output output. Sample input
4.X ... Xx...... 2XX. X3. X.x.x.x.3 .... Xx. XX4 .......... 0
Sample output
51524

Analysis: Because the data range is very small, the direct violence searches can;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<ext/rope>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=Ten;Const intdis[][2]={0,1,-1,0,0,-1,1,0};using namespacestd;using namespace__gnu_cxx;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,m,ma;CharA[MAXN][MAXN];voidDfsintPintCNT) {    if(p==n*n) {Ma=max (ma,cnt);return;} intx=p/n,y=p%N; DFS (P+1, CNT); if(a[x][y]=='.')    {        intok=1;  for(inti=x;i<=n-1; i++) {if(a[i][y]=='X') Break;if(a[i][y]=='Q') ok=0;}  for(inti=x;i>=0; i--) {if(a[i][y]=='X') Break;if(a[i][y]=='Q') ok=0;}  for(inti=y;i<=n-1; i++) {if(a[x][i]=='X') Break;if(a[x][i]=='Q') ok=0;}  for(inti=y;i>=0; i--) {if(a[x][i]=='X') Break;if(a[x][i]=='Q') ok=0;} if(OK) a[x][y]='Q', DFS (p+1, cnt+1), a[x][y]='.'; }}intMain () {inti,j,k,t;  while(~SCANF ("%d", &n) &&N) {ma=0; Rep (I,0, N-1) scanf ("%s", A[i]); DFS (0,0); printf ("%d\n", MA); }    //System ("pause");    return 0;}

"Recursive" conflicts

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.