Problem G
Rooks
Input: Standard Input
Output:Standard Output
Time Limit:8 Seconds
Given a chessboardNxN, on which the rooks are placed. You have tocolor those rooks in a minimal number of colors in that way – no horizontal and vertical line contains two rooks of the same color.
Input
First line of the input file contains an integerS(0<S<10) that indicates how many sets of inputs are there. The description of each set is given below:
The first line of each input set contains numberN (0≤N≤100).
The next N lines contain a chessboard (arrayNxN), where an empty cell is marked as ‘.’, and a cell that contains a rook is marked as ‘*’(there are not blanks between the symbols in a line).
Output
The description of output for each test case is given below:
The first line of the output for each test case contains numberM- the minimal number of colors. The next N lines contain a chessboard, where an empty cell is marked as ‘0’, and a cell that contains a rook is marked as ‘K’, whereK is a color of the rook. There can be more than correct solution any valid solution will be accepted.
Sample Input Output for Sample Input
2 2 *. ** 4 *.*. *.*. ***. ..** | 2 2 0 1 2 4 1 0 2 0 3 0 1 0 2 1 3 0 0 0 4 1
|
Problem source: Russiansummer training camp 2000.
Problem author: Maxim Babenko
Problem translation: Dmytro Chernysh
题意:给出一个方格图,里面有一些是“*”,你要为他们染色,使得每行每列不会有同样的颜色,并且颜色要最少。
思路:这题给跪了。。。建图的话是按行号和列号分别为两个集合,如果该行该列有“*”,就连一条线,我就只能想到这里了。。。。然后颜色的最少数目为这个图里面最大的度数。。。不会证明,不要问我。然后做法就是为这个二分图补充一些边使得每个点的度数等于最大度数。然后开始匹配,匹配一次,就把边给删掉。如果匹配出的行列有”*“,就标颜色到输出答案中,否则不用管。
代码:
#include<iostream>
#include<cstdio>
#include<ctime>
#include<string.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 105;
int n , rook;
int board[maxn][maxn];
char buffer[maxn][maxn];
int link[maxn];
bool T[maxn];
vector<int> G[maxn];
int deg[maxn];
void input()
{
memset(buffer,0,sizeof(buffer));
scanf("%d",&n);
rook = 0;
memset(deg,0,sizeof(deg));
for (int i = 0 ; i < n ; ++i)
{
G[i].clear();
scanf("%s",buffer[i]);
int cnt = 0;
for (int j = 0 ; j < n ; ++j) {
if (buffer[i][j]=='*') {
++cnt;
G[i].push_back(j);
++deg[j];
}
}
rook = max(rook,cnt);
}
for (int i = 0 ; i < n ; ++i)
{
int cnt = 0;
for (int j = n-1 ; j >= 0 ; --j)
if (buffer[j][i]=='*') ++cnt;
rook = max(rook,cnt);
}
for (int i = 0 ; i < n ; ++i) if (G[i].size()<rook) {
for (int j = 0 ; j < n && G[i].size()<rook ; ++j) if (deg[j]<rook) {
while (deg[j]<rook && G[i].size()<rook) {
G[i].push_back(j);
++deg[j];
}
}
}
}
void output()
{
for (int i = 0 ; i < n ; ++i)
{
printf("%d",board[i][0]);
for (int j = 1 ; j < n ; ++j) printf(" %d",board[i][j]);
printf("\n");
}
}
bool dfs(int x)
{
for (int i = 0 ; i < G[x].size() ; ++i) if (!T[G[x][i]])
{
int y = G[x][i];
T[y] = true;
if (link[y]==-1 || dfs(link[y]))
{
link[y] = x;
return true;
}
}
return false;
}
void maxmatch()
{
memset(link,-1,sizeof(link));
for (int i = 0 ; i < n ; ++i) {
memset(T,0,sizeof(T));
dfs(i);
}
}
void solve()
{
printf("%d\n",rook);
memset(board,0,sizeof(board));
int color = 0;
while (color < rook) {
++color;
maxmatch();
for (int i = 0 ; i < n ; ++i) {
int r = link[i] , c = i;
if (buffer[r][c]=='*') board[r][c] = color;
for (int j = 0 ; j < G[r].size() ; ++j) if (G[r][j]==c) {
G[r].erase(G[r].begin()+j);
break;
}
}
}
output();
}
int main()
{
int T; cin>>T;
while (T--) {
input();
solve();
}
}