非常有毛病的一道题,我一个一个读字符死活过不去,改成整行整行读就 A 了...

做法就是...最小点覆盖...

我们发现可以把一个点向上跳看做被吃掉了,然后最顶层的点是无法向上跳所以不能被吃掉,然后被吃掉的点相连的边都会被删除...

这样转换完模型之后特判两下用二分图匹配就好了(因为这里的环最多是四元,或者说是偶数长度环...)

注意顶部的点必须要特判...因为顶部的点无法删除...

//by Judge
#include<cstdio>
#include<cstring>
#include<iostream>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
using namespace std;
const int N=103;
const int M=2e4+3;
typedef int MP[N][N];
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){ int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} inline int cread(int* s){ Rg int len=0; Rg char ch;
while((ch=getchar())!='O'&&ch!='.');
for(s[++len]=ch=='O';(ch=getchar())=='O'||ch=='.';s[++len]=ch=='O');
return s[len+1]='\0',len;
} char sr[1<<21],z[20];int C=-1,Z;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(Rg int x,Rg char chr=' '){
if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]=chr;
} int n,cnt,tim,res,pat,head[M<<2];
arr px,py,to,vis,bl,now,ok; MP id,mp;
struct Edge{ int to,nxt; }e[M];
inline void add(int u,int v){
e[++pat]=(Edge){v,head[u]},head[u]=pat;
}
bool dfs(int u){ vis[u]=tim;
go(u) if(vis[v]^tim){ vis[v]=tim;
if(!to[v]||dfs(to[v])) return to[v]=u,1;
} return 0;
}
int main(){
fp(i,1,read()){ n=read(),cnt=pat=res=0;
fp(i,1,n){
cread(mp[i]);
fp(j,1,n) if(mp[i][j])
id[i][j]=++cnt,
px[cnt]=i,py[cnt]=j,
bl[cnt]=(i&1);
}
fp(i,1,n+1) mp[n+1][i]=0;
memset(ok,0,(cnt+2)<<2);
memset(to,0,(cnt+2)<<2);
memset(vis,0,(cnt+2)<<2);
memset(now,0,(cnt+2)<<2);
memset(head,0,(cnt+2)<<2);
fp(i,1,n) fp(j,1,n)
if(mp[i][j]&&!mp[i-1][j-1]&&!mp[i-1][j+1]){
now[id[i][j]]=1;
if(mp[i+1][j+1]) now[id[i+1][j+1]]=2;
if(mp[i+1][j-1]) now[id[i+1][j-1]]=2;
}
fp(i,1,cnt) res+=(now[i]==2);
for(Rg int i=1;i<=n;i+=2) fp(j,1,n)
if(mp[i][j]&&!now[id[i][j]]){
if(mp[i-1][j-1]&&!now[id[i-1][j-1]])
add(id[i][j],id[i-1][j-1]);
if(mp[i-1][j+1]&&!now[id[i-1][j+1]])
add(id[i][j],id[i-1][j+1]);
if(mp[i+1][j-1]&&!now[id[i+1][j-1]])
add(id[i][j],id[i+1][j-1]);
if(mp[i+1][j+1]&&!now[id[i+1][j+1]])
add(id[i][j],id[i+1][j+1]);
}
tim=0;
fp(i,1,cnt) if(!now[i])
if(++tim&&dfs(i)) ++res;
fp(i,1,cnt) if(to[i]) ok[to[i]]=1;
++tim;
fp(i,1,cnt) if(!now[i]&&bl[i]&&!ok[i]) dfs(i);
fp(i,1,cnt) if(!now[i]&&bl[i]&&vis[i]^tim) now[i]=2;
fp(i,1,cnt) if(!now[i]&&!bl[i]&&vis[i]==tim) now[i]=2;
print(res,'\n');
fd(i,cnt,1) if(now[i]&2){
Rg int x=px[i],y=py[i]; print(x),print(y);
sr[++C]=mp[x-1][y-1]?'L':'R',sr[++C]='\n';
}
} return Ot(),0;
} /*
2
7
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
OOOOOOO
3
.O.
O.O
... */

codechef: BINARY, Binary Movements的更多相关文章

  1. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  2. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  3. leetcode105:Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

  4. leetcode:Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. leetcode:Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1即反转二叉树,代码如下: /** * Defin ...

  6. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  7. [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...

  8. [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  9. [LeetCode]题解(python):099-Recover Binary Search Tree

    题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...

随机推荐

  1. 《C语言程序设计(第四版)》阅读心得(三)

    第八章  指针 1.一个变量的地址称为该变量的指针 一个专门用来存放另一变量的地址(即指针),称它为指针变量.指针变量的值是指针(即地址) 如上图2000是变量的指针,pointer是指针变量, 赋值 ...

  2. android开发里跳过的坑——button不响应点击事件

    昨天遇到一个头疼的问题,在手机上按钮事件都很正常,但是在平板上(横屏显示的状态),button点击事件不响应,代码简化如下: public class Test extends Activity im ...

  3. Google Protocol Buffer 的使用(二)

    一.protobuf应用场景 protobuf 在Java中的应用场景可以是序列化和反序列化,流可以通过文件或者通过网络TCP/UDP等方式传输.新建一个.proto文件 syntax = " ...

  4. 所有的异常都要使用try catch 语句捕获?

    在开发应用程序过程中必须检测代码可能发生的错误并进行正确的处理,这个在理想的情况下,应用程序中的每行 代码都按照预想的执行,要用到的每种资源总是可以利用,但是在实际的开发过程中,写代码难免会出错,或是 ...

  5. JSP中HTTP状态码

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/http-status-codes.html: HTTP请求格式和HTTP响应消息的格式一样,都有以下结构 ...

  6. iText输出中文的三种字体选择方式

    1.使用iTextAsian.jar中的字体    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",Bas ...

  7. sdfs

    <!DOCTYPE html><html><head><meta charset="GB18030"><title>In ...

  8. Socket编程中,阻塞与非阻塞的区别

    阻塞:一般的I/O操作可以在新建的流中运用.在服务器回应前它等待客户端发送一个空白的行.当会话结束时,服务器关闭流和客户端socket.如果在队列中没有请示将会出现什么情况呢?那个方法将会等待一个的到 ...

  9. 【转】Golang 关于通道 Chan 详解

    原文:http://blog.csdn.net/netdxy/article/details/54564436 在用 chan 类型时,发生死锁的错误,表面上看不出什么问题 ------------- ...

  10. 笔记本电脑 联想 Thinkpad E420 无法打开摄像头怎么办

    1 计算机管理-右击USB视频设备(应该显示为黄色问号,表示驱动安装不成功),点击浏览计算机以查找驱动程序软件 2 选择"从计算机的设备驱动程序列表中选择",然后选择Microso ...