HDU 4499.Cannon Search

Source: Internet
Author: User

Cannon

Time limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 965 Accepted Submission (s): 556


Problem DescriptionIn Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other Chessman along the route Or perform an eat action. The Eat action, however, is the main concern in this problem.
An eat action, for example, Cannon A eating Chessman B, requires both conditions:
1, A and B are in either the same row or the same column in the chess grid.
2, there is exactly one chessman between A and B.
Here comes the problem.
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying That any of the cannons is not able to eat. It's worth nothing so we only have account the cannon pieces your put in the grid, and no, and no, pieces shares the same cell.

InputThere is multiple test cases.
In all test case, there is three positive integers N, M and Q (1<= N, m<=5, 0<=q <= n x M) in the first line , indicating the row number, column number of the grid, and the number of the existing chessmen.
In the second line, there is Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes is numbered from 0 to N-1, and column indexes is numbered from 0 to M-1. It guarantees no pieces share the same cell.

OutputThere is a one line for each test case, containing the maximum number of cannons.

Sample INPUT4 4 1 1 5 + 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0 Sample Output89Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4499
Test instructions: In the chess board of NXM, there is a chess piece with Q, they can not eat each other. Now to add a piece "Cannon" on the board, ask the maximum number of guns can be increased so that the cannon can not eat each other (1<= N, m<=5, 0<=q <= n x M). Cannon a The rule of eating chess B is that A and b have a pawn between a and B in one row or a column.
ideas: N and m very small, direct violence BFS search. Note that cannon can increase the rules. Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intn,m;intans=0;intedge[Ten][Ten];intDfsintXintYintcou) {    intI,j,t,sign,ok;/*For (i=0, i<n; i++) {for (j=0; j<m; j + +) cout<<edge[i][j]<< "";    cout<<endl; } cout<<endl;*/    if(Cou>ans) ans=cou; Edge[x][y]=2; /** The current row can be increased*/     for(j=y+1; j<n; J + +)    {        if(edge[x][j]==1)Continue; Sign=0; OK=1;  for(t=j-1; t>=0; t--)        {            if(edge[x][t]!=0) sign++; if(sign==2&&edge[x][t]==2) {OK=0;  Break; }        }        if(ok==1) { sign=0;  for(t=x-1; t>=0; t--)            {                if(edge[t][j]!=0) sign++; if(sign==2&&edge[t][j]==2) {OK=0;  Break; }            }            if(ok==1) {Edge[x][j]=2; DFS (X,j,cou+1); EDGE[X][J]=0; }        }    }    /** The current line cannot be added, add the following line*/     for(i=x+1; i<n; i++)    {         for(j=0; j<m; J + +)        {            if(edge[i][j]==1)Continue; Sign=0; OK=1;  for(t=i-1; t>=0; t--)            {                if(edge[t][j]!=0) sign++; if(sign==2&&edge[t][j]==2) {OK=0;  Break; }            }            if(ok==1) {Edge[i][j]=2; DFS (I,j,cou+1); EDGE[I][J]=0; }        }    }}intMain () {inti,j,q; intx, y;  while(SCANF ("%d%d%d", &n,&m,&q)! =EOF) {memset (Edge,0,sizeof(Edge));  while(q--) {scanf ("%d%d",&x,&y); Edge[x][y]=1; } ans=0;  for(i=0; i<n; i++)             for(j=0; j<m; J + +)                if(edge[i][j]==0) {Edge[i][j]=2; DFS (I,J,1); EDGE[I][J]=0; } cout<<ans<<Endl; }    return 0;}
View Code

HDU 4499.Cannon Search

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.