# Antenna Placement

## POJ 刷题系列：3020. Antenna Placement

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

String INPUT = "./data/judge/201801/P3020.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

boolean[][] map;
int[] match;

boolean dfs(int v, boolean[] vis, int num) {
vis[v] = true;
for (int to = 0; to < num; ++to) {
if (map[v][to]) {
int w = match[to];
if (w == -1 || !vis[w] && dfs(w, vis, num)) {
match[v] = to;
match[to] = v;
return true;
}
}
}
return false;
}

int bipatite_match(int V) {
match = new int[V];
Arrays.fill(match, -1);
int res = 0;
for (int i = 0; i < V; ++i) {
if (match[i] < 0) {
if (dfs(i, new boolean[V], V))
res ++;
}
}
return res;
}

int T = ni();
while (T --> 0) {
int n = ni();
int m = ni();
char[][] board = new char[n][m];
List<int[]> star = new ArrayList<int[]>();
for (int i = 0; i < n; ++i) {
String ns = ns();
for (int j = 0; j < m; ++j) {
board[i][j] = ns.charAt(j);
if (board[i][j] == '*') star.add(new int[] {i, j});
}
}

int tot = star.size();

// build map
map = new boolean[n * m][n * m];

boolean[][] vis = new boolean[n][m];
int[][] dir = {{1, 0},{-1, 0},{0, 1},{0, -1}};
for (int[] now : star) {
int i = now[0];
int j = now[1];
if (!vis[i][j]) {
for (int[] d : dir) {
int ni = i + d[0];
int nj = j + d[1];
if (ni >= 0 && ni < n && nj >= 0 && nj < m && board[ni][nj] == '*' && !vis[ni][nj]) {
map[ni * m + nj][i * m + j] = true;
map[i * m + j][ni * m + nj] = true;
vis[i][j] = true;
}
}
}
}
out.println(tot - (bipatite_match(n * m)));
}
}

FastScanner in;
PrintWriter out;

void run() throws IOException {
boolean oj;
try {
oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");
} catch (Exception e) {
oj = System.getProperty("ONLINE_JUDGE") != null;
}

InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
in = new FastScanner(is);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
out.flush();
if (!oj){
System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
}
}

public boolean more(){
return in.hasNext();
}

public int ni(){
return in.nextInt();
}

public long nl(){
return in.nextLong();
}

public double nd(){
return in.nextDouble();
}

public String ns(){
return in.nextString();
}

public char nc(){
return in.nextChar();
}

class FastScanner {
StringTokenizer st;
boolean hasNext;

public FastScanner(InputStream is) throws IOException {
hasNext = true;
}

public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} catch (Exception e) {
hasNext = false;
return "##";
}
}
return st.nextToken();
}

String next = null;
public boolean hasNext(){
next = nextToken();
return hasNext;
}

public int nextInt() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Integer.parseInt(more);
}

public long nextLong() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Long.parseLong(more);
}

public double nextDouble() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Double.parseDouble(more);
}

public String nextString(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more;
}

public char nextChar(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more.charAt(0);
}
}

static class D{

public static void pp(int[][] board, int row, int col) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
}
}
System.out.println(sb.toString());
}

public static void pp(char[][] board, int row, int col) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
}
}
System.out.println(sb.toString());
}
}

static class ArrayUtils {

public static void fill(int[][] f, int value) {
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], value);
}
}

public static void fill(int[][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}

public static void fill(int[][][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}
}

static class Num{
public static <K> void inc(Map<K, Integer> mem, K k) {
if (!mem.containsKey(k)) mem.put(k, 0);
mem.put(k, mem.get(k) + 1);
}
}
}```

0 条评论

• ### POJ 刷题系列：1789. Truck History

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### POJ 刷题系列：2002. Squares

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### 1062. 昂贵的聘礼

思路： 实际上是一个图模型，采用递归，物品之间的交换有一条关系边，比如国王需要物品2，那么可以用8000来交换，此时子问题就变成了从物品2出发，所需要的最少...

• ### 挑战程序竞赛系列（82）：4.3 LCA（2）

挑战程序竞赛系列（82）：4.3 LCA（2） 传送门：POJ 1986: Distance Queries 题意： LCA距离：快速查询树中任意两个节点间...

• ### 挑战程序竞赛系列（59）：4.6树上的分治法（2）

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### POJ 刷题系列：1789. Truck History

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### POJ 刷题系列：2002. Squares

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.n...

• ### 4.7后缀数组

第一次接触后缀数组，采用《挑战》P378的后缀算法，时间复杂度为O(nlog2n)O(n\log^2n)，基本思想如下：

• ### 1062. 昂贵的聘礼

思路： 实际上是一个图模型，采用递归，物品之间的交换有一条关系边，比如国王需要物品2，那么可以用8000来交换，此时子问题就变成了从物品2出发，所需要的最少...

• ### 你听过算法也是可以贪心的吗？

贪心算法（又称贪婪算法）是指，在对问题求解时，总是做出在当前看来是最好的选择。也就是说，不从整体最优上加以考虑，他所做出的是在某种意义上的局部最优解。 贪心算法...