HDU 4499 cannon (Brute Force Search)

Source: Internet
Author: User


Question: There are t chess pieces in the square of N * M. Ask how many guns can be placed at most, and each gun cannot attack each other (the gun can be fired)


Gun: There is a pawn in the middle of the same row or column.



# Include <stdio. h> # include <iostream> # include <algorithm> # include <string. h> # include <queue> # include <math. h> # define M 50 # define ll long using namespace STD; int n, m, T, ans; int ma [m] [m]; bool BFS (int x, int y) // judge whether x, y can put {int xx = X-1; int CNT = 0; while (XX> = 0) {If (MA [XX] [Y] = 1 & CNT = 1) return false; if (MA [XX] [Y] = 1 | ma [XX] [Y]) CNT ++; xx --;} xx = x + 1; CNT = 0; while (XX <n) {If (MA [XX] [Y] = 1 & CNT = 1) return false; If (MA [XX] [Y] = 1 | ma [XX] [Y]) CNT ++; XX ++;} int YY = Y-1; CNT = 0; while (yy> = 0) {If (MA [x] [YY] = 1 & CNT = 1) return false; if (MA [x] [YY] = 1 | ma [x] [YY]) CNT ++; YY --;} YY = Y + 1; CNT = 0; while (yy <m) {If (MA [x] [YY] = 1 & CNT = 1) return false; if (MA [x] [YY] = 1 | ma [x] [YY]) CNT ++; YY ++;} return true ;} void DFS (int x, int y, int TMP) {If (TMP> ans) ans = TMP; For (INT I = x; I <n; I ++) {for (Int J = 0; j <m; j ++) {if (I = x & J <Y) contin UE; If (MA [I] [J]! =-1 & BFS (I, j) {ma [I] [J] = 1; DFS (I, j + 1, TMP + 1 ); ma [I] [J] = 0 ;}}} int main () {While (~ Scanf ("% d", & N, & M, & T) {memset (MA, 0, sizeof (MA); int X, Y; while (t --) {scanf ("% d", & X, & Y); MA [x] [Y] =-1 ;}ans = 0; DFS (0, 0); printf ("% d \ n", ANS );}}


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.