题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮)。
析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms。
代码如下:
#include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> using namespace std ; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f3f; const double eps = 1e-8; const int maxn = 1e4 + 5; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; int n, m; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int a[30]; int ans; bool isly(int x){ int b[] = { x+1, x-1, x+m, x-m}; int d[] = { x/m*m+m-1, x/m*m, n-1, 0}; int c[] = { 1, -1, m, -m}; for(int i = 0; i < 4; ++i){ bool ok = false; for(int j = b[i]; (i & 1 ? j >= d[i] : j <= d[i]); j += c[i]){ if(ok && a[j] != -1){ if(a[j] == 1) return false; else break; } else if(!ok && a[j] != -1) ok = true; } } return true; } void dfs(int x, int cnt){ ans = max(ans, cnt); for(int i = x; i < n; ++i){ if(a[i] != -1 || !isly(i)) continue; a[i] = 1; dfs(i+1, cnt+1); a[i] = -1; } } int main(){ int c; while(scanf("%d %d %d", &n, &m, &c) == 3){ n *= m; memset(a, -1, sizeof(a)); for(int i = 0; i < c; ++i){ int x, y; scanf("%d %d", &x, &y); a[x*m+y] = 0; } ans = 0; dfs(0, 0); printf("%d\n", ans); } return 0; }