HDU 4499 Cannon Violence DFS Search

Source: Internet
Author: User

CannonTime limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 589 Accepted Submission (s): 338

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 Input

Sample Output
8 9

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

Test instructions: give you a n*m chess board, there is a Q point has put the game. How many guns can you put up? There is no need to attack each other between cannon and cannon. That is, two guns can not be directly across the cannon or across chess.

Procedure: Checkerboard size maximum of 25 lattices. 2^25=3*10^7. If you enumerate each state, it is not feasible to time out. So I used Dfs. Every time that position if there is no chess, put on the gun Dfs down, each pendulum judgment and a row above the conflict does not conflict, there is no conflict left a row.   No conflict can be placed. Get rid of Dfs again. If there is no chess, DFS will not put the gun on the line.

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h># Include <string> #include <iostream> #include <algorithm>using namespace std; #include <stack># Include <queue> #include <vector> #include <deque> #include <set> #include <map>int n,m,q; int mp[5][5];int ass;int dfs (int nw,int fa) {if (nw==n*m) {return FA;} int X=nw/m;int y=nw%m;int maxx=fa;if (mp[x][y]!=1) {int flag=0;if (MP[1][0]==2&AMP;&AMP;MP[3][0]==2&AMP;&AMP;NW==20) int kk=1;for (int i=x-1;i>=0;i--) {if (flag==1&&mp[i][y]==2) flag=2;if (flag==1&&mp[i][y]==1) break ; if (flag==0&& (mp[i][y]==1| | mp[i][y]==2))//Cannon flag=1; }int flag2=0;for (int i=y-1;i>=0;i--) {if (flag2==1&&mp[x][i]==2) flag2=2;if (flag2==1&&mp[x][i]== 1) break;if (flag2==0&& (mp[x][i]==1| |  mp[x][i]==2))//Cannon flag2=1; }IF (flag!=2&&flag2!=2) {Mp[x][y]=2;maxx=max (Maxx,dfs (nw+1,fa+1)); mp[x][y]=0; Maxx=max (Maxx,dfs (NW+1,FA));} Elsemaxx=max (Maxx,dfs (NW+1,FA));} Elsemaxx=max (Maxx,dfs (NW+1,FA)); return Maxx;} int main () {while (scanf ("%d%d%d", &n,&m,&q)!=eof) {memset (mp,0,sizeof MP); ass=0;for (int i=0;i<q;i++) { int x,y;scanf ("%d%d", &x,&y); mp[x][y]=1; } printf ("%d\n", DFS (0,0));}  return 0;}

HDU 4499 Cannon Violence DFS 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.