lightoj 1171 - Knights in Chessboard (II) 【奇偶图 最小割】
来源:互联网 发布:网站搜索引擎优化案例 编辑:程序博客网 时间:2024/05/21 10:53
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
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.
Output
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+10using 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;}
- lightoj 1171 - Knights in Chessboard (II) 【奇偶图 最小割】
- lightoj-1010-Knights in Chessboard
- LightOJ 1010 Knights in Chessboard
- lightoj 1010 - Knights in Chessboard 【数学思维】
- LightOJ 1010 Knights in Chessboard <贪心思维>
- lightoj 1010-Knights in Chessboard (规律)
- LightOJ-1010-Knights in Chessboard [规律]
- LightOJ 1010 Knights in Chessboard(数学规律)
- lightoj 1010 - Knights in Chessboard (找规律思维)
- lightoj 1010 - Knights in Chessboard(找规律)
- Knights in Chessboard
- LightOJ1010---Knights in Chessboard (规律题)
- Light oj 1010 - Knights in Chessboard
- light1010 - Knights in Chessboard【找规律】
- 1010 - Knights in Chessboard(找规律)
- Light OJ 1010 - Knights in Chessboard【思维】
- LIGHT OJ 1010 Knights in Chessboard
- lightoj1010 Knights in Chessboard(找规律)
- Struts2通过json前后自动给后台自定义对象赋值
- SQL 语法
- 网站架构-前端优化
- PHP PDO基础
- Oracle数据库基础(四)--视图,序列,存储过程
- lightoj 1171 - Knights in Chessboard (II) 【奇偶图 最小割】
- 14 Longest Collatz sequence - Project Euler
- 关于AQS的学习(一)
- 安全参透之旅第2章 httpracks工具使用
- 正则表达式——编译标志
- 用Javascript进行简单的Table点击排序.
- subsets-ii
- Appium总结(二):Appium Ruby Console
- 数据库