Reprinted please indicate the source: http://blog.csdn.net/u012860063
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4499
Cannon
Time Limit: 2000/1000 MS (Java/others) memory limit: 65535/65535 K (Java/Others)
Total submission (s): 363 accepted submission (s): 214
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 two conditions:
1. A and B is 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 two cannons are not able to eat each other. it is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell. inputthere are multiple test cases.
In each test case, there are 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 are Q pairs of integers. each pair of integers x, y indicates the row index and the column index of the piece. row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. it guarantees no pieces share the same cell.
Outputthere is only one line for each test case, containing the maximum number of cannons.
Sample Input
4 4 2 1 1 1 2 5 5 8 0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
Sample output
8 9
Source2013 ACM-ICPC Jilin Tonghua national invitational Competition -- reproduction of questions
/* Question:
I will give you a chessboard with a maximum of 5x5. I will ask you how many guns you can put at most, and the guns and guns cannot attack each other,
This piece can only take one step. There are no three pieces in the middle of two guns ..
*/
Code + explanation:
# Include <cstdio> # include <cstring> int n, m, ans; int G [7] [7]; int max (int A, int B) {If (A> B) return a; return B;} void DFS (INT X, int y, int CNT) {If (x> = N) // indicates that the search has been completed {ans = max (ANS, CNT); return;} If (Y> = m) // list the bounds, indicates that the current row has been searched {DFS (x +, CNT); // return starts from 0 in the next row again ;} if (G [x] [Y] = 1) // if the current position already has pawns {DFS (X, Y + 1, CNT ); // search return again from the next one;} DFS (X, Y + 1, CNT); int T, flag = 0; For (t = X-1; t> = 0; t --) // The two for statements below are used to check whether the same column exists {// There are guns and gunshots if (G [T] [Y]) {break ;}}for (INT I = T-1; I> = 0; I --) {If (G [I] [Y]) {If (G [I] [Y] = 2) {flag = 1 ;}break ;}} if (FLAG) {return; // if the above state exists, return the previous layer} For (t = Y-1; t> = 0; t --) // The following two for statements are used to check whether the same row exists. {// The Front has a gun and a cannon frame. If (G [x] [T]) break ;} for (Int J = T-1; j> = 0; j --) {If (G [x] [J]) {If (G [x] [J] = 2) {flag = 1;} break;} If (FLAG) {return; // if the preceding situation exists, return to the previous layer} G [x] [Y] = 2; // indicates that a gun DFS (X, Y + 1, CNT + 1) is put here ); G [x] [Y] = 0; // backtracking} int main () {in T q, U, V, I; while (~ Scanf ("% d", & N, & M, & Q) {memset (G, 0, sizeof (g); for (I = 0; I <q; I ++) {scanf ("% d", & U, & V); G [u] [v] = 1; // indicates that the pawns} ans = 0; DFS (0, 0, 0); printf ("% d \ n", ANS);} return 0 ;}