链接:http://codeforces.com/contest/445/problem/A

描述:一个n*m的棋盘,有一些格子不能放棋子。现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同。输出一种可行解。

思路:脑筋急转弯。。。

下过国际象棋的都知道,棋盘本身可以染色成为黑白相间的格子,毁掉其中的格子后也不会影响其2-SAT的性质。直接输出再加一个判断当前格子是否能放棋子就可以了。

我的实现:

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define MaxN 120
6 char str[MaxN][MaxN];
7 int n,m;
8 int main()
9 {
10 int i,j,cur;
11 scanf("%d%d",&n,&m);
12 for(i=1;i<=n;++i)
13 scanf("%s",str[i]+1);
14 cur=0;
15 for(i=1;i<=n;++i)
16 {
17 for(j=1;j<=m;++j)
18 {
19 cur=(cur+1)%2;
20 if(str[i][j]=='-')
21 printf("-");
22 else if(cur)
23 printf("B");
24 else
25 printf("W");
26 }
27 if(m%2==0)
28 cur=(cur+1)%2;
29 printf("\n");
30 }
31 return 0;
32 }

效率:

然后要说的是,zyy太蠢了,一看完题目没多想就觉得是经典的2-SAT问题,然后就写啊写,结果WA了。。。。zyy觉得最多是个时间超限或者内存超限,怎么可能会WA掉!!!容后查错吧,先挖坑~~

我的实现(经典2-SAT写法,WA):

  1 #include <iostream>
2 #include <cstdio>
3 using namespace std;
4 #define MaxN 20020
5 #define MaxM 160020
6 #define Maxn 120
7 int n,m,N;
8 struct node
9 {
10 int v;
11 node *next;
12 };
13 node edge[MaxM];
14 node *cnt=&edge[0];
15 node *adj[MaxN];
16 node edge2[MaxM];
17 node *cnt2=&edge2[0];
18 node *adj2[MaxN];
19 int dfn[MaxN],low[MaxN],dcnt;
20 int stack[MaxN],top;
21 int Belong[MaxN],Num[MaxN],opp[MaxN],scc;
22 int In[MaxN],q[MaxN],col[MaxN];
23 bool Instack[MaxN],ans[MaxN];
24 char str[Maxn][Maxn];
25 inline void Addedge(int u,int v)
26 {
27 node *p=++cnt;
28 p->v=v;
29 p->next=adj[u];
30 adj[u]=p;
31 }
32 inline void Addedge2(int u,int v)
33 {
34 node *p=++cnt2;
35 p->v=v;
36 p->next=adj2[u];
37 adj2[u]=p;
38 }
39 void Read()
40 {
41 scanf("%d%d",&n,&m);
42 N=n*m;
43 int i,j;
44 for(i=1;i<=n;++i)
45 {
46 scanf("%s",str[i]+1);
47 for(j=1;j<=m;++j)
48 if(str[i][j]=='.')
49 {
50 if(i>1&&str[i-1][j]=='.')
51 {
52 Addedge((i-1)*m+j,(i-2)*m+j+N);
53 Addedge((i-1)*m+j+N,(i-2)*m+j);
54 }
55 if(j>1&&str[i][j-1]=='.')
56 {
57 Addedge((i-1)*m+j,(i-1)*m+j-1+N);
58 Addedge((i-1)*m+j+N,(i-1)*m+j-1);
59 }
60 }
61 }
62 }
63 void Tarjan(int u)
64 {
65 int v;
66 dfn[u]=low[u]=++dcnt;
67 stack[++top]=u;
68 Instack[u]=true;
69 for(node *p=adj[u];p;p=p->next)
70 {
71 v=p->v;
72 if(!dfn[v])
73 {
74 Tarjan(v);
75 low[u]=min(low[u],low[v]);
76 }
77 else if(Instack[v])
78 low[u]=min(low[u],dfn[v]);
79 }
80 if(dfn[u]==low[u])
81 {
82 scc++;
83 do
84 {
85 v=stack[top];
86 top--;
87 Instack[v]=false;
88 Belong[v]=scc;
89 Num[scc]++;
90 }while(v!=u);
91 }
92 }
93 bool Work()
94 {
95 int i;
96 for(i=1;i<=N*2;i++)
97 if(!dfn[i])
98 Tarjan(i);
99 for(i=1;i<=N;++i)
100 {
101 if(Belong[i]==Belong[i+N])
102 return false;
103 opp[Belong[i]]=Belong[i+N];
104 opp[Belong[i+N]]=Belong[i];
105 }
106 int u,v;
107 for(i=1;i<=N*2;i++)
108 for(node *p=adj[i];p;p=p->next)
109 {
110 v=p->v;
111 if(Belong[i]!=Belong[v])
112 {
113 Addedge2(Belong[v],Belong[i]);
114 In[Belong[i]]++;
115 }
116 }
117 int l=0,r=0;
118 for(i=1;i<=scc;i++)
119 if(!In[i])
120 {
121 q[r]=i;
122 r++;
123 }
124 while(l<r)
125 {
126 u=q[l];
127 l++;
128 if(!col[u])
129 {
130 col[u]=1;
131 col[opp[u]]=-1;
132 }
133 for(node *p=adj2[u];p;p=p->next)
134 {
135 v=p->v;
136 In[v]--;
137 if(!In[v])
138 {
139 q[r]=v;
140 r++;
141 }
142 }
143 }
144 for(i=1;i<=N*2;++i)
145 if(col[Belong[i]]==1)
146 ans[i]=true;
147 return true;
148 }
149 void Print()
150 {
151 if(Work())
152 {
153 int i,j;
154 for(i=1;i<=n;++i)
155 {
156 for(j=1;j<=m;++j)
157 {
158 if(str[i][j]=='-')
159 printf("-");
160 else if(ans[(i-1)*m+j])
161 printf("B");
162 else
163 printf("W");
164 }
165 printf("\n");
166 }
167 }
168 else
169 printf("NIE\n");
170 }
171 int main()
172 {
173 Read();
174 Print();
175 return 0;
176 }

欢迎大神指点!!

[题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard的更多相关文章

  1. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  2. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

  3. Codeforces Round #254 (Div. 2) A DZY Loves Chessboard

    先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...

  4. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  5. Codeforces Round #254 (Div. 1) D - DZY Loves Strings

    D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...

  6. Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力

    D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...

  7. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

  8. Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题

    A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...

  9. Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. Java集合-ArrayList源码分析

    目录 1.结构特性 2.构造函数 3.成员变量 4.常用的成员方法 5.底层数组扩容原理 6.序列化原理 7.集合元素排序 8.迭代器的实现 9.总结 1.结构特性 Java ArrayList类使用 ...

  2. 学习鸟哥linux私房菜--安装中文输入法fcitx

    首先需要卸载前面安装的scim,查阅命令,参考网址如下 http://www.cnblogs.com/propheteia/archive/2012/06/26/2563383.html 链接中博主采 ...

  3. 这个命令行HTTP客户端工具真不错

    程序员专属微信红包封面1000个,兑换码:dWK7fUs2WQG cURL相信很多做开发的.运维的都不陌生,是非常有用的一个终端请求工具,借助于它可以在命令行中进行HTTP.FTP等请求,在Linux ...

  4. mysql加强(6)~子查询简单介绍、子查询分类

    一.子查询简单介绍 1.什么是子查询? 一个查询之中嵌套了其他的若干查询. 在使用select 语句查询时,有时候where的查询条件中的限制条件不是一个确定的值,而是一个来自于另一个查询的结果. 子 ...

  5. Mac iterm2 配色以及终端大小写敏感解决方案

    iterm2是mac下非常好用的一款终端.但默认的配色实在不好用,经过一翻搜索终于找到了比较满意的,以下贴出博主的解决方案 配色 首先修改 ~/.bash_profile 加入一下代码 #enable ...

  6. Working hard to know your neighbor's margins:Local descriptor learning loss论文笔记

    Abstract 论文提出了一种新的训练方法,受到了 Lowe's matching criterion for SIFT的启发.这种新的loss,要比负责的正则方法更好.把这个新的loss方法结合L ...

  7. Vue中的发布订阅分析(Vue2/3中的 emit 实现)

    Vue中的发布订阅模式分析 模块:instanceEventEmiiter.ts(在下方有简单实现和解析) 在Vue3中,已经取消了对这个模块的引用,故而不再支持 $on.$off.$once相关的方 ...

  8. ApacheCN 深度学习译文集 20201229 更新

    新增了七个教程: TensorFlow 和 Keras 应用开发入门 零.前言 一.神经网络和深度学习简介 二.模型架构 三.模型评估和优化 四.产品化 TensorFlow 图像深度学习实用指南 零 ...

  9. bom-offset

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. WordPress子模板继承

    很多时候我们不想重写模板,而是想在某个模板的基础上进行修改,那么这个时候我们就需要用到模板继承技巧. 子主题开发 style.css 是必须的文件,只需要新增 Template: 父模板的文件夹名