链接: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. 深入理解http1.x、http 2和https

    转自 https://segmentfault.com/a/1190000015316332 一.HTTP/1.x Http1.x 缺陷:线程阻塞,在同一时间,同一域名的请求有一定数量限制,超过限制数 ...

  2. 无法自动装配。未找到 ‘xxxx’ 类型的 Bean。

    无法自动装配.未找到 'xxxx' 类型的 Bean. 1.解决办法 打开设置,找到编辑器->检查.把"自动装配Bean类",取消勾选,点击应用.确定.

  3. windows下使用LTP分词,安装pyltp

    1.LTP介绍 ltp是哈工大出品的自然语言处理工具箱, 提供包括中文分词.词性标注.命名实体识别.依存句法分析.语义角色标注等丰富. 高效.精准的自然语言处理技术.pyltp是python下对ltp ...

  4. 常见线程池 newScheduledThreadPool 定时执行任务的线程池 简单介绍

    一  定时任务 package com.aaa.threaddemo; import static java.util.concurrent.TimeUnit.NANOSECONDS; import ...

  5. HCNP Routing&Switching之组播技术-IGMP-Snooping

    前文我们了解了组播协议IGMP相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15700550.html:今天我们来聊一聊二层交换机处理组播报文所面临的 ...

  6. 【HTML】table表格拆分合并(colspan、rowspan)

    代码演示 横向合并: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...

  7. IM开发通信协议基础知识(一)---TCP、UDP、HTTP、SOCKET

    感谢大佬:https://www.cnblogs.com/sixindev/p/4723590.html 下面这些内容不了解也可以进行开发,深入了解一下还是收益良多 区别 TCP.UDP.HTTP.S ...

  8. python使用插入法实现链表反转

    # encoding=utf-8 class LNode(object): def __init__(self, x): self.data = x self.next = None def reve ...

  9. 一个label 混搭不同颜色,不同字体的文字.. by 徐

    效果如图箭头所示,只需要一个label就可以做到不同颜色或不同字体的效果 1 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, ...

  10. VUE项目部署到线上生产环境,Loading chunk xxx failed

    项目部署到生产环境,路由点击无效,报错 Loading chunk chunk-xxxxx failed.(missing xxxx) 加载失败,错误的路径. 话不多说,直接贴代码: vue.conf ...