[SRM570]TheTiles
题意:给一个$n\times m$的网格,对这个网格黑白染色,左上角为黑色。现在要用一些大小为$3$的L型图形覆盖这个网格,要求不能重复覆盖同一个格子,不能覆盖到障碍,L型可以进行旋转,但转角处格子必须为黑色,求最多能覆盖多少个L型图形
人生第一次上TC做题2333,最近机房网络不太稳定,lantern也经常崩,所以做这道题用了十几分钟才成功提交一次==
无论怎么旋转L型,两个白格必定不在同一行
所以$S$向奇数行白格连边,奇数行白格向相邻黑格连边,黑格向相邻偶数行白格连边,偶数行白格向$T$连边,这样跑最大流就对应着许多L型图形
为了保证每个格子最多被用一次,拆点连边限制流量即可
#include<stdio.h>
#include<string.h>
#include<vector>
#include<string>
using namespace std;
const int inf=2147483647;
int min(int a,int b){return a<b?a:b;}
int h[5010],cur[5010],nex[30010],to[30010],cap[30010],dis[5010],q[5010],M=1,S,T;
void add(int a,int b,int c){
M++;
to[M]=b;
cap[M]=c;
nex[M]=h[a];
h[a]=M;
M++;
to[M]=a;
cap[M]=0;
nex[M]=h[b];
h[b]=M;
}
bool bfs(){
int head,tail,x,i;
memset(dis,-1,sizeof(dis));
head=tail=1;
q[1]=S;
dis[S]=0;
while(head<=tail){
x=q[head];
head++;
for(i=h[x];i;i=nex[i]){
if(cap[i]&&dis[to[i]]==-1){
dis[to[i]]=dis[x]+1;
if(to[i]==T)return 1;
tail++;
q[tail]=to[i];
}
}
}
return 0;
}
int dfs(int x,int flow){
if(x==T)return flow;
int i,f;
for(i=cur[x];i;i=nex[i]){
if(cap[i]&&dis[to[i]]==dis[x]+1){
f=dfs(to[i],min(flow,cap[i]));
if(f){
cap[i]-=f;
cap[i^1]+=f;
if(cap[i])cur[x]=i;
return f;
}
}
}
dis[x]=-1;
return 0;
}
int dicnic(){
int ans=0,tmp;
while(bfs()){
memcpy(cur,h,sizeof(h));
while(1){
tmp=dfs(S,inf);
if(tmp==0)break;
ans+=tmp;
}
}
return ans;
}
const int gx[4]={1,-1,0,0},gy[4]={0,0,1,-1};
char s[50][50];
int n,m;
int p(int x,int y){return(x-1)*m+y;}
int in(int x,int y){return p(x,y)*2-1;}
int ou(int x,int y){return p(x,y)<<1;}
int col(int x,int y){
if(~(x+y)&1)return 2;
return(~x&1)*2+1;
}
bool ok(int x,int y){return 0<x&&x<=n&&0<y&&y<=m&&s[x][y]=='.';}
class TheTilesDivOne{
public:
int find(vector<string>board){
int i,j,k,x,y;
n=board.size();
m=board[0].length();
for(i=1;i<=n;i++)strcpy(s[i]+1,board[i-1].c_str());
S=n*m*2+1;
T=n*m*2+2;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(s[i][j]=='.'){
add(in(i,j),ou(i,j),1);
if(col(i,j)==1)add(S,in(i,j),1);
if(col(i,j)==3)
add(ou(i,j),T,1);
else{
for(k=0;k<4;k++){
x=i+gx[k];
y=j+gy[k];
if(ok(x,y)&&col(i,j)+1==col(x,y))add(ou(i,j),in(x,y),1);
}
}
}
}
}
return dicnic();
}
};
/*
vector<string>v;
TheTilesDivOne test;
char str[50];
int main(){
int n,m,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%s",str);
v.push_back(str);
}
printf("%d",test.find(v));
}
*/
辣鸡TC连warning都不给过,差评==
[SRM570]TheTiles的更多相关文章
- Topcoder SRM570 900 CurvyonRails
题意:给定一个网格,一些格子是障碍不用管,剩余的格子是城市,你可以修建铁路,铁路的形状可以是直的或者弯的,也就是说可以以这个点为节点连接它四联通的其中两个方块.要求用一个或多个环来覆盖所有城市.对于有 ...
- Topcoder SRM570 D1L3 CurvyonRails
几个样例: 5 5wCCwwwCC....w......www..wReturns: 0 3 3C.w....C.Returns: 1 21 20CC..CCCw.CwC..CC.w.CC.CCCwC ...
随机推荐
- Mockito中@Mock与@InjectMock
Mockito是java单元测试中,最常用的mck工具之一,提供了诸多打桩方法和注解.其中有两个比较常用的注解,@Mock和@InjectMock,名字和在代码中使用 的位置都很像,对于初学者,很容易 ...
- PowerShell官方文档
PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...
- html中音频和视频
HTML5音频中的新元素标签 src:音频文件路径. autobuffer:设置是否在页面加载时自动缓冲音频. autoplay:设置音频是否自动播放. loop:设置音频是否要循环播放. contr ...
- openlayers3中应用proj4js
要在openlayers3中应用proj4js,需要在html中引用proj4js,然后在引用所需要的projection的js定义,如 http://epsg.io/21781-1753.js 然后 ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- C# 文件类的操作---获取
如何获取指定目录包含的文件和子目录 . DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找: . DirectoryIn ...
- wx.ScrolledWindow wx.PseudoDC
# encoding: utf-8 import logging import random import wx import wx.lib.inspection def GetMyBitmap(): ...
- USB接口无法识别设备
http://windows.microsoft.com/zh-cn/windows/answers?tId=14fa1e44-0a19-48ef-9ba7-b7e512a837a4 小琼子 提问 2 ...
- C++高精度
整理了一下高精度,虽然可用java,但很多时候还是C++写的方便. 附上kuangbin神的高精度模板(HDU1134 求卡特兰数) #include <iostream> #includ ...
- GenericAPIView类与几个扩展类的综合使用
五个扩展类 扩展类 作用 封装的方法 状态码(成功,失败) ListModelMixin 查询多条数据 list 200 CreateModelMixin 新增一条数据 create 201,400 ...