Hoj 13028 Attacking rooks

http://acm.hnu.cn/online/?action=problem&type=show&id=13028&courseid=0

题意:国际象棋里rooks里的规则(跟象棋没什么区别吧……)。在N*N的棋盘里放置几个‘X‘,如果两个rook之间有‘X‘就不会互相攻击,问棋盘里最多能放置几个rook。

题解:标准的二分图匹配。行列之间建边。根据‘X‘建立新行和新列。

  1 #include <climits>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cctype>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <cstdlib>
  8 #include <cstdarg>
  9 #include <iostream>
 10 #include <fstream>
 11 #include <iomanip>
 12 #include <sstream>
 13 #include <exception>
 14 #include <stdexcept>
 15 #include <memory>
 16 #include <locale>
 17 #include <bitset>
 18 #include <deque>
 19 #include <list>
 20 #include <map>
 21 #include <set>
 22 #include <queue>
 23 #include <stack>
 24 #include <vector>
 25 #include <algorithm>
 26 #include <iterator>
 27 #include <functional>
 28 #include <string>
 29 #include <complex>
 30 #include <valarray>
 31
 32 using namespace std;
 33
 34 const int maxn=110;
 35 int N;
 36 char G[maxn][maxn];
 37 int row[maxn][maxn];
 38 int col[maxn][maxn];
 39 int V;
 40 vector<int> Gra[maxn*maxn];
 41 int match[maxn*maxn];
 42 bool used[maxn*maxn];
 43
 44 void add_edge(int u,int v){
 45     Gra[u].push_back(v);
 46     Gra[v].push_back(u);
 47 }
 48
 49 bool dfs(int v)
 50 {
 51     used[v]=true;
 52     for(int i=0; i<Gra[v].size(); i++) {
 53         int u=Gra[v][i];
 54         int w=match[u];
 55         if (w<0||(!used[w]&&dfs(w))) {
 56             match[v]=u;
 57             match[u]=v;
 58             return true;
 59         }
 60     }
 61     return false;
 62 }
 63
 64 void b_match()
 65 {
 66     int res=0;
 67     memset(match, -1, sizeof(match));
 68     for (int v=0; v<V; v++) {
 69         if (match[v]<0) {
 70             memset(used, 0, sizeof(used));
 71             if (dfs(v)) {
 72                 res++;
 73             }
 74         }
 75     }
 76     printf("%d\n",res);
 77 }
 78
 79 void build2() {
 80     int row_cnt = 0;
 81     int col_cnt = 0;
 82
 83     for (int i = 0; i < N; i++)
 84         for (int j = 0; j < N; j++)
 85             row[i][j] = col[i][j] = 0;
 86
 87     for (int i = 0; i < N; i++) {
 88         row_cnt++;
 89         for (int j = 0; j < N; j++) {
 90             if (‘X‘==G[i][j]) {
 91                 bool flag =  j==0;
 92                 while (j+1<N && ‘X‘==G[i][j+1]) j++;
 93                 if (j+1==N) flag = true;
 94 //                if (!flag)
 95                     row_cnt++;
 96             } else row[i][j] = row_cnt;
 97         }
 98     }
 99     for (int j = 0; j < N; j++) {
100         col_cnt++;
101         for (int i = 0; i < N; i++) {
102             if (‘X‘==G[i][j]) {
103                 bool flag = i==0;
104                 while (i+1<N && ‘X‘==G[i+1][j]) i++;
105                 if (i+i==N) flag = true;
106 //                if (!flag)
107                     col_cnt++;
108             } else col[i][j] = col_cnt;
109         }
110     }
111     V=row_cnt+1;
112 //printf("V: %d\n",V);
113     for (int i = 0; i <= V; i++) Gra[i].clear();
114
115     for(int i=0;i<N;i++)
116     {
117         for(int j=0;j<N;j++)
118         {
119             if(G[i][j]==‘.‘) {
120                 add_edge(row[i][j],row_cnt+col[i][j]);
121 //printf("row %d col %d\n",row[i][j],col[i][j]);
122             }
123         }
124     }
125 //for(int i=0;i<V;i++)
126 //{
127 //    printf("%d (%d): ", i, Gra[i].size());
128 //    for(int j=0;j<Gra[i].size();j++)
129 //    {
130 //        printf("%d ",Gra[i][j]);
131 //    }
132 //    puts("");
133 //}
134 //    for (int i = 0; i < N; i++) {
135 //        for (int j = 0; j < N; j++)
136 //            printf("%d ", row[i][j]);
137 //        puts("");
138 //    }
139 //    puts("---------------------");
140 //        for (int i = 0; i < N; i++) {
141 //        for (int j = 0; j < N; j++)
142 //            printf("%d ", col[i][j]);
143 //        puts("");
144 //    }
145
146 }
147
148 int main()
149 {
150 #ifdef PIT
151     freopen("A.in", "r", stdin);
152 #endif // PIT
153     while(scanf("%d",&N)!=EOF)
154     {
155         int cntdian=0;
156         for(int i=0;i<N;i++)
157         {
158             scanf("%s",G[i]);
159 for(int j=0;j<N;j++)
160 {
161     if(G[i][j]==‘.‘) cntdian++;
162 }
163 //printf("%d\n",cntdian);
164 //            Gra[i].clear();
165         }
166         build2();
167         //build_graph();
168         b_match();
169     }
170     return 0;
171 }
时间: 2024-10-18 05:47:28

Hoj 13028 Attacking rooks的相关文章

UVALive 6525 Attacking rooks(二分图最大匹配)

Attacking rooks 在一个n*n的图中,'X'代表卒,在'.'的地方放置尽量多的车,使得它们不互相攻击.问最多可放置车的数目. 和Fire Net一样,但这里图是100*100的,搜索会超时(其实我还脑残的试了试). 正解是二分图匹配,将每行中连续为.的作为X集合中一个点,同样,将每列中连续为.的点作为Y集合中的一个点.对原图中每个'.',将其对应的X集合和Y集合中的标号建边,便形成了二分图,对该图求最大匹配,每形成一个匹配即选择该点放车,那么与它相同编号的行和列都被覆盖了,都不能被

UVALive 6525 Attacking rooks

将行中连续的' . '当作X集的一个点,列中连续的' . '看成Y集中的一个点,然后把每一个' . '看成一条边,连接Xi,Yj. 则问题转化成求此二分的最大匹配数.每找到一条匹配边的意义是找到了一个点放置一个,并且覆盖了所在的连续的行和列. 所以答案即为此二分图的最大匹配. 20s的时限,只跑了29ms....这是有多不相信自己的服务器.... #include <iostream> #include <algorithm> #include <cstdlib> #i

UVALive 6525 Attacking rooks 二分匹配 经典题

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: 给定n*n的棋盘, 能够在'.'上摆 象棋中的车(X是墙壁) 使得随意两个车都不能互相攻击到 问:最多能摆多少个车. 思路: 二分匹配 1.若没有X.那么做法就是 X点集为行,Y点集为列,对于图上的每一个点所在的行和列(x,y) 建一条边 x->y 2.有了X,那么对于每一个点所在的上方能接触到的X必须

LA 6525 Attacking rooks 二分匹配

题意: 给定n*n的棋盘, 可以在'.'上摆 象棋中的车(X是墙壁) 使得任意两个车都不能互相攻击到 求最多能摆多少个车. 思路:将每行中连续为.的作为X集合中一个点,同样,将每列中连续为.的点作为Y集合中的一个点.对原图中每个'.',将其对应的X 集合和Y集合中的标号建边,便形成了二分图,对该图求最大匹配.详见代码: /********************************************************* file name: LA6525.cpp author :

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)

Attacking rooks Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB Total submit users: 12, Accepted users: 7 Problem 13028 : No special judgement Problem description Chess inspired problems are a common source of exercises in algor

1005 - Rooks(规律)

1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current positio

Lightoj 1005 Rooks(DP)

A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vertically or horizontally from its current position and two rooks attack each other if one is on the path of the other. In the following fig

(light OJ 1005) Rooks dp

http://www.lightoj.com/volume_showproblem.php?problem=1005    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move vert

SGU[222] Little Rooks

Description 描述 Inspired by a "Little Bishops" problem, Petya now wants to solve problem for rooks. A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move horizontally and vertically from its