[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 ...
随机推荐
- webpack 3.8 使用 extract-text-webpack-plugin 3.0 抽取css失败:You may need an appropriate loader to handle this file type.
webpack 3.8.1 使用 extract-text-webpack-plugin 3.0.2 抽取css时失败,报错: ERROR in ./src/static/style/localTim ...
- 在xml文件中引入带有@Configuration的Java类
在xml引入带有@Configuration的Java类,就是把这个带有@Configuration的Java类,当做一个普通的的类用<bean>标签引入: 核心代码如下: @Config ...
- hive subprocess failed with code X 的错误码对应信息
PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operati ...
- C#弱引用
加菲猫 Just have a little faith. C#弱引用 .NET框架提供了另一有趣的特色,被用于实现多样的高速缓存.在.NET中弱引用通过System.WeakReference类实现 ...
- 【BZOJ2338】【HNOI2011】数矩形 [计算几何]
数矩形 Time Limit: 20 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 最近某歌手在研究自己的全国巡回演出, ...
- Web Application Vulnerabilities and Potential Problem Due to Bad Design
web应用设计中不安全的设计及潜在的风险: REF: https://msdn.microsoft.com/en-us/library/ff648647.aspx
- 链接加载文件gcc __attribute__ section
在阅读源代码的过程中,发现一个头文件有引用: /** The address of the first device table entry. */ extern device_t devices[] ...
- Linux内核线程之深入浅出【转】
转自:http://blog.csdn.net/yiyeguzhou100/article/details/53126626 [-] 线程和进程的差别 线程的分类 1 内核线程 2 轻 ...
- linux coredump测试
1 )如何生成 coredump 文件 ? 登陆 LINUX 服务器,任意位置键入 echo "ulimit -c 1024" >> /etc/profile 退出 L ...
- 学习struts2
有部分内容转载牛人的博客: http://blog.csdn.net/hudie1234567/article/details/6730481 http://blog.csdn.net/lishuan ...