PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 32 MB |
Given an m x n chessboard where some of the cells are broken. Now you are about to place chess knights in the chessboard. You have to find the maximum number of knights that can be placed in the chessboard such that no two knights attack each other. You can't place knights in the broken cells.
Those who are not familiar with chess knights, note that a chess knight can attack eight positions in the board as shown in the picture below.
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case starts with a blank line. The next line contains three integers m, n, K (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively. Each of the next K lines will contain two integers x, y (1 ≤ x ≤ m, 1 ≤ y ≤ n) denoting that the cell(x, y)is broken already. No broken cell will be reported more than once.
For each case of input, print the case number and the maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input |
Output for Sample Input |
2
8 8 0
2 5 4 1 3 1 4 2 3 2 4 |
Case 1: 32 Case 2: 6 |
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define INF 0x3f3f3f3f #define MAXN 40000+10 #define MAXM 1000000+10 using namespace std; struct Edge{ int from, to, cap, flow, next; }; Edge edge[MAXM]; int head[MAXN], edgenum; int dist[MAXN], cur[MAXN]; bool vis[MAXN]; void init(){ edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E1 = {u, v, w, 0, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } int S, T; int Map[201][201]; int num[201][201]; int n, m, k; bool judge(int x, int y){ return x >= 1 && x <= n && y >= 1 && y <= m; } void getMap() { scanf("%d%d%d", &n, &m, &k); memset(Map, 0, sizeof(Map)); for(int i = 0; i < k; i++) { int a, b; scanf("%d%d", &a, &b); Map[a][b] = 1; } init(); int Move[8][2] = {-2,-1, -2,1, -1,-2, 1,-2, -1,2, 1,2, 2,-1, 2,1}; int oddnum = 0, evennum = 0; memset(num, 0, sizeof(num)); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(Map[i][j] == 0 && (i + j) & 1) num[i][j] = ++oddnum; else if(Map[i][j] == 0 && (i + j) % 2 == 0) num[i][j] = ++evennum; } } S = 0, T = oddnum + evennum + 1; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if((i + j) & 1 && Map[i][j] == 0) { addEdge(S, num[i][j], 1); for(int p = 0; p < 8; p++) { int x = i + Move[p][0]; int y = j + Move[p][1]; if(judge(x, y) && (x+y) % 2 == 0 && Map[x][y] == 0) addEdge(num[i][j], num[x][y] + oddnum, INF); } } else if((i + j) % 2 == 0 && Map[i][j] == 0) addEdge(num[i][j] + oddnum, T, 1); } } } bool BFS(int s, int t) { queue<int> Q; memset(dist, -1, sizeof(dist)); memset(vis, false, sizeof(vis)); Q.push(s); dist[s] = 0; vis[s] = true; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { dist[E.to] = dist[u] + 1; if(E.to == t) return true; vis[E.to] = true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int t) { if(x == t || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next) { Edge &E = edge[i]; if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0) { edge[i].flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int s, int t) { int flow = 0; while(BFS(s, t)) { memcpy(cur, head, sizeof(head)); flow += DFS(s, INF, t); } return flow; } int kcase = 1; void solve() { getMap(); printf("Case %d: %d\n", kcase++, n*m - k - Maxflow(S, T)); } int main() { int t; scanf("%d", &t); while(t--){ solve(); } return 0; }