[题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard
链接: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的更多相关文章
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry
链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
- Codeforces Round #254 (Div. 2) A DZY Loves Chessboard
先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...
- 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 ...
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- 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 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题
A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...
- 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 ...
随机推荐
- 使用Xamarin开发移动应用示例——数独游戏(一)项目的创建与调试
最近项目中需要移动客户端,由于团队基本上使用.Net产品线,所以决定使用Xmarin进行开发,这样技术路线统一,便于后期维护.官网上是这样介绍的" Xamarin 允许你使用 .NET 代码 ...
- pytest文档1-环境搭建
1.安装方法 pip install -U pytest 2.查看安装版本 pip show pytest pytest -version 3.快速开始 新建test开头py文件 打开test_sam ...
- Tomcat下载安装以及配置方法
Tomcat环境变量配置方法 注意一定要在java环境配置成功之后再来配置tomcat.我这里仅展现在Windows系统下载的安装方法 Tomcat下载地址如下: https://tomcat.apa ...
- SQL查询字段,起别名,列参与数学运算
13.简单查询 13.1.查询一个字段? select 字段名 from 表名: 其中要注意: select和from都是关键字 字段名和表名都是标识符. 强调: 对于SQL语句说,是通用的 所有的S ...
- js生成指定范围的随机整数
定义一个random()函数,原理是 随机数和最大值减最小值的差相乘 最后再加上最小值. function random(min, max) { return Math.floor(Math.rand ...
- linux挂载windows nfs
1.win下创建nfs文件夹并共享 2.登陆linux,执行 yum 3.创建挂载点 4.挂载win nfs 5./etc/fstab添加永久挂载 6.查看挂载磁盘,此时windows盘已落在linu ...
- DB2重组表失败处理办法
set integrity for XXX all immediate unchecked reorg table XXX allow no access 如set integrity for a a ...
- python input函数
函数 input() 让程序暂停运行,等待用户输入值,之后再把值赋给变量,输出.
- fio硬盘压力测试
fio测试工具支持同步(pread/pwrite)和异步(libaio)FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, lib ...
- Gulp自动化任务及nvm、npm常用命令
项目环境配置 nvm: node版本管理工具,安装和环境变量 cmd常用命令: · nvm use [version]: 切换至指定版本的node · nvm install no ...